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 <p.f.mo...@gmail.com> wrote:

> On Tue, 12 Jan 2021 at 17:16, Christopher Barker <python...@gmail.com>
> 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-le...@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/C6YULKSV5RV36WSWMGKWPMLUFDL7YN2Y/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Sebastian Kreft
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/EK5DBSL57BMXEQTD7JYC3GVRN2T3YH6K/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to