Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _arg-parsing: |
| 4 | |
| 5 | Parsing arguments and building values |
| 6 | ===================================== |
| 7 | |
| 8 | These functions are useful when creating your own extensions functions and |
| 9 | methods. Additional information and examples are available in |
| 10 | :ref:`extending-index`. |
| 11 | |
| 12 | The first three of these functions described, :cfunc:`PyArg_ParseTuple`, |
| 13 | :cfunc:`PyArg_ParseTupleAndKeywords`, and :cfunc:`PyArg_Parse`, all use *format |
| 14 | strings* which are used to tell the function about the expected arguments. The |
| 15 | format strings use the same syntax for each of these functions. |
| 16 | |
| 17 | A format string consists of zero or more "format units." A format unit |
| 18 | describes one Python object; it is usually a single character or a parenthesized |
| 19 | sequence of format units. With a few exceptions, a format unit that is not a |
| 20 | parenthesized sequence normally corresponds to a single address argument to |
| 21 | these functions. In the following description, the quoted form is the format |
| 22 | unit; the entry in (round) parentheses is the Python object type that matches |
| 23 | the format unit; and the entry in [square] brackets is the type of the C |
| 24 | variable(s) whose address should be passed. |
| 25 | |
| 26 | ``s`` (string or Unicode object) [const char \*] |
| 27 | Convert a Python string or Unicode object to a C pointer to a character string. |
| 28 | You must not provide storage for the string itself; a pointer to an existing |
| 29 | string is stored into the character pointer variable whose address you pass. |
| 30 | The C string is NUL-terminated. The Python string must not contain embedded NUL |
| 31 | bytes; if it does, a :exc:`TypeError` exception is raised. Unicode objects are |
| 32 | converted to C strings using the default encoding. If this conversion fails, a |
| 33 | :exc:`UnicodeError` is raised. |
| 34 | |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 35 | ``s*`` (string, Unicode, or any buffer compatible object) [Py_buffer \*] |
Georg Brandl | 8fa8952 | 2008-09-01 16:45:35 +0000 | [diff] [blame] | 36 | This is similar to ``s``, but the code fills a :ctype:`Py_buffer` structure |
| 37 | provided by the caller. In this case the Python string may contain embedded |
| 38 | null bytes. Unicode objects pass back a pointer to the default encoded |
| 39 | string version of the object if such a conversion is possible. The |
| 40 | underlying buffer is locked, so that the caller can subsequently use the |
| 41 | buffer even inside a ``Py_BEGIN_ALLOW_THREADS`` block. **The caller is |
| 42 | responsible** for calling ``PyBuffer_Release`` with the structure after it |
| 43 | has processed the data. |
| 44 | |
| 45 | ``s#`` (string, Unicode or any read buffer compatible object) [const char \*, int] |
| 46 | This variant on ``s*`` stores into two C variables, the first one a pointer |
| 47 | to a character string, the second one its length. All other read-buffer |
| 48 | compatible objects pass back a reference to the raw internal data |
| 49 | representation. Since this format doesn't allow writable buffer compatible |
| 50 | objects like byte arrays, ``s*`` is to be preferred. |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 51 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 52 | ``y`` (bytes object) [const char \*] |
Georg Brandl | 8fa8952 | 2008-09-01 16:45:35 +0000 | [diff] [blame] | 53 | This variant on ``s`` converts a Python bytes or bytearray object to a C |
| 54 | pointer to a character string. The bytes object must not contain embedded |
| 55 | NUL bytes; if it does, a :exc:`TypeError` exception is raised. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 56 | |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 57 | ``y*`` (bytes object) [Py_buffer \*] |
| 58 | This is to ``s*`` as ``y`` is to ``s``. |
| 59 | |
Georg Brandl | 8fa8952 | 2008-09-01 16:45:35 +0000 | [diff] [blame] | 60 | ``y#`` (bytes object) [const char \*, int] |
| 61 | This variant on ``s#`` stores into two C variables, the first one a pointer |
| 62 | to a character string, the second one its length. This only accepts bytes |
| 63 | objects, no byte arrays. |
| 64 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 65 | ``z`` (string or ``None``) [const char \*] |
| 66 | Like ``s``, but the Python object may also be ``None``, in which case the C |
| 67 | pointer is set to *NULL*. |
| 68 | |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 69 | ``z*`` (string or ``None`` or any buffer compatible object) [Py_buffer*] |
| 70 | This is to ``s*`` as ``z`` is to ``s``. |
| 71 | |
Georg Brandl | 8fa8952 | 2008-09-01 16:45:35 +0000 | [diff] [blame] | 72 | ``z#`` (string or ``None`` or any read buffer compatible object) [const char \*, int] |
| 73 | This is to ``s#`` as ``z`` is to ``s``. |
| 74 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 75 | ``u`` (Unicode object) [Py_UNICODE \*] |
| 76 | Convert a Python Unicode object to a C pointer to a NUL-terminated buffer of |
| 77 | 16-bit Unicode (UTF-16) data. As with ``s``, there is no need to provide |
| 78 | storage for the Unicode data buffer; a pointer to the existing Unicode data is |
| 79 | stored into the :ctype:`Py_UNICODE` pointer variable whose address you pass. |
| 80 | |
| 81 | ``u#`` (Unicode object) [Py_UNICODE \*, int] |
| 82 | This variant on ``u`` stores into two C variables, the first one a pointer to a |
| 83 | Unicode data buffer, the second one its length. Non-Unicode objects are handled |
| 84 | by interpreting their read-buffer pointer as pointer to a :ctype:`Py_UNICODE` |
| 85 | array. |
| 86 | |
| 87 | ``Z`` (Unicode or ``None``) [Py_UNICODE \*] |
| 88 | Like ``s``, but the Python object may also be ``None``, in which case the C |
| 89 | pointer is set to *NULL*. |
| 90 | |
| 91 | ``Z#`` (Unicode or ``None``) [Py_UNICODE \*, int] |
| 92 | This is to ``u#`` as ``Z`` is to ``u``. |
| 93 | |
| 94 | ``es`` (string, Unicode object or character buffer compatible object) [const char \*encoding, char \*\*buffer] |
| 95 | This variant on ``s`` is used for encoding Unicode and objects convertible to |
| 96 | Unicode into a character buffer. It only works for encoded data without embedded |
| 97 | NUL bytes. |
| 98 | |
| 99 | This format requires two arguments. The first is only used as input, and |
| 100 | must be a :ctype:`const char\*` which points to the name of an encoding as a |
| 101 | NUL-terminated string, or *NULL*, in which case the default encoding is used. |
| 102 | An exception is raised if the named encoding is not known to Python. The |
| 103 | second argument must be a :ctype:`char\*\*`; the value of the pointer it |
| 104 | references will be set to a buffer with the contents of the argument text. |
| 105 | The text will be encoded in the encoding specified by the first argument. |
| 106 | |
| 107 | :cfunc:`PyArg_ParseTuple` will allocate a buffer of the needed size, copy the |
| 108 | encoded data into this buffer and adjust *\*buffer* to reference the newly |
| 109 | allocated storage. The caller is responsible for calling :cfunc:`PyMem_Free` to |
| 110 | free the allocated buffer after use. |
| 111 | |
| 112 | ``et`` (string, Unicode object or character buffer compatible object) [const char \*encoding, char \*\*buffer] |
| 113 | Same as ``es`` except that 8-bit string objects are passed through without |
| 114 | recoding them. Instead, the implementation assumes that the string object uses |
| 115 | the encoding passed in as parameter. |
| 116 | |
| 117 | ``es#`` (string, Unicode object or character buffer compatible object) [const char \*encoding, char \*\*buffer, int \*buffer_length] |
| 118 | This variant on ``s#`` is used for encoding Unicode and objects convertible to |
| 119 | Unicode into a character buffer. Unlike the ``es`` format, this variant allows |
| 120 | input data which contains NUL characters. |
| 121 | |
| 122 | It requires three arguments. The first is only used as input, and must be a |
| 123 | :ctype:`const char\*` which points to the name of an encoding as a |
| 124 | NUL-terminated string, or *NULL*, in which case the default encoding is used. |
| 125 | An exception is raised if the named encoding is not known to Python. The |
| 126 | second argument must be a :ctype:`char\*\*`; the value of the pointer it |
| 127 | references will be set to a buffer with the contents of the argument text. |
| 128 | The text will be encoded in the encoding specified by the first argument. |
| 129 | The third argument must be a pointer to an integer; the referenced integer |
| 130 | will be set to the number of bytes in the output buffer. |
| 131 | |
| 132 | There are two modes of operation: |
| 133 | |
| 134 | If *\*buffer* points a *NULL* pointer, the function will allocate a buffer of |
| 135 | the needed size, copy the encoded data into this buffer and set *\*buffer* to |
| 136 | reference the newly allocated storage. The caller is responsible for calling |
| 137 | :cfunc:`PyMem_Free` to free the allocated buffer after usage. |
| 138 | |
| 139 | If *\*buffer* points to a non-*NULL* pointer (an already allocated buffer), |
| 140 | :cfunc:`PyArg_ParseTuple` will use this location as the buffer and interpret the |
| 141 | initial value of *\*buffer_length* as the buffer size. It will then copy the |
| 142 | encoded data into the buffer and NUL-terminate it. If the buffer is not large |
| 143 | enough, a :exc:`ValueError` will be set. |
| 144 | |
| 145 | In both cases, *\*buffer_length* is set to the length of the encoded data |
| 146 | without the trailing NUL byte. |
| 147 | |
| 148 | ``et#`` (string, Unicode object or character buffer compatible object) [const char \*encoding, char \*\*buffer] |
| 149 | Same as ``es#`` except that string objects are passed through without recoding |
| 150 | them. Instead, the implementation assumes that the string object uses the |
| 151 | encoding passed in as parameter. |
| 152 | |
| 153 | ``b`` (integer) [char] |
| 154 | Convert a Python integer to a tiny int, stored in a C :ctype:`char`. |
| 155 | |
| 156 | ``B`` (integer) [unsigned char] |
| 157 | Convert a Python integer to a tiny int without overflow checking, stored in a C |
| 158 | :ctype:`unsigned char`. |
| 159 | |
| 160 | ``h`` (integer) [short int] |
| 161 | Convert a Python integer to a C :ctype:`short int`. |
| 162 | |
| 163 | ``H`` (integer) [unsigned short int] |
| 164 | Convert a Python integer to a C :ctype:`unsigned short int`, without overflow |
| 165 | checking. |
| 166 | |
| 167 | ``i`` (integer) [int] |
| 168 | Convert a Python integer to a plain C :ctype:`int`. |
| 169 | |
| 170 | ``I`` (integer) [unsigned int] |
| 171 | Convert a Python integer to a C :ctype:`unsigned int`, without overflow |
| 172 | checking. |
| 173 | |
| 174 | ``l`` (integer) [long int] |
| 175 | Convert a Python integer to a C :ctype:`long int`. |
| 176 | |
| 177 | ``k`` (integer) [unsigned long] |
| 178 | Convert a Python integer to a C :ctype:`unsigned long` without |
| 179 | overflow checking. |
| 180 | |
| 181 | ``L`` (integer) [PY_LONG_LONG] |
| 182 | Convert a Python integer to a C :ctype:`long long`. This format is only |
| 183 | available on platforms that support :ctype:`long long` (or :ctype:`_int64` on |
| 184 | Windows). |
| 185 | |
| 186 | ``K`` (integer) [unsigned PY_LONG_LONG] |
| 187 | Convert a Python integer to a C :ctype:`unsigned long long` |
| 188 | without overflow checking. This format is only available on platforms that |
| 189 | support :ctype:`unsigned long long` (or :ctype:`unsigned _int64` on Windows). |
| 190 | |
| 191 | ``n`` (integer) [Py_ssize_t] |
| 192 | Convert a Python integer to a C :ctype:`Py_ssize_t`. |
| 193 | |
| 194 | ``c`` (string of length 1) [char] |
| 195 | Convert a Python character, represented as a string of length 1, to a C |
| 196 | :ctype:`char`. |
| 197 | |
| 198 | ``f`` (float) [float] |
| 199 | Convert a Python floating point number to a C :ctype:`float`. |
| 200 | |
| 201 | ``d`` (float) [double] |
| 202 | Convert a Python floating point number to a C :ctype:`double`. |
| 203 | |
| 204 | ``D`` (complex) [Py_complex] |
| 205 | Convert a Python complex number to a C :ctype:`Py_complex` structure. |
| 206 | |
| 207 | ``O`` (object) [PyObject \*] |
| 208 | Store a Python object (without any conversion) in a C object pointer. The C |
| 209 | program thus receives the actual object that was passed. The object's reference |
| 210 | count is not increased. The pointer stored is not *NULL*. |
| 211 | |
| 212 | ``O!`` (object) [*typeobject*, PyObject \*] |
| 213 | Store a Python object in a C object pointer. This is similar to ``O``, but |
| 214 | takes two C arguments: the first is the address of a Python type object, the |
| 215 | second is the address of the C variable (of type :ctype:`PyObject\*`) into which |
| 216 | the object pointer is stored. If the Python object does not have the required |
| 217 | type, :exc:`TypeError` is raised. |
| 218 | |
| 219 | ``O&`` (object) [*converter*, *anything*] |
| 220 | Convert a Python object to a C variable through a *converter* function. This |
| 221 | takes two arguments: the first is a function, the second is the address of a C |
| 222 | variable (of arbitrary type), converted to :ctype:`void \*`. The *converter* |
| 223 | function in turn is called as follows:: |
| 224 | |
| 225 | status = converter(object, address); |
| 226 | |
| 227 | where *object* is the Python object to be converted and *address* is the |
| 228 | :ctype:`void\*` argument that was passed to the :cfunc:`PyArg_Parse\*` function. |
| 229 | The returned *status* should be ``1`` for a successful conversion and ``0`` if |
| 230 | the conversion has failed. When the conversion fails, the *converter* function |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 231 | should raise an exception and leave the content of *address* unmodified. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 232 | |
| 233 | ``S`` (string) [PyStringObject \*] |
| 234 | Like ``O`` but requires that the Python object is a string object. Raises |
| 235 | :exc:`TypeError` if the object is not a string object. The C variable may also |
| 236 | be declared as :ctype:`PyObject\*`. |
| 237 | |
| 238 | ``U`` (Unicode string) [PyUnicodeObject \*] |
| 239 | Like ``O`` but requires that the Python object is a Unicode object. Raises |
| 240 | :exc:`TypeError` if the object is not a Unicode object. The C variable may also |
| 241 | be declared as :ctype:`PyObject\*`. |
| 242 | |
| 243 | ``t#`` (read-only character buffer) [char \*, int] |
| 244 | Like ``s#``, but accepts any object which implements the read-only buffer |
| 245 | interface. The :ctype:`char\*` variable is set to point to the first byte of |
| 246 | the buffer, and the :ctype:`int` is set to the length of the buffer. Only |
| 247 | single-segment buffer objects are accepted; :exc:`TypeError` is raised for all |
| 248 | others. |
| 249 | |
| 250 | ``w`` (read-write character buffer) [char \*] |
| 251 | Similar to ``s``, but accepts any object which implements the read-write buffer |
| 252 | interface. The caller must determine the length of the buffer by other means, |
| 253 | or use ``w#`` instead. Only single-segment buffer objects are accepted; |
| 254 | :exc:`TypeError` is raised for all others. |
| 255 | |
Georg Brandl | 8fa8952 | 2008-09-01 16:45:35 +0000 | [diff] [blame] | 256 | ``w*`` (read-write byte-oriented buffer) [Py_buffer \*] |
| 257 | This is to ``w`` what ``s*`` is to ``s``. |
| 258 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 259 | ``w#`` (read-write character buffer) [char \*, int] |
| 260 | Like ``s#``, but accepts any object which implements the read-write buffer |
| 261 | interface. The :ctype:`char \*` variable is set to point to the first byte of |
| 262 | the buffer, and the :ctype:`int` is set to the length of the buffer. Only |
| 263 | single-segment buffer objects are accepted; :exc:`TypeError` is raised for all |
| 264 | others. |
| 265 | |
| 266 | ``(items)`` (tuple) [*matching-items*] |
| 267 | The object must be a Python sequence whose length is the number of format units |
| 268 | in *items*. The C arguments must correspond to the individual format units in |
| 269 | *items*. Format units for sequences may be nested. |
| 270 | |
| 271 | It is possible to pass "long" integers (integers whose value exceeds the |
| 272 | platform's :const:`LONG_MAX`) however no proper range checking is done --- the |
| 273 | most significant bits are silently truncated when the receiving field is too |
| 274 | small to receive the value (actually, the semantics are inherited from downcasts |
| 275 | in C --- your mileage may vary). |
| 276 | |
| 277 | A few other characters have a meaning in a format string. These may not occur |
| 278 | inside nested parentheses. They are: |
| 279 | |
| 280 | ``|`` |
| 281 | Indicates that the remaining arguments in the Python argument list are optional. |
| 282 | The C variables corresponding to optional arguments should be initialized to |
| 283 | their default value --- when an optional argument is not specified, |
| 284 | :cfunc:`PyArg_ParseTuple` does not touch the contents of the corresponding C |
| 285 | variable(s). |
| 286 | |
| 287 | ``:`` |
| 288 | The list of format units ends here; the string after the colon is used as the |
| 289 | function name in error messages (the "associated value" of the exception that |
| 290 | :cfunc:`PyArg_ParseTuple` raises). |
| 291 | |
| 292 | ``;`` |
| 293 | The list of format units ends here; the string after the semicolon is used as |
| 294 | the error message *instead* of the default error message. Clearly, ``:`` and |
| 295 | ``;`` mutually exclude each other. |
| 296 | |
| 297 | Note that any Python object references which are provided to the caller are |
| 298 | *borrowed* references; do not decrement their reference count! |
| 299 | |
| 300 | Additional arguments passed to these functions must be addresses of variables |
| 301 | whose type is determined by the format string; these are used to store values |
| 302 | from the input tuple. There are a few cases, as described in the list of format |
| 303 | units above, where these parameters are used as input values; they should match |
| 304 | what is specified for the corresponding format unit in that case. |
| 305 | |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 306 | For the conversion to succeed, the *arg* object must match the format |
| 307 | and the format must be exhausted. On success, the |
| 308 | :cfunc:`PyArg_Parse\*` functions return true, otherwise they return |
| 309 | false and raise an appropriate exception. When the |
| 310 | :cfunc:`PyArg_Parse\*` functions fail due to conversion failure in one |
| 311 | of the format units, the variables at the addresses corresponding to that |
| 312 | and the following format units are left untouched. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 313 | |
| 314 | |
| 315 | .. cfunction:: int PyArg_ParseTuple(PyObject *args, const char *format, ...) |
| 316 | |
| 317 | Parse the parameters of a function that takes only positional parameters into |
| 318 | local variables. Returns true on success; on failure, it returns false and |
| 319 | raises the appropriate exception. |
| 320 | |
| 321 | |
| 322 | .. cfunction:: int PyArg_VaParse(PyObject *args, const char *format, va_list vargs) |
| 323 | |
| 324 | Identical to :cfunc:`PyArg_ParseTuple`, except that it accepts a va_list rather |
| 325 | than a variable number of arguments. |
| 326 | |
| 327 | |
| 328 | .. cfunction:: int PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], ...) |
| 329 | |
| 330 | Parse the parameters of a function that takes both positional and keyword |
| 331 | parameters into local variables. Returns true on success; on failure, it |
| 332 | returns false and raises the appropriate exception. |
| 333 | |
| 334 | |
| 335 | .. cfunction:: int PyArg_VaParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], va_list vargs) |
| 336 | |
| 337 | Identical to :cfunc:`PyArg_ParseTupleAndKeywords`, except that it accepts a |
| 338 | va_list rather than a variable number of arguments. |
| 339 | |
| 340 | |
| 341 | .. XXX deprecated, will be removed |
| 342 | .. cfunction:: int PyArg_Parse(PyObject *args, const char *format, ...) |
| 343 | |
| 344 | Function used to deconstruct the argument lists of "old-style" functions --- |
| 345 | these are functions which use the :const:`METH_OLDARGS` parameter parsing |
| 346 | method. This is not recommended for use in parameter parsing in new code, and |
| 347 | most code in the standard interpreter has been modified to no longer use this |
| 348 | for that purpose. It does remain a convenient way to decompose other tuples, |
| 349 | however, and may continue to be used for that purpose. |
| 350 | |
| 351 | |
| 352 | .. cfunction:: int PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...) |
| 353 | |
| 354 | A simpler form of parameter retrieval which does not use a format string to |
| 355 | specify the types of the arguments. Functions which use this method to retrieve |
| 356 | their parameters should be declared as :const:`METH_VARARGS` in function or |
| 357 | method tables. The tuple containing the actual parameters should be passed as |
| 358 | *args*; it must actually be a tuple. The length of the tuple must be at least |
| 359 | *min* and no more than *max*; *min* and *max* may be equal. Additional |
| 360 | arguments must be passed to the function, each of which should be a pointer to a |
| 361 | :ctype:`PyObject\*` variable; these will be filled in with the values from |
| 362 | *args*; they will contain borrowed references. The variables which correspond |
| 363 | to optional parameters not given by *args* will not be filled in; these should |
| 364 | be initialized by the caller. This function returns true on success and false if |
| 365 | *args* is not a tuple or contains the wrong number of elements; an exception |
| 366 | will be set if there was a failure. |
| 367 | |
| 368 | This is an example of the use of this function, taken from the sources for the |
| 369 | :mod:`_weakref` helper module for weak references:: |
| 370 | |
| 371 | static PyObject * |
| 372 | weakref_ref(PyObject *self, PyObject *args) |
| 373 | { |
| 374 | PyObject *object; |
| 375 | PyObject *callback = NULL; |
| 376 | PyObject *result = NULL; |
| 377 | |
| 378 | if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) { |
| 379 | result = PyWeakref_NewRef(object, callback); |
| 380 | } |
| 381 | return result; |
| 382 | } |
| 383 | |
| 384 | The call to :cfunc:`PyArg_UnpackTuple` in this example is entirely equivalent to |
| 385 | this call to :cfunc:`PyArg_ParseTuple`:: |
| 386 | |
| 387 | PyArg_ParseTuple(args, "O|O:ref", &object, &callback) |
| 388 | |
| 389 | |
| 390 | .. cfunction:: PyObject* Py_BuildValue(const char *format, ...) |
| 391 | |
| 392 | Create a new value based on a format string similar to those accepted by the |
| 393 | :cfunc:`PyArg_Parse\*` family of functions and a sequence of values. Returns |
| 394 | the value or *NULL* in the case of an error; an exception will be raised if |
| 395 | *NULL* is returned. |
| 396 | |
| 397 | :cfunc:`Py_BuildValue` does not always build a tuple. It builds a tuple only if |
| 398 | its format string contains two or more format units. If the format string is |
| 399 | empty, it returns ``None``; if it contains exactly one format unit, it returns |
| 400 | whatever object is described by that format unit. To force it to return a tuple |
| 401 | of size 0 or one, parenthesize the format string. |
| 402 | |
| 403 | When memory buffers are passed as parameters to supply data to build objects, as |
| 404 | for the ``s`` and ``s#`` formats, the required data is copied. Buffers provided |
| 405 | by the caller are never referenced by the objects created by |
| 406 | :cfunc:`Py_BuildValue`. In other words, if your code invokes :cfunc:`malloc` |
| 407 | and passes the allocated memory to :cfunc:`Py_BuildValue`, your code is |
| 408 | responsible for calling :cfunc:`free` for that memory once |
| 409 | :cfunc:`Py_BuildValue` returns. |
| 410 | |
| 411 | In the following description, the quoted form is the format unit; the entry in |
| 412 | (round) parentheses is the Python object type that the format unit will return; |
| 413 | and the entry in [square] brackets is the type of the C value(s) to be passed. |
| 414 | |
| 415 | The characters space, tab, colon and comma are ignored in format strings (but |
| 416 | not within format units such as ``s#``). This can be used to make long format |
| 417 | strings a tad more readable. |
| 418 | |
| 419 | ``s`` (string) [char \*] |
| 420 | Convert a null-terminated C string to a Python object. If the C string pointer |
| 421 | is *NULL*, ``None`` is used. |
| 422 | |
| 423 | ``s#`` (string) [char \*, int] |
| 424 | Convert a C string and its length to a Python object. If the C string pointer |
| 425 | is *NULL*, the length is ignored and ``None`` is returned. |
| 426 | |
| 427 | ``z`` (string or ``None``) [char \*] |
| 428 | Same as ``s``. |
| 429 | |
| 430 | ``z#`` (string or ``None``) [char \*, int] |
| 431 | Same as ``s#``. |
| 432 | |
| 433 | ``u`` (Unicode string) [Py_UNICODE \*] |
| 434 | Convert a null-terminated buffer of Unicode (UCS-2 or UCS-4) data to a Python |
| 435 | Unicode object. If the Unicode buffer pointer is *NULL*, ``None`` is returned. |
| 436 | |
| 437 | ``u#`` (Unicode string) [Py_UNICODE \*, int] |
| 438 | Convert a Unicode (UCS-2 or UCS-4) data buffer and its length to a Python |
| 439 | Unicode object. If the Unicode buffer pointer is *NULL*, the length is ignored |
| 440 | and ``None`` is returned. |
| 441 | |
| 442 | ``U`` (string) [char \*] |
| 443 | Convert a null-terminated C string to a Python unicode object. If the C string |
| 444 | pointer is *NULL*, ``None`` is used. |
| 445 | |
| 446 | ``U#`` (string) [char \*, int] |
| 447 | Convert a C string and its length to a Python unicode object. If the C string |
| 448 | pointer is *NULL*, the length is ignored and ``None`` is returned. |
| 449 | |
| 450 | ``i`` (integer) [int] |
| 451 | Convert a plain C :ctype:`int` to a Python integer object. |
| 452 | |
| 453 | ``b`` (integer) [char] |
| 454 | Convert a plain C :ctype:`char` to a Python integer object. |
| 455 | |
| 456 | ``h`` (integer) [short int] |
| 457 | Convert a plain C :ctype:`short int` to a Python integer object. |
| 458 | |
| 459 | ``l`` (integer) [long int] |
| 460 | Convert a C :ctype:`long int` to a Python integer object. |
| 461 | |
| 462 | ``B`` (integer) [unsigned char] |
| 463 | Convert a C :ctype:`unsigned char` to a Python integer object. |
| 464 | |
| 465 | ``H`` (integer) [unsigned short int] |
| 466 | Convert a C :ctype:`unsigned short int` to a Python integer object. |
| 467 | |
| 468 | ``I`` (integer/long) [unsigned int] |
| 469 | Convert a C :ctype:`unsigned int` to a Python long integer object. |
| 470 | |
| 471 | ``k`` (integer/long) [unsigned long] |
| 472 | Convert a C :ctype:`unsigned long` to a Python long integer object. |
| 473 | |
| 474 | ``L`` (long) [PY_LONG_LONG] |
| 475 | Convert a C :ctype:`long long` to a Python integer object. Only available |
| 476 | on platforms that support :ctype:`long long`. |
| 477 | |
| 478 | ``K`` (long) [unsigned PY_LONG_LONG] |
| 479 | Convert a C :ctype:`unsigned long long` to a Python integer object. Only |
| 480 | available on platforms that support :ctype:`unsigned long long`. |
| 481 | |
| 482 | ``n`` (int) [Py_ssize_t] |
| 483 | Convert a C :ctype:`Py_ssize_t` to a Python integer. |
| 484 | |
| 485 | ``c`` (string of length 1) [char] |
| 486 | Convert a C :ctype:`int` representing a character to a Python string of length |
| 487 | 1. |
| 488 | |
| 489 | ``d`` (float) [double] |
| 490 | Convert a C :ctype:`double` to a Python floating point number. |
| 491 | |
| 492 | ``f`` (float) [float] |
| 493 | Same as ``d``. |
| 494 | |
| 495 | ``D`` (complex) [Py_complex \*] |
| 496 | Convert a C :ctype:`Py_complex` structure to a Python complex number. |
| 497 | |
| 498 | ``O`` (object) [PyObject \*] |
| 499 | Pass a Python object untouched (except for its reference count, which is |
| 500 | incremented by one). If the object passed in is a *NULL* pointer, it is assumed |
| 501 | that this was caused because the call producing the argument found an error and |
| 502 | set an exception. Therefore, :cfunc:`Py_BuildValue` will return *NULL* but won't |
| 503 | raise an exception. If no exception has been raised yet, :exc:`SystemError` is |
| 504 | set. |
| 505 | |
| 506 | ``S`` (object) [PyObject \*] |
| 507 | Same as ``O``. |
| 508 | |
| 509 | ``N`` (object) [PyObject \*] |
| 510 | Same as ``O``, except it doesn't increment the reference count on the object. |
| 511 | Useful when the object is created by a call to an object constructor in the |
| 512 | argument list. |
| 513 | |
| 514 | ``O&`` (object) [*converter*, *anything*] |
| 515 | Convert *anything* to a Python object through a *converter* function. The |
| 516 | function is called with *anything* (which should be compatible with :ctype:`void |
| 517 | \*`) as its argument and should return a "new" Python object, or *NULL* if an |
| 518 | error occurred. |
| 519 | |
| 520 | ``(items)`` (tuple) [*matching-items*] |
| 521 | Convert a sequence of C values to a Python tuple with the same number of items. |
| 522 | |
| 523 | ``[items]`` (list) [*matching-items*] |
| 524 | Convert a sequence of C values to a Python list with the same number of items. |
| 525 | |
| 526 | ``{items}`` (dictionary) [*matching-items*] |
| 527 | Convert a sequence of C values to a Python dictionary. Each pair of consecutive |
| 528 | C values adds one item to the dictionary, serving as key and value, |
| 529 | respectively. |
| 530 | |
| 531 | If there is an error in the format string, the :exc:`SystemError` exception is |
| 532 | set and *NULL* returned. |