Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix hard reference to objects in emitted arguments #301

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/psygnal/_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Callable
from typing import TYPE_CHECKING, Any, Callable

from ._weak_callback import WeakCallback

Expand All @@ -19,13 +19,13 @@ class EmitLoopError(Exception):

def __init__(
self,
cb: WeakCallback | Callable,
args: tuple,
cb: WeakCallback | Callable | None,
args: tuple[Any, ...] | None,
exc: BaseException,
signal: SignalInstance | None = None,
) -> None:
self.exc = exc
self.args = args
self.args = args or ()
self.__cause__ = exc # mypyc doesn't set this, but uncompiled code would
if signal is None:
sig_name = ""
Expand Down
7 changes: 5 additions & 2 deletions src/psygnal/_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,9 @@ def __call__(
check_types=check_types,
)

_args: tuple[Any, ...]
_caller: WeakCallback | None

def _run_emit_loop(self, args: tuple[Any, ...]) -> None:
with self._lock:
self._emit_queue.append(args)
Expand All @@ -1054,17 +1057,17 @@ def _run_emit_loop(self, args: tuple[Any, ...]) -> None:
) from e
finally:
self._emit_queue.clear()
self._args = ()
self._caller = None

def _run_emit_loop_immediate(self) -> None:
self._args = args = self._emit_queue.popleft()
caller = None
for caller in self._slots:
self._caller = caller
caller.cb(args)

def _run_emit_loop_deferred(self) -> None:
i = 0
caller = None
while i < len(self._emit_queue):
self._args = args = self._emit_queue[i]
for caller in self._slots:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_psygnal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,3 +1090,23 @@ def f():
mock.assert_called_once()

assert t.sig is t.sig


def test_emit_should_not_prevent_gc():
from weakref import WeakSet

from psygnal import Signal

class Obj:
pass

class SomethingWithSignal:
changed = Signal(object)

object_instances: WeakSet[Obj] = WeakSet()
something = SomethingWithSignal()
obj = Obj()
object_instances.add(obj)
something.changed.emit(obj)
del obj
assert len(object_instances) == 0
Loading