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