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