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

Restore weakref support for lru_cache wrappers #84684

Closed
rhettinger opened this issue May 4, 2020 · 5 comments
Closed

Restore weakref support for lru_cache wrappers #84684

rhettinger opened this issue May 4, 2020 · 5 comments
Labels
3.9 only security fixes easy extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@rhettinger
Copy link
Contributor

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

    @rhettinger
    Copy link
    Contributor Author

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

    @rhettinger rhettinger added 3.9 only security fixes extension-modules C modules in the Modules dir easy type-bug An unexpected behavior, bug, or error labels May 4, 2020
    @sweeneyde
    Copy link
    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)>

    @rhettinger
    Copy link
    Contributor Author

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

    @rhettinger
    Copy link
    Contributor Author

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

    @rhettinger
    Copy link
    Contributor Author

    Thanks for the PE :-)

    @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
    3.9 only security fixes easy extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants