Skip to content
Closed
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
26 changes: 26 additions & 0 deletions Lib/test/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <vectorcall>`, which does not expose the dict to the callee.
23 changes: 23 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand All @@ -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},
Expand All @@ -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},
Expand Down
42 changes: 41 additions & 1 deletion Objects/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment thread
MojoVampire marked this conversation as resolved.
Resolved
assert(kwlen >= 0);
if (kwlen > 0) {
newdict = PyDict_New();
if (newdict == NULL) {
return NULL;
}
if (_PyDict_MergeEx(newdict, mapping, 2) < 0) {
Py_DECREF(newdict);
Comment thread
MojoVampire marked this conversation as resolved.
Resolved
return NULL;
}
}
}

return newdict;
}

PyObject *
_PyObject_Call(PyThreadState *tstate, PyObject *callable,
PyObject *args, PyObject *kwargs)
Expand All @@ -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,
Expand All @@ -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);
}
}
Expand Down
9 changes: 9 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down