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

Expose weakref callback for introspection purposes. #61843

Closed
mdickinson opened this issue Apr 6, 2013 · 12 comments
Closed

Expose weakref callback for introspection purposes. #61843

mdickinson opened this issue Apr 6, 2013 · 12 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@mdickinson
Copy link
Member

BPO 17643
Nosy @terryjreedy, @mdickinson, @pitrou, @benjaminp
Files
  • weakref___callback__.patch
  • weakref___callback__2.patch
  • 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 = 'https://github.com/mdickinson'
    closed_at = <Date 2013-04-13.14:47:28.637>
    created_at = <Date 2013-04-06.12:54:57.703>
    labels = ['interpreter-core', 'type-feature']
    title = 'Expose weakref callback for introspection purposes.'
    updated_at = <Date 2013-04-13.14:47:28.636>
    user = 'https://github.com/mdickinson'

    bugs.python.org fields:

    activity = <Date 2013-04-13.14:47:28.636>
    actor = 'mark.dickinson'
    assignee = 'mark.dickinson'
    closed = True
    closed_date = <Date 2013-04-13.14:47:28.637>
    closer = 'mark.dickinson'
    components = ['Interpreter Core']
    creation = <Date 2013-04-06.12:54:57.703>
    creator = 'mark.dickinson'
    dependencies = []
    files = ['29685', '29687']
    hgrepos = []
    issue_num = 17643
    keywords = ['patch']
    message_count = 12.0
    messages = ['186122', '186124', '186125', '186132', '186151', '186154', '186155', '186655', '186665', '186668', '186722', '186723']
    nosy_count = 6.0
    nosy_names = ['terry.reedy', 'mark.dickinson', 'pitrou', 'benjamin.peterson', 'python-dev', 'sbt']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue17643'
    versions = ['Python 3.4']

    @mdickinson
    Copy link
    Member Author

    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, lambda ref: 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.

    @mdickinson mdickinson added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Apr 6, 2013
    @pitrou
    Copy link
    Member

    pitrou commented Apr 6, 2013

    Sounds fine to me.

    @benjaminp
    Copy link
    Contributor

    Instead of a getset, I think you just use a read-only T_OBJECT member.

    @mdickinson
    Copy link
    Member Author

    Ah yes; that's easier. New patch including doc updates.

    @pitrou
    Copy link
    Member

    pitrou commented Apr 6, 2013

    "del x" will not be enough on non-refcounted implementations.

    @mdickinson
    Copy link
    Member Author

    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
    Copy link
    Member

    pitrou commented Apr 6, 2013

    Ah, then we can probably keep using the same style.

    @terryjreedy
    Copy link
    Member

    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
    Copy link
    Mannequin

    sbt mannequin commented Apr 12, 2013

    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.

    @mdickinson
    Copy link
    Member Author

    Ah, test.support.gc_collect looks useful. Thanks!

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented 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
    Copy link
    Member Author

    Committed, using support.gc_collect for the test that requires garbage collection. Thanks for the reviews!

    @mdickinson mdickinson self-assigned this Apr 13, 2013
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants