@@ -3,7 +3,7 @@ Title: C API to import-export Python integers
33Author: Sergey B Kirpichev <
[email protected] >,
44 Victor Stinner <
[email protected] >
55Discussions-To: https://discuss.python.org/t/63895
6- Status: Accepted
6+ Status: Final
77Type: Standards Track
88Created: 13-Sep-2024
99Python-Version: 3.14
@@ -67,11 +67,13 @@ functions.
6767
6868 .. c :member :: uint8_t bits_per_digit
6969
70- Bits per digit.
70+ Bits per digit. For example, a 15 bit digit means that bits 0-14
71+ contain meaningful information.
7172
7273 .. c :member :: uint8_t digit_size
7374
74- Digit size in bytes.
75+ Digit size in bytes. For example, a 15 bit digit will require at least
76+ 2 bytes.
7577
7678 .. c :member :: int8_t digits_order
7779
@@ -85,7 +87,7 @@ functions.
8587 Digit endianness:
8688
8789 - ``1 `` for most significant byte first (big endian)
88- - ``-1 `` for least significant first (little endian)
90+ - ``-1 `` for least significant byte first (little endian)
8991
9092
9193.. c :function :: const PyLongLayout* PyLong_GetNativeLayout (void)
@@ -110,11 +112,9 @@ Export API
110112 There are two cases:
111113
112114 * If :c:member:`digits` is ``NULL``, only use the :c:member:`value` member.
113- Calling :c:func:`PyLong_FreeExport` is optional in this case.
114115 * If :c:member:`digits` is not ``NULL``, use :c:member:`negative`,
115116 :c:member:`ndigits` and :c:member:`digits` members.
116- Calling :c:func:`PyLong_FreeExport` is mandatory in this case.
117-
117+
118118 .. c:member:: int64_t value
119119
120120 The native integer value of the exported :class:`int` object.
@@ -145,11 +145,18 @@ If :c:member:`PyLongExport.digits` is not ``NULL``, a private field of the
145145
146146 Export a Python :class: `int ` object.
147147
148- On success, set *\* export_long * and return 0.
148+ *export_long * must point to a :c:struct: `PyLongExport ` structure allocated
149+ by the caller. It must not be ``NULL ``.
150+
151+ On success, fill in *\* export_long * and return 0.
149152 On error, set an exception and return -1.
150153
151- If *export_long->digits * is not ``NULL ``, :c:func: `PyLong_FreeExport ` must be
152- called when the export is no longer needed.
154+ :c:func: `PyLong_FreeExport ` must be called when the export is no longer
155+ needed.
156+
157+ .. impl-detail ::
158+ This function always succeeds if *obj * is a Python :class: `int ` object
159+ or a subclass.
153160
154161
155162On CPython 3.14, no memory copy is needed in :c:func: `PyLong_Export `, it's just
@@ -160,12 +167,15 @@ a thin wrapper to expose Python :class:`int` internal digits array.
160167
161168 Release the export *export_long * created by :c:func: `PyLong_Export `.
162169
170+ .. impl-detail ::
171+ Calling :c:func: `PyLong_FreeExport ` is optional if *export_long->digits *
172+ is ``NULL ``.
173+
163174
164175Import API
165176----------
166177
167- The :c:type: `PyLongWriter ` API can be used to import an integer:
168- create a Python :class: `int ` object from a digits array.
178+ The :c:type: `PyLongWriter ` API can be used to import an integer.
169179
170180.. c :struct :: PyLongWriter
171181
@@ -187,12 +197,20 @@ create a Python :class:`int` object from a digits array.
187197 *ndigits * is the number of digits in the *digits * array. It must be
188198 greater than 0.
189199
190- The caller can either initialize the array of digits *digits * and then
191- either call :c:func: `PyLongWriter_Finish ` to get a Python :class: `int ` or
192- :c:func: `PyLongWriter_Discard ` to destroy the writer instance. Digits must
193- be in the range [``0 ``; ``(1 << bits_per_digit) - 1 ``] (where the
194- :c:struct: `~PyLongLayout.bits_per_digit ` is the number of bits per digit).
195- The unused most-significant digits must be set to ``0``.
200+ *digits * must not be NULL.
201+
202+ After a successful call to this function, the caller should fill in the
203+ array of digits *digits * and then call :c:func: `PyLongWriter_Finish ` to get
204+ a Python :class: `int `.
205+ The layout of *digits * is described by :c:func: `PyLong_GetNativeLayout `.
206+
207+ Digits must be in the range [``0 ``; ``(1 << bits_per_digit) - 1 ``]
208+ (where the :c:struct: `~PyLongLayout.bits_per_digit ` is the number of bits
209+ per digit).
210+ Any unused most significant digits must be set to ``0``.
211+
212+ Alternately, call :c:func:`PyLongWriter_Discard` to destroy the writer
213+ instance without creating an :class:`~int` object.
196214
197215
198216On CPython 3.14, the :c:func:`PyLongWriter_Create` implementation is a thin
@@ -209,14 +227,16 @@ wrapper to the private :c:func:`!_PyLong_New()` function.
209227 The function takes care of normalizing the digits and converts the
210228 object to a compact integer if needed.
211229
212- The writer instance is invalid after the call.
230+ The writer instance and the * digits * array are invalid after the call.
213231
214232
215233.. c :function :: void PyLongWriter_Discard (PyLongWriter *writer)
216234
217235 Discard a :c:type: `PyLongWriter ` created by :c:func: `PyLongWriter_Create `.
218236
219- The writer instance is invalid after the call.
237+ *writer * must not be ``NULL ``.
238+
239+ The writer instance and the *digits * array are invalid after the call.
220240
221241
222242Optimize import for small integers
0 commit comments