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

weakref cannot handle bound methods (in contrast to docu) #42003

Closed
graik mannequin opened this issue May 22, 2005 · 2 comments
Closed

weakref cannot handle bound methods (in contrast to docu) #42003

graik mannequin opened this issue May 22, 2005 · 2 comments
Labels
extension-modules C modules in the Modules dir

Comments

@graik
Copy link
Mannequin

graik mannequin commented May 22, 2005

BPO 1206537
Nosy @arigo

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2005-05-23.14:08:14.000>
created_at = <Date 2005-05-22.14:37:09.000>
labels = ['extension-modules', 'invalid']
title = 'weakref cannot handle bound methods (in contrast to docu)'
updated_at = <Date 2005-05-23.14:08:14.000>
user = 'https://bugs.python.org/graik'

bugs.python.org fields:

activity = <Date 2005-05-23.14:08:14.000>
actor = 'arigo'
assignee = 'none'
closed = True
closed_date = None
closer = None
components = ['Extension Modules']
creation = <Date 2005-05-22.14:37:09.000>
creator = 'graik'
dependencies = []
files = []
hgrepos = []
issue_num = 1206537
keywords = []
message_count = 2.0
messages = ['25395', '25396']
nosy_count = 2.0
nosy_names = ['arigo', 'graik']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1206537'
versions = []

@graik
Copy link
Mannequin Author

graik mannequin commented May 22, 2005

According to the documentation of the weakref module,
weakreferences can be applied to
"...class instances, functions written in Python (but
not in C), methods (both bound and unbound)..."

In reality, bound methods cannot be referenced (see bug
813299):

import weakref

## this works:
def testF( event ):    pass
r = weakref.ref( testF )

## this doesnt:
class EventListener:
    def handleEventA( self, event ):
        pass

t = EventListener()
## gives a "dead" ref
r = weakref.ref( t.handleEventA )

This behaviour is unexpected for anyone not aquainted
to the inner clockwork of python and unfortunate
because it, for example, prevents to keep weak
references to callback methods in event handling
patterns.

A workaround is proposed at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81253

Discussion:
(minimal) Solution 1:
Change the weakref documentation

(preferred) Solution 2:
Adapt weakref to allow references to bound methods

Perhaps this bug should be converted into a
documentation bug and a feature request.

Python version 2.3 and 2.4
OS: Linux 2.6

@graik graik mannequin closed this as completed May 22, 2005
@graik graik mannequin added invalid extension-modules C modules in the Modules dir labels May 22, 2005
@arigo
Copy link
Mannequin

arigo mannequin commented May 23, 2005

Logged In: YES
user_id=4771

Of course, the weakref documentation is ultimately right,
and the problem, unrelated to bound methods, is that you
always get a dead weakref if you do

    weakref.ref(<expr-than-returns-a-new-object>)

But I'm not being particularly helpful here.

A trick simpler than the cookbook proposals is to force the
method object to be alive as long as the instance itself by
storing it on the instance:

    obj = MyClass()
    obj.m = obj.m
    ref = weakref.ref(obj.m)

This works because the last "obj.m" returns an existing
object, as opposed to one created just-in-time. This
might be mentioned in the weakref documentation with the
comment that it's a general rule to be careful not to take
weakrefs to short-lived object; the same problem would
occur e.g. when taking a weakref to "obj.a" where "a" is
a computed property. Storing the result back on "obj" --
under the same or another name -- is a workaround.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir
Projects
None yet
Development

No branches or pull requests

0 participants