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