Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 57f91ac

Browse files
committed
Document kwnames in _PyObject_FastCallKeywords() and _PyStack_AsDict()
Issue #27213.
1 parent b8d768b commit 57f91ac

4 files changed

Lines changed: 53 additions & 30 deletions

File tree

Include/abstract.h

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
273273
PyObject **stack,
274274
Py_ssize_t nargs);
275275

276+
/* Convert keyword arguments from the (stack, kwnames) format to a Python
277+
dictionary.
278+
279+
kwnames must only contains str strings, no subclass, and all keys must
280+
be unique. kwnames is not checked, usually these checks are done before or later
281+
calling _PyStack_AsDict(). For example, _PyArg_ParseStack() raises an
282+
error if a key is not a string. */
276283
PyAPI_FUNC(PyObject *) _PyStack_AsDict(
277284
PyObject **values,
278285
PyObject *kwnames);
@@ -293,36 +300,39 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
293300
PyObject **kwnames,
294301
PyObject *func);
295302

296-
/* Call the callable object func with the "fast call" calling convention:
297-
args is a C array for positional arguments (nargs is the number of
298-
positional arguments), kwargs is a dictionary for keyword arguments.
299-
300-
If nargs is equal to zero, args can be NULL. kwargs can be NULL.
301-
nargs must be greater or equal to zero.
302-
303-
Return the result on success. Raise an exception on return NULL on
304-
error. */
305-
PyAPI_FUNC(PyObject *) _PyObject_FastCallDict(PyObject *func,
306-
PyObject **args, Py_ssize_t nargs,
307-
PyObject *kwargs);
308-
309-
/* Call the callable object func with the "fast call" calling convention:
310-
args is a C array for positional arguments followed by values of
311-
keyword arguments. Keys of keyword arguments are stored as a tuple
312-
of strings in kwnames. nargs is the number of positional parameters at
313-
the beginning of stack. The size of kwnames gives the number of keyword
314-
values in the stack after positional arguments.
315-
316-
If nargs is equal to zero and there is no keyword argument (kwnames is
317-
NULL or its size is zero), args can be NULL.
318-
319-
Return the result on success. Raise an exception and return NULL on
320-
error. */
321-
PyAPI_FUNC(PyObject *) _PyObject_FastCallKeywords
322-
(PyObject *func,
323-
PyObject **args,
324-
Py_ssize_t nargs,
325-
PyObject *kwnames);
303+
/* Call the callable object func with the "fast call" calling convention:
304+
args is a C array for positional arguments (nargs is the number of
305+
positional arguments), kwargs is a dictionary for keyword arguments.
306+
307+
If nargs is equal to zero, args can be NULL. kwargs can be NULL.
308+
nargs must be greater or equal to zero.
309+
310+
Return the result on success. Raise an exception on return NULL on
311+
error. */
312+
PyAPI_FUNC(PyObject *) _PyObject_FastCallDict(PyObject *func,
313+
PyObject **args, Py_ssize_t nargs,
314+
PyObject *kwargs);
315+
316+
/* Call the callable object func with the "fast call" calling convention:
317+
args is a C array for positional arguments followed by values of
318+
keyword arguments. Keys of keyword arguments are stored as a tuple
319+
of strings in kwnames. nargs is the number of positional parameters at
320+
the beginning of stack. The size of kwnames gives the number of keyword
321+
values in the stack after positional arguments.
322+
323+
kwnames must only contains str strings, no subclass, and all keys must
324+
be unique.
325+
326+
If nargs is equal to zero and there is no keyword argument (kwnames is
327+
NULL or its size is zero), args can be NULL.
328+
329+
Return the result on success. Raise an exception and return NULL on
330+
error. */
331+
PyAPI_FUNC(PyObject *) _PyObject_FastCallKeywords
332+
(PyObject *func,
333+
PyObject **args,
334+
Py_ssize_t nargs,
335+
PyObject *kwnames);
326336

327337
#define _PyObject_FastCall(func, args, nargs) \
328338
_PyObject_FastCallDict((func), (args), (nargs), NULL)

Objects/abstract.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,6 +2457,9 @@ _PyObject_FastCallKeywords(PyObject *func, PyObject **stack, Py_ssize_t nargs,
24572457
assert(nargs >= 0);
24582458
assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
24592459
assert((nargs == 0 && nkwargs == 0) || stack != NULL);
2460+
/* kwnames must only contains str strings, no subclass, and all keys must
2461+
be unique: these are implemented in Python/ceval.c and
2462+
_PyArg_ParseStack(). */
24602463

24612464
if (PyFunction_Check(func)) {
24622465
return _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);

Objects/methodobject.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ _PyCFunction_FastCallKeywords(PyObject *func, PyObject **stack,
276276
Py_ssize_t nkwargs;
277277

278278
assert(PyCFunction_Check(func));
279+
assert(nargs >= 0);
280+
assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
281+
assert((nargs == 0 && nkwargs == 0) || stack != NULL);
282+
/* kwnames must only contains str strings, no subclass, and all keys must
283+
be unique */
279284

280285
nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
281286
if (nkwargs > 0) {

Python/ceval.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4863,7 +4863,12 @@ fast_function(PyObject *func, PyObject **stack,
48634863
Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
48644864
Py_ssize_t nd;
48654865

4866+
assert(PyFunction_Check(func));
4867+
assert(nargs >= 0);
4868+
assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
48664869
assert((nargs == 0 && nkwargs == 0) || stack != NULL);
4870+
/* kwnames must only contains str strings, no subclass, and all keys must
4871+
be unique */
48674872

48684873
PCALL(PCALL_FUNCTION);
48694874
PCALL(PCALL_FAST_FUNCTION);

0 commit comments

Comments
 (0)