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