Skip to content

Python implementation of functools.lru_cache does not check that argument is callable #144030

Closed

Description

@cfbolz

Bug report

Bug description:

The C implementation of functools.lru_cache checks whether its argument is callable and raises an exception if not. The Python version does not do that:

import sys
sys.modules['_functools'] = None

import functools
functools.lru_cache()(1) # does not raise

without blocking the import of _functools you get:

Traceback (most recent call last):
  File "/home/cfbolz/projects/cpython/functoolserror.py", line 5, in <module>
    functools.lru_cache()(1)
    ~~~~~~~~~~~~~~~~~~~~~^^^
  File "/home/cfbolz/projects/cpython/Lib/functools.py", line 598, in decorating_function
    wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
TypeError: the first argument must be callable

I think this should simply be fixed in the Python implementation and will prepare a patch.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Jan 19, 2026
  2. cfbolz commented on Jan 19, 2026

    @cfbolz
    ContributorAuthor

    (bug found by @zahinwahab via fuzzing and reported first here: pypy/pypy#5369 )

  3. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Jan 19, 2026
  4. self-assigned this
    on Jan 20, 2026
  5. added a commit that references this issue on Jan 21, 2026
  6. vstinner commented on Jan 21, 2026

    @vstinner
    Member

    Merged, thanks for the fix @cfbolz.

  7. added a commit that references this issue on Feb 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions