1.. highlight:: c 2 3.. _arg-parsing: 4 5Parsing arguments and building values 6===================================== 7 8These functions are useful when creating your own extensions functions and 9methods. Additional information and examples are available in 10:ref:`extending-index`. 11 12The first three of these functions described, :c:func:`PyArg_ParseTuple`, 13:c:func:`PyArg_ParseTupleAndKeywords`, and :c:func:`PyArg_Parse`, all use *format 14strings* which are used to tell the function about the expected arguments. The 15format strings use the same syntax for each of these functions. 16 17----------------- 18Parsing arguments 19----------------- 20 21A format string consists of zero or more "format units." A format unit 22describes one Python object; it is usually a single character or a parenthesized 23sequence of format units. With a few exceptions, a format unit that is not a 24parenthesized sequence normally corresponds to a single address argument to 25these functions. In the following description, the quoted form is the format 26unit; the entry in (round) parentheses is the Python object type that matches 27the format unit; and the entry in [square] brackets is the type of the C 28variable(s) whose address should be passed. 29 30.. _arg-parsing-string-and-buffers: 31 32Strings and buffers 33------------------- 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 42These formats allow accessing an object as a contiguous chunk of memory. 43You don't have to provide raw storage for the returned unicode or bytes 44area. 45 46Unless otherwise stated, buffers are not NUL-terminated. 47 48There are three ways strings and buffers can be converted to C: 49 50* Formats such as ``y*`` and ``s*`` fill a :c:type:`Py_buffer` structure. 51 This locks the underlying buffer so that the caller can subsequently use 52 the buffer even inside a :c:type:`Py_BEGIN_ALLOW_THREADS` 53 block without the risk of mutable data being resized or destroyed. 54 As a result, **you have to call** :c:func:`PyBuffer_Release` after you have 55 finished processing the data (or in any early abort case). 56 57* The ``es``, ``es#``, ``et`` and ``et#`` formats allocate the result buffer. 58 **You have to call** :c:func:`PyMem_Free` after you have finished 59 processing the data (or in any early abort case). 60 61* .. _c-arg-borrowed-buffer: 62 63 Other formats take a :class:`str` or a read-only :term:`bytes-like object`, 64 such as :class:`bytes`, and provide a ``const char *`` pointer to 65 its buffer. 66 In this case the buffer is "borrowed": it is managed by the corresponding 67 Python object, and shares the lifetime of this object. 68 You won't have to release any memory yourself. 69 70 To ensure that the underlying buffer may be safely borrowed, the object's 71 :c:member:`PyBufferProcs.bf_releasebuffer` field must be ``NULL``. 72 This disallows common mutable objects such as :class:`bytearray`, 73 but also some read-only objects such as :class:`memoryview` of 74 :class:`bytes`. 75 76 Besides this ``bf_releasebuffer`` requirement, there is no check to verify 77 whether the input object is immutable (e.g. whether it would honor a request 78 for a writable buffer, or whether another thread can mutate the data). 79 80``s`` (:class:`str`) [const char \*] 81 Convert a Unicode object to a C pointer to a character string. 82 A pointer to an existing string is stored in the character pointer 83 variable whose address you pass. The C string is NUL-terminated. 84 The Python string must not contain embedded null code points; if it does, 85 a :exc:`ValueError` exception is raised. Unicode objects are converted 86 to C strings using ``'utf-8'`` encoding. If this conversion fails, a 87 :exc:`UnicodeError` is raised. 88 89 .. note:: 90 This format does not accept :term:`bytes-like objects 91 <bytes-like object>`. If you want to accept 92 filesystem paths and convert them to C character strings, it is 93 preferable to use the ``O&`` format with :c:func:`PyUnicode_FSConverter` 94 as *converter*. 95 96 .. versionchanged:: 3.5 97 Previously, :exc:`TypeError` was raised when embedded null code points 98 were encountered in the Python string. 99 100``s*`` (:class:`str` or :term:`bytes-like object`) [Py_buffer] 101 This format accepts Unicode objects as well as bytes-like objects. 102 It fills a :c:type:`Py_buffer` structure provided by the caller. 103 In this case the resulting C string may contain embedded NUL bytes. 104 Unicode objects are converted to C strings using ``'utf-8'`` encoding. 105 106``s#`` (:class:`str`, read-only :term:`bytes-like object`) [const char \*, :c:type:`Py_ssize_t`] 107 Like ``s*``, except that it provides a :ref:`borrowed buffer <c-arg-borrowed-buffer>`. 108 The result is stored into two C variables, 109 the first one a pointer to a C string, the second one its length. 110 The string may contain embedded null bytes. Unicode objects are converted 111 to C strings using ``'utf-8'`` encoding. 112 113``z`` (:class:`str` or ``None``) [const char \*] 114 Like ``s``, but the Python object may also be ``None``, in which case the C 115 pointer is set to ``NULL``. 116 117``z*`` (:class:`str`, :term:`bytes-like object` or ``None``) [Py_buffer] 118 Like ``s*``, but the Python object may also be ``None``, in which case the 119 ``buf`` member of the :c:type:`Py_buffer` structure is set to ``NULL``. 120 121``z#`` (:class:`str`, read-only :term:`bytes-like object` or ``None``) [const char \*, :c:type:`Py_ssize_t`] 122 Like ``s#``, but the Python object may also be ``None``, in which case the C 123 pointer is set to ``NULL``. 124 125``y`` (read-only :term:`bytes-like object`) [const char \*] 126 This format converts a bytes-like object to a C pointer to a 127 :ref:`borrowed <c-arg-borrowed-buffer>` character string; 128 it does not accept Unicode objects. The bytes buffer must not 129 contain embedded null bytes; if it does, a :exc:`ValueError` 130 exception is raised. 131 132 .. versionchanged:: 3.5 133 Previously, :exc:`TypeError` was raised when embedded null bytes were 134 encountered in the bytes buffer. 135 136``y*`` (:term:`bytes-like object`) [Py_buffer] 137 This variant on ``s*`` doesn't accept Unicode objects, only 138 bytes-like objects. **This is the recommended way to accept 139 binary data.** 140 141``y#`` (read-only :term:`bytes-like object`) [const char \*, :c:type:`Py_ssize_t`] 142 This variant on ``s#`` doesn't accept Unicode objects, only bytes-like 143 objects. 144 145``S`` (:class:`bytes`) [PyBytesObject \*] 146 Requires that the Python object is a :class:`bytes` object, without 147 attempting any conversion. Raises :exc:`TypeError` if the object is not 148 a bytes object. The C variable may also be declared as :c:expr:`PyObject*`. 149 150``Y`` (:class:`bytearray`) [PyByteArrayObject \*] 151 Requires that the Python object is a :class:`bytearray` object, without 152 attempting any conversion. Raises :exc:`TypeError` if the object is not 153 a :class:`bytearray` object. The C variable may also be declared as :c:expr:`PyObject*`. 154 155``U`` (:class:`str`) [PyObject \*] 156 Requires that the Python object is a Unicode object, without attempting 157 any conversion. Raises :exc:`TypeError` if the object is not a Unicode 158 object. The C variable may also be declared as :c:expr:`PyObject*`. 159 160``w*`` (read-write :term:`bytes-like object`) [Py_buffer] 161 This format accepts any object which implements the read-write buffer 162 interface. It fills a :c:type:`Py_buffer` structure provided by the caller. 163 The buffer may contain embedded null bytes. The caller have to call 164 :c:func:`PyBuffer_Release` when it is done with the buffer. 165 166``es`` (:class:`str`) [const char \*encoding, char \*\*buffer] 167 This variant on ``s`` is used for encoding Unicode into a character buffer. 168 It only works for encoded data without embedded NUL bytes. 169 170 This format requires two arguments. The first is only used as input, and 171 must be a :c:expr:`const char*` which points to the name of an encoding as a 172 NUL-terminated string, or ``NULL``, in which case ``'utf-8'`` encoding is used. 173 An exception is raised if the named encoding is not known to Python. The 174 second argument must be a :c:expr:`char**`; the value of the pointer it 175 references will be set to a buffer with the contents of the argument text. 176 The text will be encoded in the encoding specified by the first argument. 177 178 :c:func:`PyArg_ParseTuple` will allocate a buffer of the needed size, copy the 179 encoded data into this buffer and adjust *\*buffer* to reference the newly 180 allocated storage. The caller is responsible for calling :c:func:`PyMem_Free` to 181 free the allocated buffer after use. 182 183``et`` (:class:`str`, :class:`bytes` or :class:`bytearray`) [const char \*encoding, char \*\*buffer] 184 Same as ``es`` except that byte string objects are passed through without 185 recoding them. Instead, the implementation assumes that the byte string object uses 186 the encoding passed in as parameter. 187 188``es#`` (:class:`str`) [const char \*encoding, char \*\*buffer, :c:type:`Py_ssize_t` \*buffer_length] 189 This variant on ``s#`` is used for encoding Unicode into a character buffer. 190 Unlike the ``es`` format, this variant allows input data which contains NUL 191 characters. 192 193 It requires three arguments. The first is only used as input, and must be a 194 :c:expr:`const char*` which points to the name of an encoding as a 195 NUL-terminated string, or ``NULL``, in which case ``'utf-8'`` encoding is used. 196 An exception is raised if the named encoding is not known to Python. The 197 second argument must be a :c:expr:`char**`; the value of the pointer it 198 references will be set to a buffer with the contents of the argument text. 199 The text will be encoded in the encoding specified by the first argument. 200 The third argument must be a pointer to an integer; the referenced integer 201 will be set to the number of bytes in the output buffer. 202 203 There are two modes of operation: 204 205 If *\*buffer* points a ``NULL`` pointer, the function will allocate a buffer of 206 the needed size, copy the encoded data into this buffer and set *\*buffer* to 207 reference the newly allocated storage. The caller is responsible for calling 208 :c:func:`PyMem_Free` to free the allocated buffer after usage. 209 210 If *\*buffer* points to a non-``NULL`` pointer (an already allocated buffer), 211 :c:func:`PyArg_ParseTuple` will use this location as the buffer and interpret the 212 initial value of *\*buffer_length* as the buffer size. It will then copy the 213 encoded data into the buffer and NUL-terminate it. If the buffer is not large 214 enough, a :exc:`ValueError` will be set. 215 216 In both cases, *\*buffer_length* is set to the length of the encoded data 217 without the trailing NUL byte. 218 219``et#`` (:class:`str`, :class:`bytes` or :class:`bytearray`) [const char \*encoding, char \*\*buffer, :c:type:`Py_ssize_t` \*buffer_length] 220 Same as ``es#`` except that byte string objects are passed through without recoding 221 them. Instead, the implementation assumes that the byte string object uses the 222 encoding passed in as parameter. 223 224.. versionchanged:: 3.12 225 ``u``, ``u#``, ``Z``, and ``Z#`` are removed because they used a legacy 226 ``Py_UNICODE*`` representation. 227 228 229Numbers 230------- 231 232``b`` (:class:`int`) [unsigned char] 233 Convert a nonnegative Python integer to an unsigned tiny int, stored in a C 234 :c:expr:`unsigned char`. 235 236``B`` (:class:`int`) [unsigned char] 237 Convert a Python integer to a tiny int without overflow checking, stored in a C 238 :c:expr:`unsigned char`. 239 240``h`` (:class:`int`) [short int] 241 Convert a Python integer to a C :c:expr:`short int`. 242 243``H`` (:class:`int`) [unsigned short int] 244 Convert a Python integer to a C :c:expr:`unsigned short int`, without overflow 245 checking. 246 247``i`` (:class:`int`) [int] 248 Convert a Python integer to a plain C :c:expr:`int`. 249 250``I`` (:class:`int`) [unsigned int] 251 Convert a Python integer to a C :c:expr:`unsigned int`, without overflow 252 checking. 253 254``l`` (:class:`int`) [long int] 255 Convert a Python integer to a C :c:expr:`long int`. 256 257``k`` (:class:`int`) [unsigned long] 258 Convert a Python integer to a C :c:expr:`unsigned long` without 259 overflow checking. 260 261``L`` (:class:`int`) [long long] 262 Convert a Python integer to a C :c:expr:`long long`. 263 264``K`` (:class:`int`) [unsigned long long] 265 Convert a Python integer to a C :c:expr:`unsigned long long` 266 without overflow checking. 267 268``n`` (:class:`int`) [:c:type:`Py_ssize_t`] 269 Convert a Python integer to a C :c:type:`Py_ssize_t`. 270 271``c`` (:class:`bytes` or :class:`bytearray` of length 1) [char] 272 Convert a Python byte, represented as a :class:`bytes` or 273 :class:`bytearray` object of length 1, to a C :c:expr:`char`. 274 275 .. versionchanged:: 3.3 276 Allow :class:`bytearray` objects. 277 278``C`` (:class:`str` of length 1) [int] 279 Convert a Python character, represented as a :class:`str` object of 280 length 1, to a C :c:expr:`int`. 281 282``f`` (:class:`float`) [float] 283 Convert a Python floating-point number to a C :c:expr:`float`. 284 285``d`` (:class:`float`) [double] 286 Convert a Python floating-point number to a C :c:expr:`double`. 287 288``D`` (:class:`complex`) [Py_complex] 289 Convert a Python complex number to a C :c:type:`Py_complex` structure. 290 291Other objects 292------------- 293 294``O`` (object) [PyObject \*] 295 Store a Python object (without any conversion) in a C object pointer. The C 296 program thus receives the actual object that was passed. A new 297 :term:`strong reference` to the object is not created 298 (i.e. its reference count is not increased). 299 The pointer stored is not ``NULL``. 300 301``O!`` (object) [*typeobject*, PyObject \*] 302 Store a Python object in a C object pointer. This is similar to ``O``, but 303 takes two C arguments: the first is the address of a Python type object, the 304 second is the address of the C variable (of type :c:expr:`PyObject*`) into which 305 the object pointer is stored. If the Python object does not have the required 306 type, :exc:`TypeError` is raised. 307 308.. _o_ampersand: 309 310``O&`` (object) [*converter*, *anything*] 311 Convert a Python object to a C variable through a *converter* function. This 312 takes two arguments: the first is a function, the second is the address of a C 313 variable (of arbitrary type), converted to :c:expr:`void *`. The *converter* 314 function in turn is called as follows:: 315 316 status = converter(object, address); 317 318 where *object* is the Python object to be converted and *address* is the 319 :c:expr:`void*` argument that was passed to the ``PyArg_Parse*`` function. 320 The returned *status* should be ``1`` for a successful conversion and ``0`` if 321 the conversion has failed. When the conversion fails, the *converter* function 322 should raise an exception and leave the content of *address* unmodified. 323 324 If the *converter* returns ``Py_CLEANUP_SUPPORTED``, it may get called a 325 second time if the argument parsing eventually fails, giving the converter a 326 chance to release any memory that it had already allocated. In this second 327 call, the *object* parameter will be ``NULL``; *address* will have the same value 328 as in the original call. 329 330 .. versionchanged:: 3.1 331 ``Py_CLEANUP_SUPPORTED`` was added. 332 333``p`` (:class:`bool`) [int] 334 Tests the value passed in for truth (a boolean **p**\ redicate) and converts 335 the result to its equivalent C true/false integer value. 336 Sets the int to ``1`` if the expression was true and ``0`` if it was false. 337 This accepts any valid Python value. See :ref:`truth` for more 338 information about how Python tests values for truth. 339 340 .. versionadded:: 3.3 341 342``(items)`` (:class:`tuple`) [*matching-items*] 343 The object must be a Python sequence whose length is the number of format units 344 in *items*. The C arguments must correspond to the individual format units in 345 *items*. Format units for sequences may be nested. 346 347It is possible to pass "long" integers (integers whose value exceeds the 348platform's :c:macro:`LONG_MAX`) however no proper range checking is done --- the 349most significant bits are silently truncated when the receiving field is too 350small to receive the value (actually, the semantics are inherited from downcasts 351in C --- your mileage may vary). 352 353A few other characters have a meaning in a format string. These may not occur 354inside nested parentheses. They are: 355 356``|`` 357 Indicates that the remaining arguments in the Python argument list are optional. 358 The C variables corresponding to optional arguments should be initialized to 359 their default value --- when an optional argument is not specified, 360 :c:func:`PyArg_ParseTuple` does not touch the contents of the corresponding C 361 variable(s). 362 363``$`` 364 :c:func:`PyArg_ParseTupleAndKeywords` only: 365 Indicates that the remaining arguments in the Python argument list are 366 keyword-only. Currently, all keyword-only arguments must also be optional 367 arguments, so ``|`` must always be specified before ``$`` in the format 368 string. 369 370 .. versionadded:: 3.3 371 372``:`` 373 The list of format units ends here; the string after the colon is used as the 374 function name in error messages (the "associated value" of the exception that 375 :c:func:`PyArg_ParseTuple` raises). 376 377``;`` 378 The list of format units ends here; the string after the semicolon is used as 379 the error message *instead* of the default error message. ``:`` and ``;`` 380 mutually exclude each other. 381 382Note that any Python object references which are provided to the caller are 383*borrowed* references; do not release them 384(i.e. do not decrement their reference count)! 385 386Additional arguments passed to these functions must be addresses of variables 387whose type is determined by the format string; these are used to store values 388from the input tuple. There are a few cases, as described in the list of format 389units above, where these parameters are used as input values; they should match 390what is specified for the corresponding format unit in that case. 391 392For the conversion to succeed, the *arg* object must match the format 393and the format must be exhausted. On success, the 394``PyArg_Parse*`` functions return true, otherwise they return 395false and raise an appropriate exception. When the 396``PyArg_Parse*`` functions fail due to conversion failure in one 397of the format units, the variables at the addresses corresponding to that 398and the following format units are left untouched. 399 400API Functions 401------------- 402 403.. c:function:: int PyArg_ParseTuple(PyObject *args, const char *format, ...) 404 405 Parse the parameters of a function that takes only positional parameters into 406 local variables. Returns true on success; on failure, it returns false and 407 raises the appropriate exception. 408 409 410.. c:function:: int PyArg_VaParse(PyObject *args, const char *format, va_list vargs) 411 412 Identical to :c:func:`PyArg_ParseTuple`, except that it accepts a va_list rather 413 than a variable number of arguments. 414 415 416.. c:function:: int PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char * const *keywords, ...) 417 418 Parse the parameters of a function that takes both positional and keyword 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 423 :ref:`positional-only parameters <positional-only_parameter>`. 424 Returns true on success; on failure, it returns false and raises the 425 appropriate exception. 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 433 .. versionchanged:: 3.6 434 Added support for :ref:`positional-only parameters 435 <positional-only_parameter>`. 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 442 443 444.. c:function:: int PyArg_VaParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char * const *keywords, va_list vargs) 445 446 Identical to :c:func:`PyArg_ParseTupleAndKeywords`, except that it accepts a 447 va_list rather than a variable number of arguments. 448 449 450.. c:function:: int PyArg_ValidateKeywordArguments(PyObject *) 451 452 Ensure that the keys in the keywords argument dictionary are strings. This 453 is only needed if :c:func:`PyArg_ParseTupleAndKeywords` is not used, since the 454 latter already does this check. 455 456 .. versionadded:: 3.2 457 458 459.. c:function:: int PyArg_Parse(PyObject *args, const char *format, ...) 460 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 } 477 478 479.. c:function:: int PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...) 480 481 A simpler form of parameter retrieval which does not use a format string to 482 specify the types of the arguments. Functions which use this method to retrieve 483 their parameters should be declared as :c:macro:`METH_VARARGS` in function or 484 method tables. The tuple containing the actual parameters should be passed as 485 *args*; it must actually be a tuple. The length of the tuple must be at least 486 *min* and no more than *max*; *min* and *max* may be equal. Additional 487 arguments must be passed to the function, each of which should be a pointer to a 488 :c:expr:`PyObject*` variable; these will be filled in with the values from 489 *args*; they will contain :term:`borrowed references <borrowed reference>`. 490 The variables which correspond 491 to optional parameters not given by *args* will not be filled in; these should 492 be initialized by the caller. This function returns true on success and false if 493 *args* is not a tuple or contains the wrong number of elements; an exception 494 will be set if there was a failure. 495 496 This is an example of the use of this function, taken from the sources for the 497 :mod:`!_weakref` helper module for weak references:: 498 499 static PyObject * 500 weakref_ref(PyObject *self, PyObject *args) 501 { 502 PyObject *object; 503 PyObject *callback = NULL; 504 PyObject *result = NULL; 505 506 if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) { 507 result = PyWeakref_NewRef(object, callback); 508 } 509 return result; 510 } 511 512 The call to :c:func:`PyArg_UnpackTuple` in this example is entirely equivalent to 513 this call to :c:func:`PyArg_ParseTuple`:: 514 515 PyArg_ParseTuple(args, "O|O:ref", &object, &callback) 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 530 531--------------- 532Building values 533--------------- 534 535.. c:function:: PyObject* Py_BuildValue(const char *format, ...) 536 537 Create a new value based on a format string similar to those accepted by the 538 ``PyArg_Parse*`` family of functions and a sequence of values. Returns 539 the value or ``NULL`` in the case of an error; an exception will be raised if 540 ``NULL`` is returned. 541 542 :c:func:`Py_BuildValue` does not always build a tuple. It builds a tuple only if 543 its format string contains two or more format units. If the format string is 544 empty, it returns ``None``; if it contains exactly one format unit, it returns 545 whatever object is described by that format unit. To force it to return a tuple 546 of size 0 or one, parenthesize the format string. 547 548 When memory buffers are passed as parameters to supply data to build objects, as 549 for the ``s`` and ``s#`` formats, the required data is copied. Buffers provided 550 by the caller are never referenced by the objects created by 551 :c:func:`Py_BuildValue`. In other words, if your code invokes :c:func:`malloc` 552 and passes the allocated memory to :c:func:`Py_BuildValue`, your code is 553 responsible for calling :c:func:`free` for that memory once 554 :c:func:`Py_BuildValue` returns. 555 556 In the following description, the quoted form is the format unit; the entry in 557 (round) parentheses is the Python object type that the format unit will return; 558 and the entry in [square] brackets is the type of the C value(s) to be passed. 559 560 The characters space, tab, colon and comma are ignored in format strings (but 561 not within format units such as ``s#``). This can be used to make long format 562 strings a tad more readable. 563 564 ``s`` (:class:`str` or ``None``) [const char \*] 565 Convert a null-terminated C string to a Python :class:`str` object using ``'utf-8'`` 566 encoding. If the C string pointer is ``NULL``, ``None`` is used. 567 568 ``s#`` (:class:`str` or ``None``) [const char \*, :c:type:`Py_ssize_t`] 569 Convert a C string and its length to a Python :class:`str` object using ``'utf-8'`` 570 encoding. If the C string pointer is ``NULL``, the length is ignored and 571 ``None`` is returned. 572 573 ``y`` (:class:`bytes`) [const char \*] 574 This converts a C string to a Python :class:`bytes` object. If the C 575 string pointer is ``NULL``, ``None`` is returned. 576 577 ``y#`` (:class:`bytes`) [const char \*, :c:type:`Py_ssize_t`] 578 This converts a C string and its lengths to a Python object. If the C 579 string pointer is ``NULL``, ``None`` is returned. 580 581 ``z`` (:class:`str` or ``None``) [const char \*] 582 Same as ``s``. 583 584 ``z#`` (:class:`str` or ``None``) [const char \*, :c:type:`Py_ssize_t`] 585 Same as ``s#``. 586 587 ``u`` (:class:`str`) [const wchar_t \*] 588 Convert a null-terminated :c:type:`wchar_t` buffer of Unicode (UTF-16 or UCS-4) 589 data to a Python Unicode object. If the Unicode buffer pointer is ``NULL``, 590 ``None`` is returned. 591 592 ``u#`` (:class:`str`) [const wchar_t \*, :c:type:`Py_ssize_t`] 593 Convert a Unicode (UTF-16 or UCS-4) data buffer and its length to a Python 594 Unicode object. If the Unicode buffer pointer is ``NULL``, the length is ignored 595 and ``None`` is returned. 596 597 ``U`` (:class:`str` or ``None``) [const char \*] 598 Same as ``s``. 599 600 ``U#`` (:class:`str` or ``None``) [const char \*, :c:type:`Py_ssize_t`] 601 Same as ``s#``. 602 603 ``i`` (:class:`int`) [int] 604 Convert a plain C :c:expr:`int` to a Python integer object. 605 606 ``b`` (:class:`int`) [char] 607 Convert a plain C :c:expr:`char` to a Python integer object. 608 609 ``h`` (:class:`int`) [short int] 610 Convert a plain C :c:expr:`short int` to a Python integer object. 611 612 ``l`` (:class:`int`) [long int] 613 Convert a C :c:expr:`long int` to a Python integer object. 614 615 ``B`` (:class:`int`) [unsigned char] 616 Convert a C :c:expr:`unsigned char` to a Python integer object. 617 618 ``H`` (:class:`int`) [unsigned short int] 619 Convert a C :c:expr:`unsigned short int` to a Python integer object. 620 621 ``I`` (:class:`int`) [unsigned int] 622 Convert a C :c:expr:`unsigned int` to a Python integer object. 623 624 ``k`` (:class:`int`) [unsigned long] 625 Convert a C :c:expr:`unsigned long` to a Python integer object. 626 627 ``L`` (:class:`int`) [long long] 628 Convert a C :c:expr:`long long` to a Python integer object. 629 630 ``K`` (:class:`int`) [unsigned long long] 631 Convert a C :c:expr:`unsigned long long` to a Python integer object. 632 633 ``n`` (:class:`int`) [:c:type:`Py_ssize_t`] 634 Convert a C :c:type:`Py_ssize_t` to a Python integer. 635 636 ``c`` (:class:`bytes` of length 1) [char] 637 Convert a C :c:expr:`int` representing a byte to a Python :class:`bytes` object of 638 length 1. 639 640 ``C`` (:class:`str` of length 1) [int] 641 Convert a C :c:expr:`int` representing a character to Python :class:`str` 642 object of length 1. 643 644 ``d`` (:class:`float`) [double] 645 Convert a C :c:expr:`double` to a Python floating-point number. 646 647 ``f`` (:class:`float`) [float] 648 Convert a C :c:expr:`float` to a Python floating-point number. 649 650 ``D`` (:class:`complex`) [Py_complex \*] 651 Convert a C :c:type:`Py_complex` structure to a Python complex number. 652 653 ``O`` (object) [PyObject \*] 654 Pass a Python object untouched but create a new 655 :term:`strong reference` to it 656 (i.e. its reference count is incremented by one). 657 If the object passed in is a ``NULL`` pointer, it is assumed 658 that this was caused because the call producing the argument found an error and 659 set an exception. Therefore, :c:func:`Py_BuildValue` will return ``NULL`` but won't 660 raise an exception. If no exception has been raised yet, :exc:`SystemError` is 661 set. 662 663 ``S`` (object) [PyObject \*] 664 Same as ``O``. 665 666 ``N`` (object) [PyObject \*] 667 Same as ``O``, except it doesn't create a new :term:`strong reference`. 668 Useful when the object is created by a call to an object constructor in the 669 argument list. 670 671 ``O&`` (object) [*converter*, *anything*] 672 Convert *anything* to a Python object through a *converter* function. The 673 function is called with *anything* (which should be compatible with :c:expr:`void*`) 674 as its argument and should return a "new" Python object, or ``NULL`` if an 675 error occurred. 676 677 ``(items)`` (:class:`tuple`) [*matching-items*] 678 Convert a sequence of C values to a Python tuple with the same number of items. 679 680 ``[items]`` (:class:`list`) [*matching-items*] 681 Convert a sequence of C values to a Python list with the same number of items. 682 683 ``{items}`` (:class:`dict`) [*matching-items*] 684 Convert a sequence of C values to a Python dictionary. Each pair of consecutive 685 C values adds one item to the dictionary, serving as key and value, 686 respectively. 687 688 If there is an error in the format string, the :exc:`SystemError` exception is 689 set and ``NULL`` returned. 690 691.. c:function:: PyObject* Py_VaBuildValue(const char *format, va_list vargs) 692 693 Identical to :c:func:`Py_BuildValue`, except that it accepts a va_list 694 rather than a variable number of arguments. 695