[New-bugs-announce] [issue42629] PyObject_Call not behaving as documented

Max Bachmann report at bugs.python.org
Sat Dec 12 19:21:25 EST 2020


New submission from Max Bachmann <kontakt at maxbachmann.de>:

The documentation of PyObject_Call here: https://docs.python.org/3/c-api/call.html#c.PyObject_Call
states, that it is the equivalent of the Python expression: callable(*args, **kwargs).

so I would expect:
PyObject* args = PyTuple_New(0);
PyObject* kwargs = PyDict_New();
PyObject_Call(funcObj, args, kwargs)

to behave similar to
args = []
kwargs = {}
func(*args, **kwargs)

however this is not the case since in this case when I edit kwargs inside
PyObject* func(PyObject* /*self*/, PyObject* /*args*/, PyObject* keywds)
{
  PyObject* str = PyUnicode_FromString("test_str");
  PyDict_SetItemString(keywds, "test", str);
}

it changes the original dictionary passed into PyObject_Call. I was wondering, whether this means, that:
a) it is not allowed to modify the keywds argument passed to a PyCFunctionWithKeywords
b) when calling PyObject_Call it is required to copy the kwargs for the call using PyDict_Copy

Neither the documentation of PyObject_Call nor the documentation of PyCFunctionWithKeywords (https://docs.python.org/3/c-api/structures.html#c.PyCFunctionWithKeywords) made this clear to me.

----------
components: C API
messages: 382927
nosy: maxbachmann
priority: normal
severity: normal
status: open
title: PyObject_Call not behaving as documented
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42629>
_______________________________________


More information about the New-bugs-announce mailing list