@@ -27,9 +27,18 @@ unit; the entry in (round) parentheses is the Python object type that matches
2727the format unit; and the entry in [square] brackets is the type of the C
2828variable(s) whose address should be passed.
2929
30+ .. _arg-parsing-string-and-buffers :
31+
3032Strings and buffers
3133-------------------
3234
35+ .. note ::
36+
37+ On Python 3.12 and older, the macro :c:macro: `!PY_SSIZE_T_CLEAN ` must be
38+ defined before including :file: `Python.h ` to use all ``# `` variants of
39+ formats (``s# ``, ``y# ``, etc.) explained below.
40+ This is not necessary on Python 3.13 and later.
41+
3342These formats allow accessing an object as a contiguous chunk of memory.
3443You don't have to provide raw storage for the returned unicode or bytes
3544area.
@@ -68,15 +77,6 @@ There are three ways strings and buffers can be converted to C:
6877 whether the input object is immutable (e.g. whether it would honor a request
6978 for a writable buffer, or whether another thread can mutate the data).
7079
71- .. note ::
72-
73- For all ``# `` variants of formats (``s# ``, ``y# ``, etc.), the macro
74- :c:macro: `PY_SSIZE_T_CLEAN ` must be defined before including
75- :file: `Python.h `. On Python 3.9 and older, the type of the length argument
76- is :c:type: `Py_ssize_t ` if the :c:macro: `PY_SSIZE_T_CLEAN ` macro is defined,
77- or int otherwise.
78-
79-
8080``s `` (:class: `str `) [const char \* ]
8181 Convert a Unicode object to a C pointer to a character string.
8282 A pointer to an existing string is stored in the character pointer
@@ -413,21 +413,35 @@ API Functions
413413 than a variable number of arguments.
414414
415415
416- .. c :function :: int PyArg_ParseTupleAndKeywords (PyObject *args, PyObject *kw, const char *format, char *keywords[] , ...)
416+ .. c :function :: int PyArg_ParseTupleAndKeywords (PyObject *args, PyObject *kw, const char *format, char * const * keywords, ...)
417417
418418 Parse the parameters of a function that takes both positional and keyword
419- parameters into local variables. The *keywords * argument is a
420- ``NULL ``-terminated array of keyword parameter names. Empty names denote
419+ parameters into local variables.
420+ The *keywords * argument is a ``NULL ``-terminated array of keyword parameter
421+ names specified as null-terminated ASCII or UTF-8 encoded C strings.
422+ Empty names denote
421423 :ref: `positional-only parameters <positional-only_parameter >`.
422424 Returns true on success; on failure, it returns false and raises the
423425 appropriate exception.
424426
427+ .. note ::
428+
429+ The *keywords * parameter declaration is :c:expr: `char * const * ` in C and
430+ :c:expr: `const char * const * ` in C++.
431+ This can be overridden with the :c:macro: `PY_CXX_CONST ` macro.
432+
425433 .. versionchanged :: 3.6
426434 Added support for :ref: `positional-only parameters
427435 <positional-only_parameter>`.
428436
437+ .. versionchanged :: 3.13
438+ The *keywords * parameter has now type :c:expr: `char * const * ` in C and
439+ :c:expr: `const char * const * ` in C++, instead of :c:expr: `char ** `.
440+ Added support for non-ASCII keyword parameter names.
441+
429442
430- .. c :function :: int PyArg_VaParseTupleAndKeywords (PyObject *args, PyObject *kw, const char *format, char *keywords[], va_list vargs)
443+
444+ .. c :function :: int PyArg_VaParseTupleAndKeywords (PyObject *args, PyObject *kw, const char *format, char * const *keywords, va_list vargs)
431445
432446 Identical to :c:func: `PyArg_ParseTupleAndKeywords `, except that it accepts a
433447 va_list rather than a variable number of arguments.
@@ -442,16 +456,24 @@ API Functions
442456 .. versionadded :: 3.2
443457
444458
445- .. XXX deprecated, will be removed
446459.. c :function :: int PyArg_Parse (PyObject *args, const char *format, ...)
447460
448- Function used to deconstruct the argument lists of "old-style" functions ---
449- these are functions which use the :const: `METH_OLDARGS ` parameter parsing
450- method, which has been removed in Python 3. This is not recommended for use
451- in parameter parsing in new code, and most code in the standard interpreter
452- has been modified to no longer use this for that purpose. It does remain a
453- convenient way to decompose other tuples, however, and may continue to be
454- used for that purpose.
461+ Parse the parameter of a function that takes a single positional parameter
462+ into a local variable. Returns true on success; on failure, it returns
463+ false and raises the appropriate exception.
464+
465+ Example::
466+
467+ // Function using METH_O calling convention
468+ static PyObject*
469+ my_function(PyObject *module, PyObject *arg)
470+ {
471+ int value;
472+ if (!PyArg_Parse(arg, "i:my_function", &value)) {
473+ return NULL;
474+ }
475+ // ... use value ...
476+ }
455477
456478
457479.. c :function :: int PyArg_UnpackTuple (PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
@@ -492,6 +514,19 @@ API Functions
492514
493515 PyArg_ParseTuple(args, "O|O:ref", &object, &callback)
494516
517+ .. c :macro :: PY_CXX_CONST
518+
519+ The value to be inserted, if any, before :c:expr: `char * const * `
520+ in the *keywords * parameter declaration of
521+ :c:func: `PyArg_ParseTupleAndKeywords ` and
522+ :c:func: `PyArg_VaParseTupleAndKeywords `.
523+ Default empty for C and ``const `` for C++
524+ (:c:expr: `const char * const * `).
525+ To override , define it to the desired value before including
526+ :file:`Python.h`.
527+
528+ .. versionadded:: 3.13
529+
495530
496531---------------
497532Building values
0 commit comments