Skip to content

Restore weakref support for lru_cache wrappers #84684

Description

@rhettinger
BPO 40504
Nosy @rhettinger, @serhiy-storchaka, @sweeneyde
PRs
  • bpo-40504: Allow weakrefs to lru_cache objects #19938
  • 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 2020-05-05.21:15:17.962>
    created_at = <Date 2020-05-04.20:36:26.737>
    labels = ['extension-modules', 'easy', 'type-bug', '3.9']
    title = 'Restore weakref support for lru_cache wrappers'
    updated_at = <Date 2020-05-05.21:15:17.961>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2020-05-05.21:15:17.961>
    actor = 'rhettinger'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-05-05.21:15:17.962>
    closer = 'rhettinger'
    components = ['Extension Modules']
    creation = <Date 2020-05-04.20:36:26.737>
    creator = 'rhettinger'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40504
    keywords = ['patch', 'easy (C)']
    message_count = 5.0
    messages = ['368081', '368093', '368097', '368207', '368208']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'serhiy.storchaka', 'Dennis Sweeney']
    pr_nums = ['19938']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue40504'
    versions = ['Python 3.9']

    Activity

    1. rhettinger commented on May 4, 2020

      @rhettinger
      ContributorAuthor

      The pure python version of lru_cache() wrappers supported weak references. It's not essential, but I have used it a couple of times. In the spirit of PEP-399, the C version should add back the weakref support (and a test).

    2. sweeneyde commented on May 4, 2020

      @sweeneyde
      Member

      I can submit a PR. Just making sure I understand, is this essentially the desired behavior change?

      import weakref
      import functools
      
      if 0:
          from test.support import import_fresh_module
          functools = import_fresh_module('functools', blocked=['_functools'])
      
      @functools.lru_cache
      def f(x):
          return x
      
      ref_to_f = weakref.ref(f)
      print(ref_to_f)

      # Current:
      # TypeError: cannot create weak reference to 'functools._lru_cache_wrapper' object
      # Desired:
      # <weakref at 0x000001ABFE0F5090; to 'function' at 0x000001ABFE2D5AF0 (f)>

    3. rhettinger commented on May 5, 2020

      @rhettinger
      ContributorAuthor

      Yes. That is the desired behavior. Use Objects/setobject.c and Include/setobject.h as a model.

    4. rhettinger commented on May 5, 2020

      @rhettinger
      ContributorAuthor

      New changeset 1253c3e by Dennis Sweeney in branch 'master':
      bpo-40504: Allow weakrefs to lru_cache objects (GH-19938)
      1253c3e

    5. rhettinger commented on May 5, 2020

      @rhettinger
      ContributorAuthor

      Thanks for the PE :-)

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

    No one assigned

      Labels

      3.9 (EOL)end of lifeeasyextension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions