Skip to content

[3.7] bpo-36650: Fix handling of empty keyword args in C version of lru_cache. (GH-12881) #12888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,20 @@ def f(x):
self.assertEqual(f(20), '.20.')
self.assertEqual(f.cache_info().currsize, 10)

def test_lru_bug_36650(self):
# C version of lru_cache was treating a call with an empty **kwargs
# dictionary as being distinct from a call with no keywords at all.
# This did not result in an incorrect answer, but it did trigger
# an unexpected cache miss.

@self.module.lru_cache()
def f(x):
pass

f(0)
f(0, **{})
self.assertEqual(f.cache_info().hits, 1)

def test_lru_hash_only_once(self):
# To protect against weird reentrancy bugs and to improve
# efficiency when faced with slow __hash__ methods, the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The C version of functools.lru_cache() was treating calls with an empty
``**kwargs`` dictionary as being distinct from calls with no keywords at all.
This did not result in an incorrect answer, but it did trigger an unexpected
cache miss.
7 changes: 3 additions & 4 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,8 +750,10 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed)
PyObject *key, *keyword, *value;
Py_ssize_t key_size, pos, key_pos, kwds_size;

kwds_size = kwds ? PyDict_GET_SIZE(kwds) : 0;

/* short path, key will match args anyway, which is a tuple */
if (!typed && !kwds) {
if (!typed && !kwds_size) {
if (PyTuple_GET_SIZE(args) == 1) {
key = PyTuple_GET_ITEM(args, 0);
if (PyUnicode_CheckExact(key) || PyLong_CheckExact(key)) {
Expand All @@ -765,9 +767,6 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed)
return args;
}

kwds_size = kwds ? PyDict_GET_SIZE(kwds) : 0;
assert(kwds_size >= 0);

key_size = PyTuple_GET_SIZE(args);
if (kwds_size)
key_size += kwds_size * 2 + 1;
Expand Down