@@ -229,12 +229,24 @@ There are three ways strings and buffers can be converted to C:
229
229
Numbers
230
230
-------
231
231
232
+ These formats allow representing Python numbers or single characters as C numbers.
233
+ Formats that require :class: `int `, :class: `float ` or :class: `complex ` can
234
+ also use the corresponding special methods :meth: `~object.__index__ `,
235
+ :meth: `~object.__float__ ` or :meth: `~object.__complex__ ` to convert
236
+ the Python object to the required type.
237
+
238
+ For signed integer formats, :exc: `OverflowError ` is raised if the value
239
+ is out of range for the C type.
240
+ For unsigned integer formats, no range checking is done --- the
241
+ most significant bits are silently truncated when the receiving field is too
242
+ small to receive the value.
243
+
232
244
``b `` (:class: `int `) [unsigned char]
233
- Convert a nonnegative Python integer to an unsigned tiny int , stored in a C
245
+ Convert a nonnegative Python integer to an unsigned tiny integer , stored in a C
234
246
:c:expr: `unsigned char `.
235
247
236
248
``B `` (:class: `int `) [unsigned char]
237
- Convert a Python integer to a tiny int without overflow checking, stored in a C
249
+ Convert a Python integer to a tiny integer without overflow checking, stored in a C
238
250
:c:expr: `unsigned char `.
239
251
240
252
``h `` (:class: `int `) [short int]
@@ -307,7 +319,7 @@ Other objects
307
319
308
320
.. _o_ampersand :
309
321
310
- ``O& `` (object) [*converter *, *anything *]
322
+ ``O& `` (object) [*converter *, *address *]
311
323
Convert a Python object to a C variable through a *converter * function. This
312
324
takes two arguments: the first is a function, the second is the address of a C
313
325
variable (of arbitrary type), converted to :c:expr: `void * `. The *converter *
@@ -321,14 +333,20 @@ Other objects
321
333
the conversion has failed. When the conversion fails, the *converter * function
322
334
should raise an exception and leave the content of *address * unmodified.
323
335
324
- If the *converter * returns ``Py_CLEANUP_SUPPORTED ``, it may get called a
336
+ .. c :macro :: Py_CLEANUP_SUPPORTED
337
+ :no-typesetting:
338
+
339
+ If the *converter * returns :c:macro: `!Py_CLEANUP_SUPPORTED `, it may get called a
325
340
second time if the argument parsing eventually fails, giving the converter a
326
341
chance to release any memory that it had already allocated. In this second
327
342
call, the *object * parameter will be ``NULL ``; *address * will have the same value
328
343
as in the original call.
329
344
345
+ Examples of converters: :c:func: `PyUnicode_FSConverter ` and
346
+ :c:func: `PyUnicode_FSDecoder `.
347
+
330
348
.. versionchanged :: 3.1
331
- `` Py_CLEANUP_SUPPORTED ` ` was added.
349
+ :c:macro: ` ! Py_CLEANUP_SUPPORTED ` was added.
332
350
333
351
``p `` (:class: `bool `) [int]
334
352
Tests the value passed in for truth (a boolean **p **\ redicate) and converts
@@ -344,12 +362,6 @@ Other objects
344
362
in *items *. The C arguments must correspond to the individual format units in
345
363
*items *. Format units for sequences may be nested.
346
364
347
- It is possible to pass "long" integers (integers whose value exceeds the
348
- platform's :c:macro: `LONG_MAX `) however no proper range checking is done --- the
349
- most significant bits are silently truncated when the receiving field is too
350
- small to receive the value (actually, the semantics are inherited from downcasts
351
- in C --- your mileage may vary).
352
-
353
365
A few other characters have a meaning in a format string. These may not occur
354
366
inside nested parentheses. They are:
355
367
0 commit comments