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

saferef.safeRef() doesn't update BoundMethodWeakref.deletionMethods #5

Open
z-tux-mind opened this issue Jun 30, 2023 · 1 comment
Open

Comments

@z-tux-mind
Copy link

Thank you for this great library, it is of great help for event-driven apps 😊

Calling again saferef.safeRef(object, newOnDeleteCallback) results in setting the BoundMethodWeakref.deletionMethods to [newOnDeleteCallback] instead of [oldOnDeleteCallback, newOnDeleteCallback].

I think this is caused by a wrong usage of __new__. Objects returned by __new__ are consequently passed to __init__.
Therefore, __init__ is re-run when another reference is created, which is not the intended behaviour as the code is written.

def __new__( cls, target, onDelete=None, *arguments,**named ):

A simple solution would be to check if the instance has already been initialised in __init__:

def __init__(self, ...):
    if hasattr(self, 'deletionMethods'): return

Also, another change I would add is:

# in __new__
if onDelete is not None:
    current.deletionMethods.append( onDelete)
# in __init__
self.deletionMethods = [onDelete] if onDelete is not None else []

Thank you for your time and great help!

@mcfletch
Copy link
Owner

Sorry, been working on paying work rather intently. I'm taking off time next month, so should be able to look into this then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants