Skip to content

Commit

Permalink
Improved error message for infinite update loop & support all Qt libs (
Browse files Browse the repository at this point in the history
…#28)

* print name of watcher callback for cyclical dependency

* support all Qt libs and provide better errors in watchers in general

* correct error message

* better error message

* remove test lines

* Update pyproject.toml

* Update __init__.py

* update lockfile
  • Loading branch information
Korijn authored Sep 23, 2021
1 parent 9240d74 commit 203577a
Show file tree
Hide file tree
Showing 6 changed files with 1,053 additions and 1,038 deletions.
2 changes: 1 addition & 1 deletion observ/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.4.0"
__version__ = "0.4.1"


from .api import *
20 changes: 14 additions & 6 deletions observ/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
and should be integrated in the event loop of your choosing.
"""
from bisect import bisect
import importlib


class Scheduler:
Expand All @@ -19,10 +20,16 @@ def register_qt(self):
"""
Utility function for integration with Qt event loop
"""
# Currently only supports PySide6
from PySide6.QtCore import QTimer
for qt in ("PySide6", "PyQt6", "PySide2", "PyQt5", "PySide", "PyQt4"):
try:
QtCore = importlib.import_module(f"{qt}.QtCore")
break
except ImportError:
continue
else:
raise ImportError("Could not import QtCore")

self.timer = QTimer()
self.timer = QtCore.QTimer()
self.timer.setSingleShot(True)
self.timer.timeout.connect(scheduler.flush)
# Set interval to 0 to trigger the timer as soon
Expand Down Expand Up @@ -58,9 +65,10 @@ def flush(self):
if watcher.id in self.has:
self.circular[watcher.id] = self.circular.get(watcher.id, 0) + 1
if self.circular[watcher.id] > 100:
# TODO: help user to figure out which watcher
# or function this is about
raise RecursionError("Infinite update loop detected")
raise RecursionError(
"Infinite update loop detected in watched"
f" expression {watcher.fn_fqn}"
)

self.index += 1

Expand Down
4 changes: 4 additions & 0 deletions observ/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ def depend(self) -> None:
if Dep.stack:
for dep in self._deps:
dep.depend()

@property
def fn_fqn(self) -> str:
return f"{self.fn.__module__}.{self.fn.__qualname__}"
Loading

0 comments on commit 203577a

Please sign in to comment.