You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Expose weakref callback for introspection purposes. #61843
It would be nice to be able to access the callback of a weakref as an attribute on the weakref itself. For example:
Python 3.4.0a0 (default:2bf154ca43c6+, Apr 6 2013, 13:31:29)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import weakref
>>> x = {1, 2, 3}
>>> ref = weakref.ref(x, lambdaref: print("done"))
>>> ref.__callback__
<function <lambda> at 0x1004f56d0>
>>> del x
done
>>> ref.__callback__ # Returns None
I encountered this while writing a tool to show graphs of Python objects and their references to each other: I wanted to be able to annotate each edge of the graph. For something like a function, it's easy to use introspection to compare the reference target with f.__code__, f.__annotations__, etc. For a weakref, I couldn't find an easy way to retrieve the callback (or even determine whether there *was* a callback associated to the weakref). One can do a "gc.get_referents" call and hope that if there's exactly one object returned it's the callback, but that won't work so well with weakref.ref subclasses.
Patch attached: it has tests but no doc updates as yet.
True: I'm not sure what to do about that---there are other tests in that test module that also rely on del resulting in immediate cleanup. I'm not sure what other implementations are currently doing with this test module.
I could mark the new test as cpython only, or add a gc.collect (but I'm not sure if the latter is enough, either).
there are other tests in that test module that also rely on del resulting in immediate cleanup
I would mark them all as 'cpython-only', since they are. Other implementation must be omitting this test module, ignoring its failures, or patching out the specific tests.
sbt commented on Apr 12, 2013
sbtmannequin
Mannequin
More actions
I could mark the new test as cpython only, or add a gc.collect (but I'm
not sure if the latter is enough, either).
test.support.gc_collect() should work on non-refcounted implementations.
Activity
mdickinson commentedon Apr 6, 2013
It would be nice to be able to access the callback of a weakref as an attribute on the weakref itself. For example:
I encountered this while writing a tool to show graphs of Python objects and their references to each other: I wanted to be able to annotate each edge of the graph. For something like a function, it's easy to use introspection to compare the reference target with f.__code__, f.__annotations__, etc. For a weakref, I couldn't find an easy way to retrieve the callback (or even determine whether there *was* a callback associated to the weakref). One can do a "gc.get_referents" call and hope that if there's exactly one object returned it's the callback, but that won't work so well with weakref.ref subclasses.
Patch attached: it has tests but no doc updates as yet.
pitrou commentedon Apr 6, 2013
Sounds fine to me.
benjaminp commentedon Apr 6, 2013
Instead of a getset, I think you just use a read-only T_OBJECT member.
mdickinson commentedon Apr 6, 2013
Ah yes; that's easier. New patch including doc updates.
pitrou commentedon Apr 6, 2013
"del x" will not be enough on non-refcounted implementations.
mdickinson commentedon Apr 6, 2013
True: I'm not sure what to do about that---there are other tests in that test module that also rely on del resulting in immediate cleanup. I'm not sure what other implementations are currently doing with this test module.
I could mark the new test as cpython only, or add a gc.collect (but I'm not sure if the latter is enough, either).
pitrou commentedon Apr 6, 2013
Ah, then we can probably keep using the same style.
terryjreedy commentedon Apr 12, 2013
I would mark them all as 'cpython-only', since they are. Other implementation must be omitting this test module, ignoring its failures, or patching out the specific tests.
sbt commentedon Apr 12, 2013
test.support.gc_collect() should work on non-refcounted implementations.
mdickinson commentedon Apr 12, 2013
Ah, test.support.gc_collect looks useful. Thanks!
python-dev commentedon Apr 13, 2013
New changeset 9e7d31b04d78 by Mark Dickinson in branch 'default':
Issue bpo-17643: Add __callback__ attribute to weakref.ref.
http://hg.python.org/cpython/rev/9e7d31b04d78
mdickinson commentedon Apr 13, 2013
Committed, using support.gc_collect for the test that requires garbage collection. Thanks for the reviews!