-
Notifications
You must be signed in to change notification settings - Fork 26
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
Proof of concept: Weakref signalslots #98
base: main
Are you sure you want to change the base?
Conversation
Yes, I think I saved everything in a global var in order to clean all the signals in pyodide, |
@@ -88,7 +154,7 @@ def __init__(self, *args, **kwargs): | |||
self._connected_slots = [] | |||
_pyTTkSignal_obj._signals.append(self) | |||
|
|||
def connect(self, slot): | |||
def connect(self, slot, use_weak_ref=False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to think about having the "weak ref" true by default,
I am not sure what it is going to happen if connected to lambda or nedted functions.
For the naming convention I tried to use the camel case although not recommended by PEP8
because I started coding it following the QT api structure
If all signals should be weak, then all persons (including the users of pyTermTk) must be very careful:
I don't have a strong opinion, but I slightly prefer the option to give the caller of the This is only a Proof of Concept PR. It is not intended to be merged but to show the pros and cons. And please do not merge because it heavily relies on the fact that (at the moment) many widgets (like a |
(Signal-)slots with weak references
Subtitle: Tired of slot-bookkeeping?
Facts
_pyTTkSignal_obj
) are saved inside a list in the class variable_pyTTkSignal_obj._signals
TTkWindow
and its buttons (close, minimize, etc.)_connected_slots
Consequence
Even closed windows cannot be garbage collected because via the signal list, and the connected slots all the buttons are "reachable" and the buttons have links to their window and hence the windows are reachable.
Solutions
There are many many ways to solve this problem. In the PR I tried one (in my opinion a lazy and elegant one).
There is a
WeakrefSlot
class. Instances of this class behave (transparently) like "slots", i.e. they are callable. The "only" difference to a "normal" slot is, that the target-function/listener is saved via aweakref.ref
orweakref.WeakMethod
. Hence theWeakrefSlot
instances do not prevent the garbage collection.Dead
WeakrefSlot
instances (i.e.WeakrefSlot
where the target-function is not reachable anymore because it was garbage collected) are cleaned up lazily and automatically in theemit
method.I've attached a nice test
tests/test.ui.024.weakslots.py
:weakslots.webm
Some questions remain
WeakrefSlot
can only be used if the target-function/slot is "weakly referable", i.e. it allows for a__weakref__
attribute. DoesTermTk.TTkWidget
and subclasses want to support this? (see Attention)Attention
The
tests/test.ui.024.weakslots.py
code uses a current "bug" in TermTk. All the Widgets have a__dict__
(as discussed in an other PR). That's the reason why e.g.ShowEventWindow.show_event
orTTkWindow.close
are "weakly referable". Without__weakref__
attribute there is no chance to get a weakref for such objects.