classification
Title: Restore weakref support for lru_cache wrappers
Type: behavior Stage: resolved
Components: Extension Modules Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, rhettinger, serhiy.storchaka
Priority: normal Keywords: easy (C), patch

Created on 2020-05-04 20:36 by rhettinger, last changed 2020-05-05 21:15 by rhettinger. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 19938 merged Dennis Sweeney, 2020-05-05 17:10
Messages (5)
msg368081 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-05-04 20:36
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).
msg368093 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python triager) Date: 2020-05-04 23:35
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)>
msg368097 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-05-05 01:59
Yes.  That is the desired behavior.  Use Objects/setobject.c and Include/setobject.h as a model.
msg368207 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-05-05 21:14
New changeset 1253c3ef70ea5632d32ae19579a14152db0d45c1 by Dennis Sweeney in branch 'master':
bpo-40504: Allow weakrefs to lru_cache objects (GH-19938)
https://github.com/python/cpython/commit/1253c3ef70ea5632d32ae19579a14152db0d45c1
msg368208 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-05-05 21:15
Thanks for the PE :-)
History
Date User Action Args
2020-05-05 21:15:17rhettingersetstatus: open -> closed
resolution: fixed
messages: + msg368208

stage: patch review -> resolved
2020-05-05 21:14:39rhettingersetmessages: + msg368207
2020-05-05 17:10:00Dennis Sweeneysetkeywords: + patch
stage: patch review
pull_requests: + pull_request19253
2020-05-05 01:59:27rhettingersetmessages: + msg368097
2020-05-04 23:35:18Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg368093
2020-05-04 20:36:26rhettingercreate