Actually you can get the cache data from the python version, for that you need to force the use of the python version of functools def get_cache(f): freevars = f.__code__.co_freevars index = freevars.index('cache') return f.__closure__[index].cell_contents import importlib.abc class ForcePythonLruCache(importlib.abc.MetaPathFinder): def find_spec(self, fullname, path, target=None): if fullname == '_functools': raise ImportError('_functools not available') import sys del sys.modules['functools'] del sys.modules['_functools'] import functools @functools.lru_cache def test(x): return x test(1) test(2) test('a') test('foo') print(list(get_cache(test).keys())) On Tue, Jan 12, 2021 at 4:09 PM Paul Moore
On Tue, 12 Jan 2021 at 17:16, Christopher Barker
wrote: Is the implementation of lru_cache too opaque to poke into it without an
existing method? Or write a quick monkey-patch?
Sorry for not checking myself, but the ability to do that kind of thing
is one of the great things about a dynamic open source language.
I've only looked at the Python implementation, but the cache is a local variable in the wrapper, unavailable from outside. The cache information function is a closure that references that local variable, assigned as an attribute of the wrapped function.
It's about as private as it's possible to get in Python (not particularly because it's *intended* to hide anything, as far as I can tell, more likely for performance or some other implementation reason).
Paul _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/C6YULK... Code of Conduct: http://python.org/psf/codeofconduct/
-- Sebastian Kreft