diff --git a/Lib/test/test_call.py b/Lib/test/test_call.py index 6936f093e3db19..f243db26b90ac4 100644 --- a/Lib/test/test_call.py +++ b/Lib/test/test_call.py @@ -215,6 +215,32 @@ def test_varargs_keywords_ext(self): (self.expected_self, (1, 2), {'a': 3, 'b': 4}) ) + def test_keywords_copied_python(self): + d = {} + self.assertIsNot( + self.obj.meth_varargs_keywords(**d)[2], + d + ) + + d = {'a': 1} + self.assertIsNot( + self.obj.meth_varargs_keywords(**d)[2], + d + ) + + def test_keywords_copied_capi(self): + d = {} + self.assertIsNot( + self.obj.object_call(self.obj.meth_varargs_keywords, (), d)[2], + d + ) + + d = {'a': 1} + self.assertIsNot( + self.obj.object_call(self.obj.meth_varargs_keywords, (), d)[2], + d + ) + def test_o(self): self.assertEqual(self.obj.meth_o(1), (self.expected_self, 1)) diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-05-02-20-19-06.gh-issue-86199.IZbF0m.rst b/Misc/NEWS.d/next/Core and Builtins/2022-05-02-20-19-06.gh-issue-86199.IZbF0m.rst new file mode 100644 index 00000000000000..f01b50cec66428 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-05-02-20-19-06.gh-issue-86199.IZbF0m.rst @@ -0,0 +1,6 @@ +:c:func:`PyObject_Call` now guarantees any kwargs :class:`dict` passed is +shallow-copied before being passed to the callee, matching the documented +equivalence with ``callable(*args, **kwargs)``. +When a function call's sole source of keyword arguments is a single ``**`` +unpacked :class:`dict`, no copy is performed when the callable uses +:ref:`vectorcall `, which does not expose the dict to the callee. diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 9073f33e226bd3..f830d97b05d119 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -5508,6 +5508,25 @@ _null_to_none(PyObject* obj) return obj; } +/* For testing unpacking behaviors in C API without eval loop + * changing behavior by potentially copying structures dict implicitly + */ +static PyObject* +object_call(PyObject *self, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *func, *posargs, *kwargs; + + if (!_PyArg_CheckPositional("object_call", nargs, 2, 3)) { + return NULL; + } + + func = args[0]; + posargs = args[1]; + kwargs = PyVectorcall_NARGS(nargs) == 3 ? args[2] : NULL; + + return PyObject_Call(func, posargs, kwargs); +} + static PyObject* meth_varargs(PyObject* self, PyObject* args) { @@ -6202,6 +6221,7 @@ static PyMethodDef TestMethods[] = { #endif {"write_unraisable_exc", test_write_unraisable_exc, METH_VARARGS}, {"sequence_getitem", sequence_getitem, METH_VARARGS}, + {"object_call", (PyCFunction)(void(*)(void))object_call, METH_FASTCALL}, {"meth_varargs", meth_varargs, METH_VARARGS}, {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS}, {"meth_o", meth_o, METH_O}, @@ -7358,6 +7378,7 @@ static PyType_Spec HeapCTypeSetattr_spec = { }; static PyMethodDef meth_instance_methods[] = { + {"object_call", (PyCFunction)(void(*)(void))object_call, METH_FASTCALL}, {"meth_varargs", meth_varargs, METH_VARARGS}, {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS}, {"meth_o", meth_o, METH_O}, @@ -7380,6 +7401,7 @@ static PyTypeObject MethInstance_Type = { }; static PyMethodDef meth_class_methods[] = { + {"object_call", (PyCFunction)(void(*)(void))object_call, METH_FASTCALL|METH_CLASS}, {"meth_varargs", meth_varargs, METH_VARARGS|METH_CLASS}, {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS|METH_CLASS}, {"meth_o", meth_o, METH_O|METH_CLASS}, @@ -7402,6 +7424,7 @@ static PyTypeObject MethClass_Type = { }; static PyMethodDef meth_static_methods[] = { + {"object_call", (PyCFunction)(void(*)(void))object_call, METH_FASTCALL|METH_STATIC}, {"meth_varargs", meth_varargs, METH_VARARGS|METH_STATIC}, {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS|METH_STATIC}, {"meth_o", meth_o, METH_O|METH_STATIC}, diff --git a/Objects/call.c b/Objects/call.c index 448476223adc04..0369b9a19e5a8e 100644 --- a/Objects/call.c +++ b/Objects/call.c @@ -309,6 +309,31 @@ _PyObject_FastCall(PyObject *func, PyObject *const *args, Py_ssize_t nargs) } +static PyObject* +copy_kwds(PyObject *mapping) +{ + PyObject *newdict = NULL; + + if (mapping != NULL) { + assert(PyDict_Check(mapping)); + + Py_ssize_t kwlen = PyDict_Size(mapping); + assert(kwlen >= 0); + if (kwlen > 0) { + newdict = PyDict_New(); + if (newdict == NULL) { + return NULL; + } + if (_PyDict_MergeEx(newdict, mapping, 2) < 0) { + Py_DECREF(newdict); + return NULL; + } + } + } + + return newdict; +} + PyObject * _PyObject_Call(PyThreadState *tstate, PyObject *callable, PyObject *args, PyObject *kwargs) @@ -328,6 +353,7 @@ _PyObject_Call(PyThreadState *tstate, PyObject *callable, return _PyVectorcall_Call(tstate, vector_func, callable, args, kwargs); } else { + PyObject *kwcopy; call = Py_TYPE(callable)->tp_call; if (call == NULL) { _PyErr_Format(tstate, PyExc_TypeError, @@ -336,14 +362,28 @@ _PyObject_Call(PyThreadState *tstate, PyObject *callable, return NULL; } + /* Adhere to documented equivalence: + callable(*args, **kwargs) == PyObject_Call(callable, args, kwargs) + + The dict must be copied to ensure the caller's copy isn't modified. + Not needed when following vectorcall paths that don't use the dict. + */ + kwcopy = copy_kwds(kwargs); + if (kwcopy == NULL && _PyErr_Occurred(tstate)) { + return NULL; + } + if (_Py_EnterRecursiveCall(tstate, " while calling a Python object")) { + Py_XDECREF(kwcopy); return NULL; } - result = (*call)(callable, args, kwargs); + result = (*call)(callable, args, kwcopy); _Py_LeaveRecursiveCall(tstate); + Py_XDECREF(kwcopy); + return _Py_CheckFunctionResult(tstate, callable, result, NULL); } } diff --git a/Python/compile.c b/Python/compile.c index 10d6307a48406f..6d3406e6aa91ad 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -5094,6 +5094,15 @@ compiler_call_helper(struct compiler *c, keyword_ty kw = asdl_seq_GET(keywords, i); if (kw->arg == NULL) { /* A keyword argument unpacking. */ + if (nkwelts == 1) { + /* Only one **kwargs passed, with no individual + keyword arguments; no need to make new dict and merge + as this will be done for us if needed + */ + VISIT(c, expr, kw->value); + have_dict = 1; + break; + } if (nseen) { if (!compiler_subkwargs(c, keywords, i - nseen, i)) { return 0;