|
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 |
|
17 A format string consists of zero or more "format units." A format unit |
|
18 describes one Python object; it is usually a single character or a parenthesized |
|
19 sequence of format units. With a few exceptions, a format unit that is not a |
|
20 parenthesized sequence normally corresponds to a single address argument to |
|
21 these functions. In the following description, the quoted form is the format |
|
22 unit; the entry in (round) parentheses is the Python object type that matches |
|
23 the format unit; and the entry in [square] brackets is the type of the C |
|
24 variable(s) whose address should be passed. |
|
25 |
|
26 ``s`` (string or Unicode object) [const char \*] |
|
27 Convert a Python string or Unicode object to a C pointer to a character string. |
|
28 You must not provide storage for the string itself; a pointer to an existing |
|
29 string is stored into the character pointer variable whose address you pass. |
|
30 The C string is NUL-terminated. The Python string must not contain embedded NUL |
|
31 bytes; if it does, a :exc:`TypeError` exception is raised. Unicode objects are |
|
32 converted to C strings using the default encoding. If this conversion fails, a |
|
33 :exc:`UnicodeError` is raised. |
|
34 |
|
35 ``s#`` (string, Unicode or any read buffer compatible object) [const char \*, int (or :ctype:`Py_ssize_t`, see below)] |
|
36 This variant on ``s`` stores into two C variables, the first one a pointer to a |
|
37 character string, the second one its length. In this case the Python string may |
|
38 contain embedded null bytes. Unicode objects pass back a pointer to the default |
|
39 encoded string version of the object if such a conversion is possible. All |
|
40 other read-buffer compatible objects pass back a reference to the raw internal |
|
41 data representation. |
|
42 |
|
43 Starting with Python 2.5 the type of the length argument can be |
|
44 controlled by defining the macro :cmacro:`PY_SSIZE_T_CLEAN` before |
|
45 including :file:`Python.h`. If the macro is defined, length is a |
|
46 :ctype:`Py_ssize_t` rather than an int. |
|
47 |
|
48 ``s*`` (string, Unicode, or any buffer compatible object) [Py_buffer \*] |
|
49 Similar to ``s#``, this code fills a Py_buffer structure provided by the caller. |
|
50 The buffer gets locked, so that the caller can subsequently use the buffer even |
|
51 inside a ``Py_BEGIN_ALLOW_THREADS`` block; the caller is responsible for calling |
|
52 ``PyBuffer_Release`` with the structure after it has processed the data. |
|
53 |
|
54 .. versionadded:: 2.6 |
|
55 |
|
56 ``z`` (string or ``None``) [const char \*] |
|
57 Like ``s``, but the Python object may also be ``None``, in which case the C |
|
58 pointer is set to *NULL*. |
|
59 |
|
60 ``z#`` (string or ``None`` or any read buffer compatible object) [const char \*, int] |
|
61 This is to ``s#`` as ``z`` is to ``s``. |
|
62 |
|
63 ``z*`` (string or ``None`` or any buffer compatible object) [Py_buffer*] |
|
64 This is to ``s*`` as ``z`` is to ``s``. |
|
65 |
|
66 .. versionadded:: 2.6 |
|
67 |
|
68 ``u`` (Unicode object) [Py_UNICODE \*] |
|
69 Convert a Python Unicode object to a C pointer to a NUL-terminated buffer of |
|
70 16-bit Unicode (UTF-16) data. As with ``s``, there is no need to provide |
|
71 storage for the Unicode data buffer; a pointer to the existing Unicode data is |
|
72 stored into the :ctype:`Py_UNICODE` pointer variable whose address you pass. |
|
73 |
|
74 ``u#`` (Unicode object) [Py_UNICODE \*, int] |
|
75 This variant on ``u`` stores into two C variables, the first one a pointer to a |
|
76 Unicode data buffer, the second one its length. Non-Unicode objects are handled |
|
77 by interpreting their read-buffer pointer as pointer to a :ctype:`Py_UNICODE` |
|
78 array. |
|
79 |
|
80 ``es`` (string, Unicode object or character buffer compatible object) [const char \*encoding, char \*\*buffer] |
|
81 This variant on ``s`` is used for encoding Unicode and objects convertible to |
|
82 Unicode into a character buffer. It only works for encoded data without embedded |
|
83 NUL bytes. |
|
84 |
|
85 This format requires two arguments. The first is only used as input, and |
|
86 must be a :ctype:`const char\*` which points to the name of an encoding as a |
|
87 NUL-terminated string, or *NULL*, in which case the default encoding is used. |
|
88 An exception is raised if the named encoding is not known to Python. The |
|
89 second argument must be a :ctype:`char\*\*`; the value of the pointer it |
|
90 references will be set to a buffer with the contents of the argument text. |
|
91 The text will be encoded in the encoding specified by the first argument. |
|
92 |
|
93 :cfunc:`PyArg_ParseTuple` will allocate a buffer of the needed size, copy the |
|
94 encoded data into this buffer and adjust *\*buffer* to reference the newly |
|
95 allocated storage. The caller is responsible for calling :cfunc:`PyMem_Free` to |
|
96 free the allocated buffer after use. |
|
97 |
|
98 ``et`` (string, Unicode object or character buffer compatible object) [const char \*encoding, char \*\*buffer] |
|
99 Same as ``es`` except that 8-bit string objects are passed through without |
|
100 recoding them. Instead, the implementation assumes that the string object uses |
|
101 the encoding passed in as parameter. |
|
102 |
|
103 ``es#`` (string, Unicode object or character buffer compatible object) [const char \*encoding, char \*\*buffer, int \*buffer_length] |
|
104 This variant on ``s#`` is used for encoding Unicode and objects convertible to |
|
105 Unicode into a character buffer. Unlike the ``es`` format, this variant allows |
|
106 input data which contains NUL characters. |
|
107 |
|
108 It requires three arguments. The first is only used as input, and must be a |
|
109 :ctype:`const char\*` which points to the name of an encoding as a |
|
110 NUL-terminated string, or *NULL*, in which case the default encoding is used. |
|
111 An exception is raised if the named encoding is not known to Python. The |
|
112 second argument must be a :ctype:`char\*\*`; the value of the pointer it |
|
113 references will be set to a buffer with the contents of the argument text. |
|
114 The text will be encoded in the encoding specified by the first argument. |
|
115 The third argument must be a pointer to an integer; the referenced integer |
|
116 will be set to the number of bytes in the output buffer. |
|
117 |
|
118 There are two modes of operation: |
|
119 |
|
120 If *\*buffer* points a *NULL* pointer, the function will allocate a buffer of |
|
121 the needed size, copy the encoded data into this buffer and set *\*buffer* to |
|
122 reference the newly allocated storage. The caller is responsible for calling |
|
123 :cfunc:`PyMem_Free` to free the allocated buffer after usage. |
|
124 |
|
125 If *\*buffer* points to a non-*NULL* pointer (an already allocated buffer), |
|
126 :cfunc:`PyArg_ParseTuple` will use this location as the buffer and interpret the |
|
127 initial value of *\*buffer_length* as the buffer size. It will then copy the |
|
128 encoded data into the buffer and NUL-terminate it. If the buffer is not large |
|
129 enough, a :exc:`ValueError` will be set. |
|
130 |
|
131 In both cases, *\*buffer_length* is set to the length of the encoded data |
|
132 without the trailing NUL byte. |
|
133 |
|
134 ``et#`` (string, Unicode object or character buffer compatible object) [const char \*encoding, char \*\*buffer] |
|
135 Same as ``es#`` except that string objects are passed through without recoding |
|
136 them. Instead, the implementation assumes that the string object uses the |
|
137 encoding passed in as parameter. |
|
138 |
|
139 ``b`` (integer) [char] |
|
140 Convert a Python integer to a tiny int, stored in a C :ctype:`char`. |
|
141 |
|
142 ``B`` (integer) [unsigned char] |
|
143 Convert a Python integer to a tiny int without overflow checking, stored in a C |
|
144 :ctype:`unsigned char`. |
|
145 |
|
146 .. versionadded:: 2.3 |
|
147 |
|
148 ``h`` (integer) [short int] |
|
149 Convert a Python integer to a C :ctype:`short int`. |
|
150 |
|
151 ``H`` (integer) [unsigned short int] |
|
152 Convert a Python integer to a C :ctype:`unsigned short int`, without overflow |
|
153 checking. |
|
154 |
|
155 .. versionadded:: 2.3 |
|
156 |
|
157 ``i`` (integer) [int] |
|
158 Convert a Python integer to a plain C :ctype:`int`. |
|
159 |
|
160 ``I`` (integer) [unsigned int] |
|
161 Convert a Python integer to a C :ctype:`unsigned int`, without overflow |
|
162 checking. |
|
163 |
|
164 .. versionadded:: 2.3 |
|
165 |
|
166 ``l`` (integer) [long int] |
|
167 Convert a Python integer to a C :ctype:`long int`. |
|
168 |
|
169 ``k`` (integer) [unsigned long] |
|
170 Convert a Python integer or long integer to a C :ctype:`unsigned long` without |
|
171 overflow checking. |
|
172 |
|
173 .. versionadded:: 2.3 |
|
174 |
|
175 ``L`` (integer) [PY_LONG_LONG] |
|
176 Convert a Python integer to a C :ctype:`long long`. This format is only |
|
177 available on platforms that support :ctype:`long long` (or :ctype:`_int64` on |
|
178 Windows). |
|
179 |
|
180 ``K`` (integer) [unsigned PY_LONG_LONG] |
|
181 Convert a Python integer or long integer to a C :ctype:`unsigned long long` |
|
182 without overflow checking. This format is only available on platforms that |
|
183 support :ctype:`unsigned long long` (or :ctype:`unsigned _int64` on Windows). |
|
184 |
|
185 .. versionadded:: 2.3 |
|
186 |
|
187 ``n`` (integer) [Py_ssize_t] |
|
188 Convert a Python integer or long integer to a C :ctype:`Py_ssize_t`. |
|
189 |
|
190 .. versionadded:: 2.5 |
|
191 |
|
192 ``c`` (string of length 1) [char] |
|
193 Convert a Python character, represented as a string of length 1, to a C |
|
194 :ctype:`char`. |
|
195 |
|
196 ``f`` (float) [float] |
|
197 Convert a Python floating point number to a C :ctype:`float`. |
|
198 |
|
199 ``d`` (float) [double] |
|
200 Convert a Python floating point number to a C :ctype:`double`. |
|
201 |
|
202 ``D`` (complex) [Py_complex] |
|
203 Convert a Python complex number to a C :ctype:`Py_complex` structure. |
|
204 |
|
205 ``O`` (object) [PyObject \*] |
|
206 Store a Python object (without any conversion) in a C object pointer. The C |
|
207 program thus receives the actual object that was passed. The object's reference |
|
208 count is not increased. The pointer stored is not *NULL*. |
|
209 |
|
210 ``O!`` (object) [*typeobject*, PyObject \*] |
|
211 Store a Python object in a C object pointer. This is similar to ``O``, but |
|
212 takes two C arguments: the first is the address of a Python type object, the |
|
213 second is the address of the C variable (of type :ctype:`PyObject\*`) into which |
|
214 the object pointer is stored. If the Python object does not have the required |
|
215 type, :exc:`TypeError` is raised. |
|
216 |
|
217 ``O&`` (object) [*converter*, *anything*] |
|
218 Convert a Python object to a C variable through a *converter* function. This |
|
219 takes two arguments: the first is a function, the second is the address of a C |
|
220 variable (of arbitrary type), converted to :ctype:`void \*`. The *converter* |
|
221 function in turn is called as follows:: |
|
222 |
|
223 status = converter(object, address); |
|
224 |
|
225 where *object* is the Python object to be converted and *address* is the |
|
226 :ctype:`void\*` argument that was passed to the :cfunc:`PyArg_Parse\*` function. |
|
227 The returned *status* should be ``1`` for a successful conversion and ``0`` if |
|
228 the conversion has failed. When the conversion fails, the *converter* function |
|
229 should raise an exception and leave the content of *address* unmodified. |
|
230 |
|
231 ``S`` (string) [PyStringObject \*] |
|
232 Like ``O`` but requires that the Python object is a string object. Raises |
|
233 :exc:`TypeError` if the object is not a string object. The C variable may also |
|
234 be declared as :ctype:`PyObject\*`. |
|
235 |
|
236 ``U`` (Unicode string) [PyUnicodeObject \*] |
|
237 Like ``O`` but requires that the Python object is a Unicode object. Raises |
|
238 :exc:`TypeError` if the object is not a Unicode object. The C variable may also |
|
239 be declared as :ctype:`PyObject\*`. |
|
240 |
|
241 ``t#`` (read-only character buffer) [char \*, int] |
|
242 Like ``s#``, but accepts any object which implements the read-only buffer |
|
243 interface. The :ctype:`char\*` variable is set to point to the first byte of |
|
244 the buffer, and the :ctype:`int` is set to the length of the buffer. Only |
|
245 single-segment buffer objects are accepted; :exc:`TypeError` is raised for all |
|
246 others. |
|
247 |
|
248 ``w`` (read-write character buffer) [char \*] |
|
249 Similar to ``s``, but accepts any object which implements the read-write buffer |
|
250 interface. The caller must determine the length of the buffer by other means, |
|
251 or use ``w#`` instead. Only single-segment buffer objects are accepted; |
|
252 :exc:`TypeError` is raised for all others. |
|
253 |
|
254 ``w#`` (read-write character buffer) [char \*, Py_ssize_t] |
|
255 Like ``s#``, but accepts any object which implements the read-write buffer |
|
256 interface. The :ctype:`char \*` variable is set to point to the first byte of |
|
257 the buffer, and the :ctype:`int` is set to the length of the buffer. Only |
|
258 single-segment buffer objects are accepted; :exc:`TypeError` is raised for all |
|
259 others. |
|
260 |
|
261 ``w*`` (read-write byte-oriented buffer) [Py_buffer \*] |
|
262 This is to ``w`` what ``s*`` is to ``s``. |
|
263 .. versionadded:: 2.6 |
|
264 |
|
265 ``(items)`` (tuple) [*matching-items*] |
|
266 The object must be a Python sequence whose length is the number of format units |
|
267 in *items*. The C arguments must correspond to the individual format units in |
|
268 *items*. Format units for sequences may be nested. |
|
269 |
|
270 .. note:: |
|
271 |
|
272 Prior to Python version 1.5.2, this format specifier only accepted a tuple |
|
273 containing the individual parameters, not an arbitrary sequence. Code which |
|
274 previously caused :exc:`TypeError` to be raised here may now proceed without an |
|
275 exception. This is not expected to be a problem for existing code. |
|
276 |
|
277 It is possible to pass Python long integers where integers are requested; |
|
278 however no proper range checking is done --- the most significant bits are |
|
279 silently truncated when the receiving field is too small to receive the value |
|
280 (actually, the semantics are inherited from downcasts in C --- your mileage may |
|
281 vary). |
|
282 |
|
283 A few other characters have a meaning in a format string. These may not occur |
|
284 inside nested parentheses. They are: |
|
285 |
|
286 ``|`` |
|
287 Indicates that the remaining arguments in the Python argument list are optional. |
|
288 The C variables corresponding to optional arguments should be initialized to |
|
289 their default value --- when an optional argument is not specified, |
|
290 :cfunc:`PyArg_ParseTuple` does not touch the contents of the corresponding C |
|
291 variable(s). |
|
292 |
|
293 ``:`` |
|
294 The list of format units ends here; the string after the colon is used as the |
|
295 function name in error messages (the "associated value" of the exception that |
|
296 :cfunc:`PyArg_ParseTuple` raises). |
|
297 |
|
298 ``;`` |
|
299 The list of format units ends here; the string after the semicolon is used as |
|
300 the error message *instead* of the default error message. Clearly, ``:`` and |
|
301 ``;`` mutually exclude each other. |
|
302 |
|
303 Note that any Python object references which are provided to the caller are |
|
304 *borrowed* references; do not decrement their reference count! |
|
305 |
|
306 Additional arguments passed to these functions must be addresses of variables |
|
307 whose type is determined by the format string; these are used to store values |
|
308 from the input tuple. There are a few cases, as described in the list of format |
|
309 units above, where these parameters are used as input values; they should match |
|
310 what is specified for the corresponding format unit in that case. |
|
311 |
|
312 For the conversion to succeed, the *arg* object must match the format |
|
313 and the format must be exhausted. On success, the |
|
314 :cfunc:`PyArg_Parse\*` functions return true, otherwise they return |
|
315 false and raise an appropriate exception. When the |
|
316 :cfunc:`PyArg_Parse\*` functions fail due to conversion failure in one |
|
317 of the format units, the variables at the addresses corresponding to that |
|
318 and the following format units are left untouched. |
|
319 |
|
320 |
|
321 .. cfunction:: int PyArg_ParseTuple(PyObject *args, const char *format, ...) |
|
322 |
|
323 Parse the parameters of a function that takes only positional parameters into |
|
324 local variables. Returns true on success; on failure, it returns false and |
|
325 raises the appropriate exception. |
|
326 |
|
327 |
|
328 .. cfunction:: int PyArg_VaParse(PyObject *args, const char *format, va_list vargs) |
|
329 |
|
330 Identical to :cfunc:`PyArg_ParseTuple`, except that it accepts a va_list rather |
|
331 than a variable number of arguments. |
|
332 |
|
333 |
|
334 .. cfunction:: int PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], ...) |
|
335 |
|
336 Parse the parameters of a function that takes both positional and keyword |
|
337 parameters into local variables. Returns true on success; on failure, it |
|
338 returns false and raises the appropriate exception. |
|
339 |
|
340 |
|
341 .. cfunction:: int PyArg_VaParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], va_list vargs) |
|
342 |
|
343 Identical to :cfunc:`PyArg_ParseTupleAndKeywords`, except that it accepts a |
|
344 va_list rather than a variable number of arguments. |
|
345 |
|
346 |
|
347 .. cfunction:: int PyArg_Parse(PyObject *args, const char *format, ...) |
|
348 |
|
349 Function used to deconstruct the argument lists of "old-style" functions --- |
|
350 these are functions which use the :const:`METH_OLDARGS` parameter parsing |
|
351 method. This is not recommended for use in parameter parsing in new code, and |
|
352 most code in the standard interpreter has been modified to no longer use this |
|
353 for that purpose. It does remain a convenient way to decompose other tuples, |
|
354 however, and may continue to be used for that purpose. |
|
355 |
|
356 |
|
357 .. cfunction:: int PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...) |
|
358 |
|
359 A simpler form of parameter retrieval which does not use a format string to |
|
360 specify the types of the arguments. Functions which use this method to retrieve |
|
361 their parameters should be declared as :const:`METH_VARARGS` in function or |
|
362 method tables. The tuple containing the actual parameters should be passed as |
|
363 *args*; it must actually be a tuple. The length of the tuple must be at least |
|
364 *min* and no more than *max*; *min* and *max* may be equal. Additional |
|
365 arguments must be passed to the function, each of which should be a pointer to a |
|
366 :ctype:`PyObject\*` variable; these will be filled in with the values from |
|
367 *args*; they will contain borrowed references. The variables which correspond |
|
368 to optional parameters not given by *args* will not be filled in; these should |
|
369 be initialized by the caller. This function returns true on success and false if |
|
370 *args* is not a tuple or contains the wrong number of elements; an exception |
|
371 will be set if there was a failure. |
|
372 |
|
373 This is an example of the use of this function, taken from the sources for the |
|
374 :mod:`_weakref` helper module for weak references:: |
|
375 |
|
376 static PyObject * |
|
377 weakref_ref(PyObject *self, PyObject *args) |
|
378 { |
|
379 PyObject *object; |
|
380 PyObject *callback = NULL; |
|
381 PyObject *result = NULL; |
|
382 |
|
383 if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) { |
|
384 result = PyWeakref_NewRef(object, callback); |
|
385 } |
|
386 return result; |
|
387 } |
|
388 |
|
389 The call to :cfunc:`PyArg_UnpackTuple` in this example is entirely equivalent to |
|
390 this call to :cfunc:`PyArg_ParseTuple`:: |
|
391 |
|
392 PyArg_ParseTuple(args, "O|O:ref", &object, &callback) |
|
393 |
|
394 .. versionadded:: 2.2 |
|
395 |
|
396 |
|
397 .. cfunction:: PyObject* Py_BuildValue(const char *format, ...) |
|
398 |
|
399 Create a new value based on a format string similar to those accepted by the |
|
400 :cfunc:`PyArg_Parse\*` family of functions and a sequence of values. Returns |
|
401 the value or *NULL* in the case of an error; an exception will be raised if |
|
402 *NULL* is returned. |
|
403 |
|
404 :cfunc:`Py_BuildValue` does not always build a tuple. It builds a tuple only if |
|
405 its format string contains two or more format units. If the format string is |
|
406 empty, it returns ``None``; if it contains exactly one format unit, it returns |
|
407 whatever object is described by that format unit. To force it to return a tuple |
|
408 of size 0 or one, parenthesize the format string. |
|
409 |
|
410 When memory buffers are passed as parameters to supply data to build objects, as |
|
411 for the ``s`` and ``s#`` formats, the required data is copied. Buffers provided |
|
412 by the caller are never referenced by the objects created by |
|
413 :cfunc:`Py_BuildValue`. In other words, if your code invokes :cfunc:`malloc` |
|
414 and passes the allocated memory to :cfunc:`Py_BuildValue`, your code is |
|
415 responsible for calling :cfunc:`free` for that memory once |
|
416 :cfunc:`Py_BuildValue` returns. |
|
417 |
|
418 In the following description, the quoted form is the format unit; the entry in |
|
419 (round) parentheses is the Python object type that the format unit will return; |
|
420 and the entry in [square] brackets is the type of the C value(s) to be passed. |
|
421 |
|
422 The characters space, tab, colon and comma are ignored in format strings (but |
|
423 not within format units such as ``s#``). This can be used to make long format |
|
424 strings a tad more readable. |
|
425 |
|
426 ``s`` (string) [char \*] |
|
427 Convert a null-terminated C string to a Python object. If the C string pointer |
|
428 is *NULL*, ``None`` is used. |
|
429 |
|
430 ``s#`` (string) [char \*, int] |
|
431 Convert a C string and its length to a Python object. If the C string pointer |
|
432 is *NULL*, the length is ignored and ``None`` is returned. |
|
433 |
|
434 ``z`` (string or ``None``) [char \*] |
|
435 Same as ``s``. |
|
436 |
|
437 ``z#`` (string or ``None``) [char \*, int] |
|
438 Same as ``s#``. |
|
439 |
|
440 ``u`` (Unicode string) [Py_UNICODE \*] |
|
441 Convert a null-terminated buffer of Unicode (UCS-2 or UCS-4) data to a Python |
|
442 Unicode object. If the Unicode buffer pointer is *NULL*, ``None`` is returned. |
|
443 |
|
444 ``u#`` (Unicode string) [Py_UNICODE \*, int] |
|
445 Convert a Unicode (UCS-2 or UCS-4) data buffer and its length to a Python |
|
446 Unicode object. If the Unicode buffer pointer is *NULL*, the length is ignored |
|
447 and ``None`` is returned. |
|
448 |
|
449 ``i`` (integer) [int] |
|
450 Convert a plain C :ctype:`int` to a Python integer object. |
|
451 |
|
452 ``b`` (integer) [char] |
|
453 Convert a plain C :ctype:`char` to a Python integer object. |
|
454 |
|
455 ``h`` (integer) [short int] |
|
456 Convert a plain C :ctype:`short int` to a Python integer object. |
|
457 |
|
458 ``l`` (integer) [long int] |
|
459 Convert a C :ctype:`long int` to a Python integer object. |
|
460 |
|
461 ``B`` (integer) [unsigned char] |
|
462 Convert a C :ctype:`unsigned char` to a Python integer object. |
|
463 |
|
464 ``H`` (integer) [unsigned short int] |
|
465 Convert a C :ctype:`unsigned short int` to a Python integer object. |
|
466 |
|
467 ``I`` (integer/long) [unsigned int] |
|
468 Convert a C :ctype:`unsigned int` to a Python integer object or a Python long |
|
469 integer object, if it is larger than ``sys.maxint``. |
|
470 |
|
471 ``k`` (integer/long) [unsigned long] |
|
472 Convert a C :ctype:`unsigned long` to a Python integer object or a Python long |
|
473 integer object, if it is larger than ``sys.maxint``. |
|
474 |
|
475 ``L`` (long) [PY_LONG_LONG] |
|
476 Convert a C :ctype:`long long` to a Python long integer object. Only available |
|
477 on platforms that support :ctype:`long long`. |
|
478 |
|
479 ``K`` (long) [unsigned PY_LONG_LONG] |
|
480 Convert a C :ctype:`unsigned long long` to a Python long integer object. Only |
|
481 available on platforms that support :ctype:`unsigned long long`. |
|
482 |
|
483 ``n`` (int) [Py_ssize_t] |
|
484 Convert a C :ctype:`Py_ssize_t` to a Python integer or long integer. |
|
485 |
|
486 .. versionadded:: 2.5 |
|
487 |
|
488 ``c`` (string of length 1) [char] |
|
489 Convert a C :ctype:`int` representing a character to a Python string of length |
|
490 1. |
|
491 |
|
492 ``d`` (float) [double] |
|
493 Convert a C :ctype:`double` to a Python floating point number. |
|
494 |
|
495 ``f`` (float) [float] |
|
496 Same as ``d``. |
|
497 |
|
498 ``D`` (complex) [Py_complex \*] |
|
499 Convert a C :ctype:`Py_complex` structure to a Python complex number. |
|
500 |
|
501 ``O`` (object) [PyObject \*] |
|
502 Pass a Python object untouched (except for its reference count, which is |
|
503 incremented by one). If the object passed in is a *NULL* pointer, it is assumed |
|
504 that this was caused because the call producing the argument found an error and |
|
505 set an exception. Therefore, :cfunc:`Py_BuildValue` will return *NULL* but won't |
|
506 raise an exception. If no exception has been raised yet, :exc:`SystemError` is |
|
507 set. |
|
508 |
|
509 ``S`` (object) [PyObject \*] |
|
510 Same as ``O``. |
|
511 |
|
512 ``N`` (object) [PyObject \*] |
|
513 Same as ``O``, except it doesn't increment the reference count on the object. |
|
514 Useful when the object is created by a call to an object constructor in the |
|
515 argument list. |
|
516 |
|
517 ``O&`` (object) [*converter*, *anything*] |
|
518 Convert *anything* to a Python object through a *converter* function. The |
|
519 function is called with *anything* (which should be compatible with :ctype:`void |
|
520 \*`) as its argument and should return a "new" Python object, or *NULL* if an |
|
521 error occurred. |
|
522 |
|
523 ``(items)`` (tuple) [*matching-items*] |
|
524 Convert a sequence of C values to a Python tuple with the same number of items. |
|
525 |
|
526 ``[items]`` (list) [*matching-items*] |
|
527 Convert a sequence of C values to a Python list with the same number of items. |
|
528 |
|
529 ``{items}`` (dictionary) [*matching-items*] |
|
530 Convert a sequence of C values to a Python dictionary. Each pair of consecutive |
|
531 C values adds one item to the dictionary, serving as key and value, |
|
532 respectively. |
|
533 |
|
534 If there is an error in the format string, the :exc:`SystemError` exception is |
|
535 set and *NULL* returned. |