Search code examples
pythonlrufunctools

Unable to step into lru_cache'_lru_cache_wrapper


For some reason I am unable to step into the _lru_cache_wrapper's code at all.

I see that the lru_cache decorator returns a closure with _lru_cache_wrapper within functools.py. However when I put a breakpoint in _lru_cache_wrapper's code it never gets triggered. I have even put a print() statement which doesn't appear to get hit. I am really puzzled by this since the lru_cache code hits the breakpoint but not _lru_cache_wrapper.

from functools import lru_cache

@lru_cache()
def foo():
    print('foo')

if __name__ == '__main__':
    foo()
    print(foo.cache_info())

Solution

  • The version in functools.py doesn't get used. It's replaced by a version written in C from _functools:

    try:
        from _functools import _lru_cache_wrapper
    except ImportError:
        pass