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