blob: ba9ca5e0d30b9207276b8f703dfba0514299fddc [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
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
Georg Brandl60203b42010-10-06 10:11:56 +000012The first three of these functions described, :c:func:`PyArg_ParseTuple`,
13:c:func:`PyArg_ParseTupleAndKeywords`, and :c:func:`PyArg_Parse`, all use *format
Georg Brandl54a3faa2008-01-20 09:30:57 +000014strings* which are used to tell the function about the expected arguments. The
15format strings use the same syntax for each of these functions.
16
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000017-----------------
18Parsing arguments
19-----------------
20
Georg Brandl54a3faa2008-01-20 09:30:57 +000021A 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
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000030Strings and buffers
31-------------------
32
Martin Panterc04fb562016-02-10 05:44:01 +000033These formats allow accessing an object as a contiguous chunk of memory.
Antoine Pitroud53dfa32011-01-06 07:16:31 +000034You don't have to provide raw storage for the returned unicode or bytes
Martin Panter6c7d5a22016-08-03 14:35:05 +000035area.
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#``.
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000041
Georg Brandl60203b42010-10-06 10:11:56 +000042However, when a :c:type:`Py_buffer` structure gets filled, the underlying
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000043buffer is locked so that the caller can subsequently use the buffer even
Georg Brandl60203b42010-10-06 10:11:56 +000044inside a :c:type:`Py_BEGIN_ALLOW_THREADS` block without the risk of mutable data
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000045being resized or destroyed. As a result, **you have to call**
Georg Brandl60203b42010-10-06 10:11:56 +000046:c:func:`PyBuffer_Release` after you have finished processing the data (or
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000047in any early abort case).
48
49Unless otherwise stated, buffers are not NUL-terminated.
50
Martin Panter6c7d5a22016-08-03 14:35:05 +000051Some 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
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000056.. note::
Larry Hastings3732ed22014-03-15 21:13:56 -070057
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000058 For all ``#`` variants of formats (``s#``, ``y#``, etc.), the type of
Georg Brandl60203b42010-10-06 10:11:56 +000059 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
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000061 :file:`Python.h`. If the macro was defined, length is a
Georg Brandl60203b42010-10-06 10:11:56 +000062 :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`.
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000065
66
Victor Stinner69e25fa2010-06-07 21:20:41 +000067``s`` (:class:`str`) [const char \*]
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000068 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.
Martin Panter6c7d5a22016-08-03 14:35:05 +000071 The Python string must not contain embedded null code points; if it does,
Serhiy Storchaka6f379f42016-07-12 09:14:15 +030072 a :exc:`ValueError` exception is raised. Unicode objects are converted
Victor Stinner69e25fa2010-06-07 21:20:41 +000073 to C strings using ``'utf-8'`` encoding. If this conversion fails, a
Georg Brandl54a3faa2008-01-20 09:30:57 +000074 :exc:`UnicodeError` is raised.
75
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000076 .. note::
Serhiy Storchakab757c832014-12-05 22:25:22 +020077 This format does not accept :term:`bytes-like objects
78 <bytes-like object>`. If you want to accept
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000079 filesystem paths and convert them to C character strings, it is
Georg Brandl60203b42010-10-06 10:11:56 +000080 preferable to use the ``O&`` format with :c:func:`PyUnicode_FSConverter`
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000081 as *converter*.
Benjamin Peterson4469d0c2008-11-30 22:46:23 +000082
Serhiy Storchaka6f379f42016-07-12 09:14:15 +030083 .. versionchanged:: 3.5
Martin Panter6c7d5a22016-08-03 14:35:05 +000084 Previously, :exc:`TypeError` was raised when embedded null code points
Serhiy Storchaka6f379f42016-07-12 09:14:15 +030085 were encountered in the Python string.
86
Serhiy Storchakab757c832014-12-05 22:25:22 +020087``s*`` (:class:`str` or :term:`bytes-like object`) [Py_buffer]
88 This format accepts Unicode objects as well as bytes-like objects.
Georg Brandl60203b42010-10-06 10:11:56 +000089 It fills a :c:type:`Py_buffer` structure provided by the caller.
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000090 In this case the resulting C string may contain embedded NUL bytes.
Victor Stinner69e25fa2010-06-07 21:20:41 +000091 Unicode objects are converted to C strings using ``'utf-8'`` encoding.
Georg Brandl8fa89522008-09-01 16:45:35 +000092
Serhiy Storchakab757c832014-12-05 22:25:22 +020093``s#`` (:class:`str`, read-only :term:`bytes-like object`) [const char \*, int or :c:type:`Py_ssize_t`]
Martin Panter6c7d5a22016-08-03 14:35:05 +000094 Like ``s*``, except that it doesn't accept mutable objects.
95 The result is stored into two C variables,
Antoine Pitrou83fd9b92010-05-03 15:57:23 +000096 the first one a pointer to a C string, the second one its length.
Victor Stinner69e25fa2010-06-07 21:20:41 +000097 The string may contain embedded null bytes. Unicode objects are converted
98 to C strings using ``'utf-8'`` encoding.
Benjamin Peterson4469d0c2008-11-30 22:46:23 +000099
Victor Stinner69e25fa2010-06-07 21:20:41 +0000100``z`` (:class:`str` or ``None``) [const char \*]
Georg Brandl54a3faa2008-01-20 09:30:57 +0000101 Like ``s``, but the Python object may also be ``None``, in which case the C
102 pointer is set to *NULL*.
103
Serhiy Storchakab757c832014-12-05 22:25:22 +0200104``z*`` (:class:`str`, :term:`bytes-like object` or ``None``) [Py_buffer]
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000105 Like ``s*``, but the Python object may also be ``None``, in which case the
Georg Brandl60203b42010-10-06 10:11:56 +0000106 ``buf`` member of the :c:type:`Py_buffer` structure is set to *NULL*.
Martin v. Löwis423be952008-08-13 15:53:07 +0000107
Serhiy Storchakab757c832014-12-05 22:25:22 +0200108``z#`` (:class:`str`, read-only :term:`bytes-like object` or ``None``) [const char \*, int]
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000109 Like ``s#``, but the Python object may also be ``None``, in which case the C
110 pointer is set to *NULL*.
111
Serhiy Storchakab757c832014-12-05 22:25:22 +0200112``y`` (read-only :term:`bytes-like object`) [const char \*]
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000113 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
Serhiy Storchaka6f379f42016-07-12 09:14:15 +0300115 contain embedded null bytes; if it does, a :exc:`ValueError`
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000116 exception is raised.
117
Serhiy Storchaka6f379f42016-07-12 09:14:15 +0300118 .. versionchanged:: 3.5
119 Previously, :exc:`TypeError` was raised when embedded null bytes were
120 encountered in the bytes buffer.
121
Serhiy Storchakab757c832014-12-05 22:25:22 +0200122``y*`` (:term:`bytes-like object`) [Py_buffer]
Ezio Melottic228e962013-05-04 18:06:34 +0300123 This variant on ``s*`` doesn't accept Unicode objects, only
Serhiy Storchakab757c832014-12-05 22:25:22 +0200124 bytes-like objects. **This is the recommended way to accept
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000125 binary data.**
126
Serhiy Storchakab757c832014-12-05 22:25:22 +0200127``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.
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000130
Victor Stinner69e25fa2010-06-07 21:20:41 +0000131``S`` (:class:`bytes`) [PyBytesObject \*]
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000132 Requires that the Python object is a :class:`bytes` object, without
133 attempting any conversion. Raises :exc:`TypeError` if the object is not
Georg Brandl60203b42010-10-06 10:11:56 +0000134 a bytes object. The C variable may also be declared as :c:type:`PyObject\*`.
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000135
Victor Stinner69e25fa2010-06-07 21:20:41 +0000136``Y`` (:class:`bytearray`) [PyByteArrayObject \*]
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000137 Requires that the Python object is a :class:`bytearray` object, without
138 attempting any conversion. Raises :exc:`TypeError` if the object is not
Georg Brandl60203b42010-10-06 10:11:56 +0000139 a :class:`bytearray` object. The C variable may also be declared as :c:type:`PyObject\*`.
Georg Brandl8fa89522008-09-01 16:45:35 +0000140
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300141``u`` (:class:`str`) [const Py_UNICODE \*]
Georg Brandl54a3faa2008-01-20 09:30:57 +0000142 Convert a Python Unicode object to a C pointer to a NUL-terminated buffer of
Georg Brandl60203b42010-10-06 10:11:56 +0000143 Unicode characters. You must pass the address of a :c:type:`Py_UNICODE`
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000144 pointer variable, which will be filled with the pointer to an existing
Georg Brandl60203b42010-10-06 10:11:56 +0000145 Unicode buffer. Please note that the width of a :c:type:`Py_UNICODE`
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000146 character depends on compilation options (it is either 16 or 32 bits).
Martin Panter6c7d5a22016-08-03 14:35:05 +0000147 The Python string must not contain embedded null code points; if it does,
Serhiy Storchaka6f379f42016-07-12 09:14:15 +0300148 a :exc:`ValueError` exception is raised.
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000149
Serhiy Storchaka6f379f42016-07-12 09:14:15 +0300150 .. versionchanged:: 3.5
Martin Panter6c7d5a22016-08-03 14:35:05 +0000151 Previously, :exc:`TypeError` was raised when embedded null code points
Serhiy Storchaka6f379f42016-07-12 09:14:15 +0300152 were encountered in the Python string.
153
INADA Naoki42ec1902018-04-04 13:59:08 +0900154 .. deprecated-removed:: 3.3 4.0
155 Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
156 :c:func:`PyUnicode_AsWideCharString`.
157
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300158``u#`` (:class:`str`) [const Py_UNICODE \*, int]
Georg Brandl54a3faa2008-01-20 09:30:57 +0000159 This variant on ``u`` stores into two C variables, the first one a pointer to a
Martin Panter6c7d5a22016-08-03 14:35:05 +0000160 Unicode data buffer, the second one its length. This variant allows
161 null code points.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000162
INADA Naoki42ec1902018-04-04 13:59:08 +0900163 .. deprecated-removed:: 3.3 4.0
164 Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
165 :c:func:`PyUnicode_AsWideCharString`.
166
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300167``Z`` (:class:`str` or ``None``) [const Py_UNICODE \*]
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000168 Like ``u``, but the Python object may also be ``None``, in which case the
Georg Brandl60203b42010-10-06 10:11:56 +0000169 :c:type:`Py_UNICODE` pointer is set to *NULL*.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000170
INADA Naoki42ec1902018-04-04 13:59:08 +0900171 .. deprecated-removed:: 3.3 4.0
172 Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
173 :c:func:`PyUnicode_AsWideCharString`.
174
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300175``Z#`` (:class:`str` or ``None``) [const Py_UNICODE \*, int]
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000176 Like ``u#``, but the Python object may also be ``None``, in which case the
Georg Brandl60203b42010-10-06 10:11:56 +0000177 :c:type:`Py_UNICODE` pointer is set to *NULL*.
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000178
INADA Naoki42ec1902018-04-04 13:59:08 +0900179 .. deprecated-removed:: 3.3 4.0
180 Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
181 :c:func:`PyUnicode_AsWideCharString`.
182
Victor Stinner3ba82aa2011-12-01 02:52:55 +0100183``U`` (:class:`str`) [PyObject \*]
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000184 Requires that the Python object is a Unicode object, without attempting
185 any conversion. Raises :exc:`TypeError` if the object is not a Unicode
Georg Brandl60203b42010-10-06 10:11:56 +0000186 object. The C variable may also be declared as :c:type:`PyObject\*`.
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000187
Larry Hastingsab792ac2015-04-13 11:30:56 -0400188``w*`` (read-write :term:`bytes-like object`) [Py_buffer]
Victor Stinner25e8ec42010-06-25 00:02:38 +0000189 This format accepts any object which implements the read-write buffer
Georg Brandl60203b42010-10-06 10:11:56 +0000190 interface. It fills a :c:type:`Py_buffer` structure provided by the caller.
Victor Stinner25e8ec42010-06-25 00:02:38 +0000191 The buffer may contain embedded null bytes. The caller have to call
Georg Brandl60203b42010-10-06 10:11:56 +0000192 :c:func:`PyBuffer_Release` when it is done with the buffer.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000193
Victor Stinner69e25fa2010-06-07 21:20:41 +0000194``es`` (:class:`str`) [const char \*encoding, char \*\*buffer]
Victor Stinner7909b002010-06-11 23:30:12 +0000195 This variant on ``s`` is used for encoding Unicode into a character buffer.
196 It only works for encoded data without embedded NUL bytes.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000197
198 This format requires two arguments. The first is only used as input, and
Georg Brandl60203b42010-10-06 10:11:56 +0000199 must be a :c:type:`const char\*` which points to the name of an encoding as a
Victor Stinner7909b002010-06-11 23:30:12 +0000200 NUL-terminated string, or *NULL*, in which case ``'utf-8'`` encoding is used.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000201 An exception is raised if the named encoding is not known to Python. The
Georg Brandl60203b42010-10-06 10:11:56 +0000202 second argument must be a :c:type:`char\*\*`; the value of the pointer it
Georg Brandl54a3faa2008-01-20 09:30:57 +0000203 references will be set to a buffer with the contents of the argument text.
204 The text will be encoded in the encoding specified by the first argument.
205
Georg Brandl60203b42010-10-06 10:11:56 +0000206 :c:func:`PyArg_ParseTuple` will allocate a buffer of the needed size, copy the
Georg Brandl54a3faa2008-01-20 09:30:57 +0000207 encoded data into this buffer and adjust *\*buffer* to reference the newly
Georg Brandl60203b42010-10-06 10:11:56 +0000208 allocated storage. The caller is responsible for calling :c:func:`PyMem_Free` to
Georg Brandl54a3faa2008-01-20 09:30:57 +0000209 free the allocated buffer after use.
210
Victor Stinner69e25fa2010-06-07 21:20:41 +0000211``et`` (:class:`str`, :class:`bytes` or :class:`bytearray`) [const char \*encoding, char \*\*buffer]
212 Same as ``es`` except that byte string objects are passed through without
213 recoding them. Instead, the implementation assumes that the byte string object uses
Georg Brandl54a3faa2008-01-20 09:30:57 +0000214 the encoding passed in as parameter.
215
Victor Stinner69e25fa2010-06-07 21:20:41 +0000216``es#`` (:class:`str`) [const char \*encoding, char \*\*buffer, int \*buffer_length]
Victor Stinner7909b002010-06-11 23:30:12 +0000217 This variant on ``s#`` is used for encoding Unicode into a character buffer.
218 Unlike the ``es`` format, this variant allows input data which contains NUL
219 characters.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000220
221 It requires three arguments. The first is only used as input, and must be a
Georg Brandl60203b42010-10-06 10:11:56 +0000222 :c:type:`const char\*` which points to the name of an encoding as a
Victor Stinner7909b002010-06-11 23:30:12 +0000223 NUL-terminated string, or *NULL*, in which case ``'utf-8'`` encoding is used.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000224 An exception is raised if the named encoding is not known to Python. The
Georg Brandl60203b42010-10-06 10:11:56 +0000225 second argument must be a :c:type:`char\*\*`; the value of the pointer it
Georg Brandl54a3faa2008-01-20 09:30:57 +0000226 references will be set to a buffer with the contents of the argument text.
227 The text will be encoded in the encoding specified by the first argument.
228 The third argument must be a pointer to an integer; the referenced integer
229 will be set to the number of bytes in the output buffer.
230
231 There are two modes of operation:
232
233 If *\*buffer* points a *NULL* pointer, the function will allocate a buffer of
234 the needed size, copy the encoded data into this buffer and set *\*buffer* to
235 reference the newly allocated storage. The caller is responsible for calling
Georg Brandl60203b42010-10-06 10:11:56 +0000236 :c:func:`PyMem_Free` to free the allocated buffer after usage.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000237
238 If *\*buffer* points to a non-*NULL* pointer (an already allocated buffer),
Georg Brandl60203b42010-10-06 10:11:56 +0000239 :c:func:`PyArg_ParseTuple` will use this location as the buffer and interpret the
Georg Brandl54a3faa2008-01-20 09:30:57 +0000240 initial value of *\*buffer_length* as the buffer size. It will then copy the
241 encoded data into the buffer and NUL-terminate it. If the buffer is not large
242 enough, a :exc:`ValueError` will be set.
243
244 In both cases, *\*buffer_length* is set to the length of the encoded data
245 without the trailing NUL byte.
246
Victor Stinner69e25fa2010-06-07 21:20:41 +0000247``et#`` (:class:`str`, :class:`bytes` or :class:`bytearray`) [const char \*encoding, char \*\*buffer, int \*buffer_length]
248 Same as ``es#`` except that byte string objects are passed through without recoding
249 them. Instead, the implementation assumes that the byte string object uses the
Georg Brandl54a3faa2008-01-20 09:30:57 +0000250 encoding passed in as parameter.
251
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000252Numbers
253-------
254
Victor Stinner69e25fa2010-06-07 21:20:41 +0000255``b`` (:class:`int`) [unsigned char]
Benjamin Petersonda10d3b2009-01-01 00:23:30 +0000256 Convert a nonnegative Python integer to an unsigned tiny int, stored in a C
Georg Brandl60203b42010-10-06 10:11:56 +0000257 :c:type:`unsigned char`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000258
Victor Stinner69e25fa2010-06-07 21:20:41 +0000259``B`` (:class:`int`) [unsigned char]
Georg Brandl54a3faa2008-01-20 09:30:57 +0000260 Convert a Python integer to a tiny int without overflow checking, stored in a C
Georg Brandl60203b42010-10-06 10:11:56 +0000261 :c:type:`unsigned char`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000262
Victor Stinner69e25fa2010-06-07 21:20:41 +0000263``h`` (:class:`int`) [short int]
Georg Brandl60203b42010-10-06 10:11:56 +0000264 Convert a Python integer to a C :c:type:`short int`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000265
Victor Stinner69e25fa2010-06-07 21:20:41 +0000266``H`` (:class:`int`) [unsigned short int]
Georg Brandl60203b42010-10-06 10:11:56 +0000267 Convert a Python integer to a C :c:type:`unsigned short int`, without overflow
Georg Brandl54a3faa2008-01-20 09:30:57 +0000268 checking.
269
Victor Stinner69e25fa2010-06-07 21:20:41 +0000270``i`` (:class:`int`) [int]
Georg Brandl60203b42010-10-06 10:11:56 +0000271 Convert a Python integer to a plain C :c:type:`int`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000272
Victor Stinner69e25fa2010-06-07 21:20:41 +0000273``I`` (:class:`int`) [unsigned int]
Georg Brandl60203b42010-10-06 10:11:56 +0000274 Convert a Python integer to a C :c:type:`unsigned int`, without overflow
Georg Brandl54a3faa2008-01-20 09:30:57 +0000275 checking.
276
Victor Stinner69e25fa2010-06-07 21:20:41 +0000277``l`` (:class:`int`) [long int]
Georg Brandl60203b42010-10-06 10:11:56 +0000278 Convert a Python integer to a C :c:type:`long int`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000279
Victor Stinner69e25fa2010-06-07 21:20:41 +0000280``k`` (:class:`int`) [unsigned long]
Georg Brandl60203b42010-10-06 10:11:56 +0000281 Convert a Python integer to a C :c:type:`unsigned long` without
Georg Brandl54a3faa2008-01-20 09:30:57 +0000282 overflow checking.
283
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700284``L`` (:class:`int`) [long long]
285 Convert a Python integer to a C :c:type:`long long`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000286
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700287``K`` (:class:`int`) [unsigned long long]
Georg Brandl60203b42010-10-06 10:11:56 +0000288 Convert a Python integer to a C :c:type:`unsigned long long`
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700289 without overflow checking.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000290
Victor Stinner69e25fa2010-06-07 21:20:41 +0000291``n`` (:class:`int`) [Py_ssize_t]
Georg Brandl60203b42010-10-06 10:11:56 +0000292 Convert a Python integer to a C :c:type:`Py_ssize_t`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000293
Eli Bendersky906b88f2011-07-29 07:05:08 +0300294``c`` (:class:`bytes` or :class:`bytearray` of length 1) [char]
295 Convert a Python byte, represented as a :class:`bytes` or
296 :class:`bytearray` object of length 1, to a C :c:type:`char`.
297
Georg Brandl61063cc2012-06-24 22:48:30 +0200298 .. versionchanged:: 3.3
299 Allow :class:`bytearray` objects.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000300
Victor Stinner69e25fa2010-06-07 21:20:41 +0000301``C`` (:class:`str` of length 1) [int]
302 Convert a Python character, represented as a :class:`str` object of
Georg Brandl60203b42010-10-06 10:11:56 +0000303 length 1, to a C :c:type:`int`.
Benjamin Peterson7fe98532009-04-02 00:33:55 +0000304
Victor Stinner69e25fa2010-06-07 21:20:41 +0000305``f`` (:class:`float`) [float]
Georg Brandl60203b42010-10-06 10:11:56 +0000306 Convert a Python floating point number to a C :c:type:`float`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000307
Victor Stinner69e25fa2010-06-07 21:20:41 +0000308``d`` (:class:`float`) [double]
Georg Brandl60203b42010-10-06 10:11:56 +0000309 Convert a Python floating point number to a C :c:type:`double`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000310
Victor Stinner69e25fa2010-06-07 21:20:41 +0000311``D`` (:class:`complex`) [Py_complex]
Georg Brandl60203b42010-10-06 10:11:56 +0000312 Convert a Python complex number to a C :c:type:`Py_complex` structure.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000313
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000314Other objects
315-------------
316
Georg Brandl54a3faa2008-01-20 09:30:57 +0000317``O`` (object) [PyObject \*]
318 Store a Python object (without any conversion) in a C object pointer. The C
319 program thus receives the actual object that was passed. The object's reference
320 count is not increased. The pointer stored is not *NULL*.
321
322``O!`` (object) [*typeobject*, PyObject \*]
323 Store a Python object in a C object pointer. This is similar to ``O``, but
324 takes two C arguments: the first is the address of a Python type object, the
Georg Brandl60203b42010-10-06 10:11:56 +0000325 second is the address of the C variable (of type :c:type:`PyObject\*`) into which
Georg Brandl54a3faa2008-01-20 09:30:57 +0000326 the object pointer is stored. If the Python object does not have the required
327 type, :exc:`TypeError` is raised.
328
Larry Hastings2a727912014-01-16 11:32:01 -0800329.. _o_ampersand:
330
Georg Brandl54a3faa2008-01-20 09:30:57 +0000331``O&`` (object) [*converter*, *anything*]
332 Convert a Python object to a C variable through a *converter* function. This
333 takes two arguments: the first is a function, the second is the address of a C
Georg Brandl60203b42010-10-06 10:11:56 +0000334 variable (of arbitrary type), converted to :c:type:`void \*`. The *converter*
Georg Brandl54a3faa2008-01-20 09:30:57 +0000335 function in turn is called as follows::
336
337 status = converter(object, address);
338
339 where *object* is the Python object to be converted and *address* is the
Georg Brandl60203b42010-10-06 10:11:56 +0000340 :c:type:`void\*` argument that was passed to the :c:func:`PyArg_Parse\*` function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000341 The returned *status* should be ``1`` for a successful conversion and ``0`` if
342 the conversion has failed. When the conversion fails, the *converter* function
Christian Heimes78644762008-03-04 23:39:23 +0000343 should raise an exception and leave the content of *address* unmodified.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000344
Georg Brandl67b21b72010-08-17 15:07:14 +0000345 If the *converter* returns ``Py_CLEANUP_SUPPORTED``, it may get called a
346 second time if the argument parsing eventually fails, giving the converter a
347 chance to release any memory that it had already allocated. In this second
348 call, the *object* parameter will be NULL; *address* will have the same value
349 as in the original call.
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000350
351 .. versionchanged:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +0000352 ``Py_CLEANUP_SUPPORTED`` was added.
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000353
Larry Hastingsfaf91e72012-05-05 16:54:29 -0700354``p`` (:class:`bool`) [int]
Serhiy Storchakabf7b9ed2015-11-23 16:43:05 +0200355 Tests the value passed in for truth (a boolean **p**\ redicate) and converts
Larry Hastingsfaf91e72012-05-05 16:54:29 -0700356 the result to its equivalent C true/false integer value.
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300357 Sets the int to ``1`` if the expression was true and ``0`` if it was false.
Larry Hastingsfaf91e72012-05-05 16:54:29 -0700358 This accepts any valid Python value. See :ref:`truth` for more
359 information about how Python tests values for truth.
360
Georg Brandl61063cc2012-06-24 22:48:30 +0200361 .. versionadded:: 3.3
Larry Hastingsfaf91e72012-05-05 16:54:29 -0700362
Victor Stinner69e25fa2010-06-07 21:20:41 +0000363``(items)`` (:class:`tuple`) [*matching-items*]
Georg Brandl54a3faa2008-01-20 09:30:57 +0000364 The object must be a Python sequence whose length is the number of format units
365 in *items*. The C arguments must correspond to the individual format units in
366 *items*. Format units for sequences may be nested.
367
368It is possible to pass "long" integers (integers whose value exceeds the
369platform's :const:`LONG_MAX`) however no proper range checking is done --- the
370most significant bits are silently truncated when the receiving field is too
371small to receive the value (actually, the semantics are inherited from downcasts
372in C --- your mileage may vary).
373
374A few other characters have a meaning in a format string. These may not occur
375inside nested parentheses. They are:
376
377``|``
378 Indicates that the remaining arguments in the Python argument list are optional.
379 The C variables corresponding to optional arguments should be initialized to
380 their default value --- when an optional argument is not specified,
Georg Brandl60203b42010-10-06 10:11:56 +0000381 :c:func:`PyArg_ParseTuple` does not touch the contents of the corresponding C
Georg Brandl54a3faa2008-01-20 09:30:57 +0000382 variable(s).
383
Larry Hastings83a9f482012-03-20 20:06:16 +0000384``$``
385 :c:func:`PyArg_ParseTupleAndKeywords` only:
386 Indicates that the remaining arguments in the Python argument list are
387 keyword-only. Currently, all keyword-only arguments must also be optional
388 arguments, so ``|`` must always be specified before ``$`` in the format
389 string.
390
391 .. versionadded:: 3.3
392
Georg Brandl54a3faa2008-01-20 09:30:57 +0000393``:``
394 The list of format units ends here; the string after the colon is used as the
395 function name in error messages (the "associated value" of the exception that
Georg Brandl60203b42010-10-06 10:11:56 +0000396 :c:func:`PyArg_ParseTuple` raises).
Georg Brandl54a3faa2008-01-20 09:30:57 +0000397
398``;``
399 The list of format units ends here; the string after the semicolon is used as
Benjamin Peterson92035012008-12-27 16:00:54 +0000400 the error message *instead* of the default error message. ``:`` and ``;``
401 mutually exclude each other.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000402
403Note that any Python object references which are provided to the caller are
404*borrowed* references; do not decrement their reference count!
405
406Additional arguments passed to these functions must be addresses of variables
407whose type is determined by the format string; these are used to store values
408from the input tuple. There are a few cases, as described in the list of format
409units above, where these parameters are used as input values; they should match
410what is specified for the corresponding format unit in that case.
411
Christian Heimes78644762008-03-04 23:39:23 +0000412For the conversion to succeed, the *arg* object must match the format
413and the format must be exhausted. On success, the
Georg Brandl60203b42010-10-06 10:11:56 +0000414:c:func:`PyArg_Parse\*` functions return true, otherwise they return
Christian Heimes78644762008-03-04 23:39:23 +0000415false and raise an appropriate exception. When the
Georg Brandl60203b42010-10-06 10:11:56 +0000416:c:func:`PyArg_Parse\*` functions fail due to conversion failure in one
Christian Heimes78644762008-03-04 23:39:23 +0000417of the format units, the variables at the addresses corresponding to that
418and the following format units are left untouched.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000419
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000420API Functions
421-------------
Georg Brandl54a3faa2008-01-20 09:30:57 +0000422
Georg Brandl60203b42010-10-06 10:11:56 +0000423.. c:function:: int PyArg_ParseTuple(PyObject *args, const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000424
425 Parse the parameters of a function that takes only positional parameters into
426 local variables. Returns true on success; on failure, it returns false and
427 raises the appropriate exception.
428
429
Georg Brandl60203b42010-10-06 10:11:56 +0000430.. c:function:: int PyArg_VaParse(PyObject *args, const char *format, va_list vargs)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000431
Georg Brandl60203b42010-10-06 10:11:56 +0000432 Identical to :c:func:`PyArg_ParseTuple`, except that it accepts a va_list rather
Georg Brandl54a3faa2008-01-20 09:30:57 +0000433 than a variable number of arguments.
434
435
Georg Brandl60203b42010-10-06 10:11:56 +0000436.. c:function:: int PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000437
438 Parse the parameters of a function that takes both positional and keyword
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +0300439 parameters into local variables. The *keywords* argument is a
440 *NULL*-terminated array of keyword parameter names. Empty names denote
441 :ref:`positional-only parameters <positional-only_parameter>`.
442 Returns true on success; on failure, it returns false and raises the
443 appropriate exception.
444
445 .. versionchanged:: 3.6
446 Added support for :ref:`positional-only parameters
447 <positional-only_parameter>`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000448
449
Georg Brandl60203b42010-10-06 10:11:56 +0000450.. c:function:: int PyArg_VaParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], va_list vargs)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000451
Georg Brandl60203b42010-10-06 10:11:56 +0000452 Identical to :c:func:`PyArg_ParseTupleAndKeywords`, except that it accepts a
Georg Brandl54a3faa2008-01-20 09:30:57 +0000453 va_list rather than a variable number of arguments.
454
455
Georg Brandl60203b42010-10-06 10:11:56 +0000456.. c:function:: int PyArg_ValidateKeywordArguments(PyObject *)
Benjamin Petersonfb886362010-04-24 18:21:17 +0000457
458 Ensure that the keys in the keywords argument dictionary are strings. This
Georg Brandl60203b42010-10-06 10:11:56 +0000459 is only needed if :c:func:`PyArg_ParseTupleAndKeywords` is not used, since the
Benjamin Petersonfb886362010-04-24 18:21:17 +0000460 latter already does this check.
461
Benjamin Peterson44d3d782010-04-25 21:03:34 +0000462 .. versionadded:: 3.2
463
Benjamin Petersonfb886362010-04-24 18:21:17 +0000464
Georg Brandl54a3faa2008-01-20 09:30:57 +0000465.. XXX deprecated, will be removed
Georg Brandl60203b42010-10-06 10:11:56 +0000466.. c:function:: int PyArg_Parse(PyObject *args, const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000467
468 Function used to deconstruct the argument lists of "old-style" functions ---
469 these are functions which use the :const:`METH_OLDARGS` parameter parsing
Georg Brandla4c8c472014-10-31 10:38:49 +0100470 method, which has been removed in Python 3. This is not recommended for use
471 in parameter parsing in new code, and most code in the standard interpreter
472 has been modified to no longer use this for that purpose. It does remain a
473 convenient way to decompose other tuples, however, and may continue to be
474 used for that purpose.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000475
476
Georg Brandl60203b42010-10-06 10:11:56 +0000477.. c:function:: int PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000478
479 A simpler form of parameter retrieval which does not use a format string to
480 specify the types of the arguments. Functions which use this method to retrieve
481 their parameters should be declared as :const:`METH_VARARGS` in function or
482 method tables. The tuple containing the actual parameters should be passed as
483 *args*; it must actually be a tuple. The length of the tuple must be at least
484 *min* and no more than *max*; *min* and *max* may be equal. Additional
485 arguments must be passed to the function, each of which should be a pointer to a
Georg Brandl60203b42010-10-06 10:11:56 +0000486 :c:type:`PyObject\*` variable; these will be filled in with the values from
Georg Brandl54a3faa2008-01-20 09:30:57 +0000487 *args*; they will contain borrowed references. The variables which correspond
488 to optional parameters not given by *args* will not be filled in; these should
489 be initialized by the caller. This function returns true on success and false if
490 *args* is not a tuple or contains the wrong number of elements; an exception
491 will be set if there was a failure.
492
493 This is an example of the use of this function, taken from the sources for the
494 :mod:`_weakref` helper module for weak references::
495
496 static PyObject *
497 weakref_ref(PyObject *self, PyObject *args)
498 {
499 PyObject *object;
500 PyObject *callback = NULL;
501 PyObject *result = NULL;
502
503 if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) {
504 result = PyWeakref_NewRef(object, callback);
505 }
506 return result;
507 }
508
Georg Brandl60203b42010-10-06 10:11:56 +0000509 The call to :c:func:`PyArg_UnpackTuple` in this example is entirely equivalent to
510 this call to :c:func:`PyArg_ParseTuple`::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000511
512 PyArg_ParseTuple(args, "O|O:ref", &object, &callback)
513
514
Antoine Pitrou83fd9b92010-05-03 15:57:23 +0000515---------------
516Building values
517---------------
518
Georg Brandl60203b42010-10-06 10:11:56 +0000519.. c:function:: PyObject* Py_BuildValue(const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000520
521 Create a new value based on a format string similar to those accepted by the
Georg Brandl60203b42010-10-06 10:11:56 +0000522 :c:func:`PyArg_Parse\*` family of functions and a sequence of values. Returns
Georg Brandl54a3faa2008-01-20 09:30:57 +0000523 the value or *NULL* in the case of an error; an exception will be raised if
524 *NULL* is returned.
525
Georg Brandl60203b42010-10-06 10:11:56 +0000526 :c:func:`Py_BuildValue` does not always build a tuple. It builds a tuple only if
Georg Brandl54a3faa2008-01-20 09:30:57 +0000527 its format string contains two or more format units. If the format string is
528 empty, it returns ``None``; if it contains exactly one format unit, it returns
529 whatever object is described by that format unit. To force it to return a tuple
530 of size 0 or one, parenthesize the format string.
531
532 When memory buffers are passed as parameters to supply data to build objects, as
533 for the ``s`` and ``s#`` formats, the required data is copied. Buffers provided
534 by the caller are never referenced by the objects created by
Georg Brandl60203b42010-10-06 10:11:56 +0000535 :c:func:`Py_BuildValue`. In other words, if your code invokes :c:func:`malloc`
536 and passes the allocated memory to :c:func:`Py_BuildValue`, your code is
537 responsible for calling :c:func:`free` for that memory once
538 :c:func:`Py_BuildValue` returns.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000539
540 In the following description, the quoted form is the format unit; the entry in
541 (round) parentheses is the Python object type that the format unit will return;
542 and the entry in [square] brackets is the type of the C value(s) to be passed.
543
544 The characters space, tab, colon and comma are ignored in format strings (but
545 not within format units such as ``s#``). This can be used to make long format
546 strings a tad more readable.
547
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300548 ``s`` (:class:`str` or ``None``) [const char \*]
Victor Stinner2aa3af42010-06-18 23:59:45 +0000549 Convert a null-terminated C string to a Python :class:`str` object using ``'utf-8'``
Victor Stinner69e25fa2010-06-07 21:20:41 +0000550 encoding. If the C string pointer is *NULL*, ``None`` is used.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000551
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300552 ``s#`` (:class:`str` or ``None``) [const char \*, int]
Victor Stinner2aa3af42010-06-18 23:59:45 +0000553 Convert a C string and its length to a Python :class:`str` object using ``'utf-8'``
Victor Stinner69e25fa2010-06-07 21:20:41 +0000554 encoding. If the C string pointer is *NULL*, the length is ignored and
555 ``None`` is returned.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000556
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300557 ``y`` (:class:`bytes`) [const char \*]
Zachary Warea479b752014-01-31 12:06:14 -0600558 This converts a C string to a Python :class:`bytes` object. If the C
Benjamin Petersonffc94792008-10-21 21:10:07 +0000559 string pointer is *NULL*, ``None`` is returned.
560
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300561 ``y#`` (:class:`bytes`) [const char \*, int]
Benjamin Petersonffc94792008-10-21 21:10:07 +0000562 This converts a C string and its lengths to a Python object. If the C
563 string pointer is *NULL*, ``None`` is returned.
564
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300565 ``z`` (:class:`str` or ``None``) [const char \*]
Georg Brandl54a3faa2008-01-20 09:30:57 +0000566 Same as ``s``.
567
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300568 ``z#`` (:class:`str` or ``None``) [const char \*, int]
Georg Brandl54a3faa2008-01-20 09:30:57 +0000569 Same as ``s#``.
570
INADA Naoki42ec1902018-04-04 13:59:08 +0900571 ``u`` (:class:`str`) [const wchar_t \*]
572 Convert a null-terminated :c:type:`wchar_t` buffer of Unicode (UTF-16 or UCS-4)
573 data to a Python Unicode object. If the Unicode buffer pointer is *NULL*,
574 ``None`` is returned.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000575
INADA Naoki42ec1902018-04-04 13:59:08 +0900576 ``u#`` (:class:`str`) [const wchar_t \*, int]
577 Convert a Unicode (UTF-16 or UCS-4) data buffer and its length to a Python
Georg Brandl54a3faa2008-01-20 09:30:57 +0000578 Unicode object. If the Unicode buffer pointer is *NULL*, the length is ignored
579 and ``None`` is returned.
580
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300581 ``U`` (:class:`str` or ``None``) [const char \*]
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000582 Same as ``s``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000583
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300584 ``U#`` (:class:`str` or ``None``) [const char \*, int]
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000585 Same as ``s#``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000586
Victor Stinner69e25fa2010-06-07 21:20:41 +0000587 ``i`` (:class:`int`) [int]
Georg Brandl60203b42010-10-06 10:11:56 +0000588 Convert a plain C :c:type:`int` to a Python integer object.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000589
Victor Stinner69e25fa2010-06-07 21:20:41 +0000590 ``b`` (:class:`int`) [char]
Georg Brandl60203b42010-10-06 10:11:56 +0000591 Convert a plain C :c:type:`char` to a Python integer object.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000592
Victor Stinner69e25fa2010-06-07 21:20:41 +0000593 ``h`` (:class:`int`) [short int]
Georg Brandl60203b42010-10-06 10:11:56 +0000594 Convert a plain C :c:type:`short int` to a Python integer object.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000595
Victor Stinner69e25fa2010-06-07 21:20:41 +0000596 ``l`` (:class:`int`) [long int]
Georg Brandl60203b42010-10-06 10:11:56 +0000597 Convert a C :c:type:`long int` to a Python integer object.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000598
Victor Stinner69e25fa2010-06-07 21:20:41 +0000599 ``B`` (:class:`int`) [unsigned char]
Georg Brandl60203b42010-10-06 10:11:56 +0000600 Convert a C :c:type:`unsigned char` to a Python integer object.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000601
Victor Stinner69e25fa2010-06-07 21:20:41 +0000602 ``H`` (:class:`int`) [unsigned short int]
Georg Brandl60203b42010-10-06 10:11:56 +0000603 Convert a C :c:type:`unsigned short int` to a Python integer object.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000604
Victor Stinner69e25fa2010-06-07 21:20:41 +0000605 ``I`` (:class:`int`) [unsigned int]
Georg Brandl60203b42010-10-06 10:11:56 +0000606 Convert a C :c:type:`unsigned int` to a Python integer object.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000607
Victor Stinner69e25fa2010-06-07 21:20:41 +0000608 ``k`` (:class:`int`) [unsigned long]
Georg Brandl60203b42010-10-06 10:11:56 +0000609 Convert a C :c:type:`unsigned long` to a Python integer object.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000610
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700611 ``L`` (:class:`int`) [long long]
612 Convert a C :c:type:`long long` to a Python integer object.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000613
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700614 ``K`` (:class:`int`) [unsigned long long]
615 Convert a C :c:type:`unsigned long long` to a Python integer object.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000616
Victor Stinner69e25fa2010-06-07 21:20:41 +0000617 ``n`` (:class:`int`) [Py_ssize_t]
Georg Brandl60203b42010-10-06 10:11:56 +0000618 Convert a C :c:type:`Py_ssize_t` to a Python integer.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000619
Victor Stinner69e25fa2010-06-07 21:20:41 +0000620 ``c`` (:class:`bytes` of length 1) [char]
Georg Brandl60203b42010-10-06 10:11:56 +0000621 Convert a C :c:type:`int` representing a byte to a Python :class:`bytes` object of
Benjamin Petersona921fb02009-04-03 22:18:11 +0000622 length 1.
623
Victor Stinner69e25fa2010-06-07 21:20:41 +0000624 ``C`` (:class:`str` of length 1) [int]
Georg Brandl60203b42010-10-06 10:11:56 +0000625 Convert a C :c:type:`int` representing a character to Python :class:`str`
Victor Stinner69e25fa2010-06-07 21:20:41 +0000626 object of length 1.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000627
Victor Stinner69e25fa2010-06-07 21:20:41 +0000628 ``d`` (:class:`float`) [double]
Georg Brandl60203b42010-10-06 10:11:56 +0000629 Convert a C :c:type:`double` to a Python floating point number.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000630
Victor Stinner69e25fa2010-06-07 21:20:41 +0000631 ``f`` (:class:`float`) [float]
Georg Brandl60203b42010-10-06 10:11:56 +0000632 Convert a C :c:type:`float` to a Python floating point number.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000633
Victor Stinner69e25fa2010-06-07 21:20:41 +0000634 ``D`` (:class:`complex`) [Py_complex \*]
Georg Brandl60203b42010-10-06 10:11:56 +0000635 Convert a C :c:type:`Py_complex` structure to a Python complex number.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000636
637 ``O`` (object) [PyObject \*]
638 Pass a Python object untouched (except for its reference count, which is
639 incremented by one). If the object passed in is a *NULL* pointer, it is assumed
640 that this was caused because the call producing the argument found an error and
Georg Brandl60203b42010-10-06 10:11:56 +0000641 set an exception. Therefore, :c:func:`Py_BuildValue` will return *NULL* but won't
Georg Brandl54a3faa2008-01-20 09:30:57 +0000642 raise an exception. If no exception has been raised yet, :exc:`SystemError` is
643 set.
644
645 ``S`` (object) [PyObject \*]
646 Same as ``O``.
647
648 ``N`` (object) [PyObject \*]
649 Same as ``O``, except it doesn't increment the reference count on the object.
650 Useful when the object is created by a call to an object constructor in the
651 argument list.
652
653 ``O&`` (object) [*converter*, *anything*]
654 Convert *anything* to a Python object through a *converter* function. The
Georg Brandl60203b42010-10-06 10:11:56 +0000655 function is called with *anything* (which should be compatible with :c:type:`void
Georg Brandl54a3faa2008-01-20 09:30:57 +0000656 \*`) as its argument and should return a "new" Python object, or *NULL* if an
657 error occurred.
658
Victor Stinner69e25fa2010-06-07 21:20:41 +0000659 ``(items)`` (:class:`tuple`) [*matching-items*]
Georg Brandl54a3faa2008-01-20 09:30:57 +0000660 Convert a sequence of C values to a Python tuple with the same number of items.
661
Victor Stinner69e25fa2010-06-07 21:20:41 +0000662 ``[items]`` (:class:`list`) [*matching-items*]
Georg Brandl54a3faa2008-01-20 09:30:57 +0000663 Convert a sequence of C values to a Python list with the same number of items.
664
Victor Stinner69e25fa2010-06-07 21:20:41 +0000665 ``{items}`` (:class:`dict`) [*matching-items*]
Georg Brandl54a3faa2008-01-20 09:30:57 +0000666 Convert a sequence of C values to a Python dictionary. Each pair of consecutive
667 C values adds one item to the dictionary, serving as key and value,
668 respectively.
669
670 If there is an error in the format string, the :exc:`SystemError` exception is
671 set and *NULL* returned.
Benjamin Petersonda10d3b2009-01-01 00:23:30 +0000672
Georg Brandl60203b42010-10-06 10:11:56 +0000673.. c:function:: PyObject* Py_VaBuildValue(const char *format, va_list vargs)
Benjamin Petersonda10d3b2009-01-01 00:23:30 +0000674
Georg Brandl60203b42010-10-06 10:11:56 +0000675 Identical to :c:func:`Py_BuildValue`, except that it accepts a va_list
Benjamin Petersonda10d3b2009-01-01 00:23:30 +0000676 rather than a variable number of arguments.