Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
17 changes: 17 additions & 0 deletions 17 Lib/test/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import struct
import collections
import itertools
import gc


class FunctionCalls(unittest.TestCase):
Expand Down Expand Up @@ -457,6 +458,22 @@ def test_fastcall_keywords(self):
result = _testcapi.pyobject_fastcallkeywords(func, args, kwnames)
self.check_result(result, expected)

def test_fastcall_clearing_dict(self):
# Test bpo-36907: the point of the test is just checking that this
# does not crash.
class IntWithDict:
__slots__ = ["kwargs"]
def __init__(self, **kwargs):
self.kwargs = kwargs
def __index__(self):
self.kwargs.clear()
gc.collect()
return 0
x = IntWithDict(dont_inherit=IntWithDict())
# We test the argument handling of "compile" here, the compilation
# itself is not relevant. When we pass flags=x below, x.__index__() is
# called, which changes the keywords dict.
compile("pass", "", "exec", x, **x.kwargs)
encukou marked this conversation as resolved.
Outdated
Show resolved Hide resolved

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash when calling a C function with a keyword dict (``f(**kwargs)``)
and changing the dict ``kwargs`` while that function is running.
17 changes: 12 additions & 5 deletions 17 Objects/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,14 @@ _PyMethodDef_RawFastCallDict(PyMethodDef *method, PyObject *self,
}

result = (*fastmeth) (self, stack, nargs, kwnames);
if (stack != args) {
if (kwnames != NULL) {
Py_ssize_t i, n = nargs + PyTuple_GET_SIZE(kwnames);
for (i = 0; i < n; i++) {
Py_DECREF(stack[i]);
}
PyMem_Free((PyObject **)stack);
Py_DECREF(kwnames);
}
Py_XDECREF(kwnames);
break;
}

Expand Down Expand Up @@ -1334,8 +1338,11 @@ _PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
return -1;
}

/* Copy position arguments (borrowed references) */
memcpy(stack, args, nargs * sizeof(stack[0]));
/* Copy positional arguments */
for (i = 0; i < nargs; i++) {
Py_INCREF(args[i]);
stack[i] = args[i];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to not keep memcpy for the bulk copy, and only add an INCREF loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to not keep memcpy for the bulk copy, and only add an INCREF loop?

Not really, it's mostly a matter of style. I like doing the INCREF at the same time as the assignment. I also find the for loop variant simpler to understand.


kwstack = stack + nargs;
pos = i = 0;
Expand All @@ -1344,8 +1351,8 @@ _PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
called in the performance critical hot code. */
while (PyDict_Next(kwargs, &pos, &key, &value)) {
Py_INCREF(key);
Py_INCREF(value);
PyTuple_SET_ITEM(kwnames, i, key);
/* The stack contains borrowed references */
kwstack[i] = value;
i++;
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.