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