@@ -27,9 +27,18 @@ unit; the entry in (round) parentheses is the Python object type that matches
27
27
the format unit; and the entry in [square] brackets is the type of the C
28
28
variable(s) whose address should be passed.
29
29
30
+ .. _arg-parsing-string-and-buffers :
31
+
30
32
Strings and buffers
31
33
-------------------
32
34
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
+
33
42
These formats allow accessing an object as a contiguous chunk of memory.
34
43
You don't have to provide raw storage for the returned unicode or bytes
35
44
area.
@@ -68,15 +77,6 @@ There are three ways strings and buffers can be converted to C:
68
77
whether the input object is immutable (e.g. whether it would honor a request
69
78
for a writable buffer, or whether another thread can mutate the data).
70
79
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
-
80
80
``s `` (:class: `str `) [const char \* ]
81
81
Convert a Unicode object to a C pointer to a character string.
82
82
A pointer to an existing string is stored in the character pointer
@@ -413,21 +413,35 @@ API Functions
413
413
than a variable number of arguments.
414
414
415
415
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, ...)
417
417
418
418
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
421
423
:ref: `positional-only parameters <positional-only_parameter >`.
422
424
Returns true on success; on failure, it returns false and raises the
423
425
appropriate exception.
424
426
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
+
425
433
.. versionchanged :: 3.6
426
434
Added support for :ref: `positional-only parameters
427
435
<positional-only_parameter>`.
428
436
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
+
429
442
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)
431
445
432
446
Identical to :c:func: `PyArg_ParseTupleAndKeywords `, except that it accepts a
433
447
va_list rather than a variable number of arguments.
@@ -442,16 +456,24 @@ API Functions
442
456
.. versionadded :: 3.2
443
457
444
458
445
- .. XXX deprecated, will be removed
446
459
.. c :function :: int PyArg_Parse (PyObject *args, const char *format, ...)
447
460
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
+ }
455
477
456
478
457
479
.. c :function :: int PyArg_UnpackTuple (PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
@@ -492,6 +514,19 @@ API Functions
492
514
493
515
PyArg_ParseTuple(args, "O|O:ref", &object, &callback)
494
516
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
+
495
530
496
531
---------------
497
532
Building values
0 commit comments