Skip to content

Expose weakref callback for introspection purposes. #61843

Closed

Description

@mdickinson
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']

    Activity

    1. mdickinson commented on Apr 6, 2013

      @mdickinson
      MemberAuthor

      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.

    2. pitrou commented on Apr 6, 2013

      @pitrou
      Member

      Sounds fine to me.

    3. benjaminp commented on Apr 6, 2013

      @benjaminp
      Contributor

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

    4. mdickinson commented on Apr 6, 2013

      @mdickinson
      MemberAuthor

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

    5. pitrou commented on Apr 6, 2013

      @pitrou
      Member

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

    6. mdickinson commented on Apr 6, 2013

      @mdickinson
      MemberAuthor

      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).

    7. pitrou commented on Apr 6, 2013

      @pitrou
      Member

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

    8. terryjreedy commented on Apr 12, 2013

      @terryjreedy
      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.

    9. sbt commented on Apr 12, 2013

      sbtmannequin
      Mannequin

      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.

    10. mdickinson commented on Apr 12, 2013

      @mdickinson
      MemberAuthor

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

    11. python-dev commented on Apr 13, 2013

      python-devmannequin
      Mannequin

      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

    12. mdickinson commented on Apr 13, 2013

      @mdickinson
      MemberAuthor

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

    13. self-assigned this
      on Apr 13, 2013
    14. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions