-
-
Notifications
You must be signed in to change notification settings - Fork 32.9k
gh-102471, PEP 757: Add PyLong import and export API #121339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
f4fdbf2
c2e568e
f0d9525
b19764f
6f7fd11
080e079
1a7902f
b70a6dd
07552a7
0d0f942
762c33a
20be7a3
d92bf1e
b3b02a2
caca2d7
4221a49
37b1d49
d70a121
4aa25f6
90973d4
5d3e224
c7d7cb2
a3d601a
c049268
06b196b
3e8d296
86c68c2
a8fd669
a04f9d0
b2be94a
ca98ad1
167d75e
5e53a5b
c24789f
0422f9d
a529a48
3db44f3
1d2863e
d663511
816798d
033bd65
36b87d4
94d852e
a72ff83
53d584b
577598a
b08cd55
eaebef3
03248c7
0a26f97
88a62fe
45517ab
92007d1
6d3cb80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -597,10 +597,172 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate. | |||||||||||||||
Exactly what values are considered compact is an implementation detail | ||||||||||||||||
and is subject to change. | ||||||||||||||||
|
||||||||||||||||
.. versionadded:: 3.12 | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
.. c:function:: Py_ssize_t PyUnstable_Long_CompactValue(const PyLongObject* op) | ||||||||||||||||
|
||||||||||||||||
If *op* is compact, as determined by :c:func:`PyUnstable_Long_IsCompact`, | ||||||||||||||||
return its value. | ||||||||||||||||
|
||||||||||||||||
Otherwise, the return value is undefined. | ||||||||||||||||
|
||||||||||||||||
.. versionadded:: 3.12 | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
Export API | ||||||||||||||||
^^^^^^^^^^ | ||||||||||||||||
|
||||||||||||||||
.. versionadded:: 3.14 | ||||||||||||||||
|
||||||||||||||||
.. c:type:: Py_digit | ||||||||||||||||
|
||||||||||||||||
A single unsigned digit in the range [``0``; ``PyLong_BASE - 1``]. | ||||||||||||||||
|
||||||||||||||||
It is usually used in an *array of digits*, such as the | ||||||||||||||||
:c:member:`PyLong_DigitArray.digits` array. | ||||||||||||||||
|
||||||||||||||||
Its size depend on the :c:macro:`!PYLONG_BITS_IN_DIGIT` macro: | ||||||||||||||||
see the ``configure`` :option:`--enable-big-digits` option. | ||||||||||||||||
|
||||||||||||||||
See :c:member:`PyLongLayout.bits_per_digit` for the number of bits per | ||||||||||||||||
digit and :c:member:`PyLongLayout.digit_size` for the size of a digit (in | ||||||||||||||||
bytes). | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
.. c:struct:: PyLongLayout | ||||||||||||||||
|
||||||||||||||||
Layout of an array of digits, used by Python :class:`int` object. | ||||||||||||||||
|
||||||||||||||||
Use :c:func:`PyLong_GetNativeLayout` to get the native layout of Python | ||||||||||||||||
:class:`int` objects. | ||||||||||||||||
|
||||||||||||||||
See also :attr:`sys.int_info` which exposes similar information to Python. | ||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
.. c:member:: uint8_t bits_per_digit | ||||||||||||||||
|
||||||||||||||||
Bits per digit. | ||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
.. c:member:: uint8_t digit_size | ||||||||||||||||
|
||||||||||||||||
Digit size in bytes. | ||||||||||||||||
|
||||||||||||||||
.. c:member:: int8_t digits_order | ||||||||||||||||
|
||||||||||||||||
Word endian: | ||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
- ``1`` for most significant word first (big endian) | ||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
- ``-1`` for least significant first (little endian) | ||||||||||||||||
|
||||||||||||||||
.. c:member:: int8_t endian | ||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
Array endian: | ||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
- ``1`` for most significant byte first (big endian) | ||||||||||||||||
- ``-1`` for least significant first (little endian) | ||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
.. c:function:: const PyLongLayout* PyLong_GetNativeLayout(void) | ||||||||||||||||
|
||||||||||||||||
Get the native layout of Python :class:`int` objects. | ||||||||||||||||
|
||||||||||||||||
See the :c:struct:`PyLongLayout` structure. | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
.. c:struct:: PyLong_DigitArray | ||||||||||||||||
|
||||||||||||||||
A Python :class:`int` object exported as an array of digits. | ||||||||||||||||
|
||||||||||||||||
.. c:member:: int negative | ||||||||||||||||
|
||||||||||||||||
1 if the number is negative, 0 otherwise. | ||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
.. c:member:: Py_ssize_t ndigits | ||||||||||||||||
|
||||||||||||||||
Number of digits in :c:member:`digits` array. | ||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
.. c:member:: const void *digits | ||||||||||||||||
|
||||||||||||||||
Read-only array of unsigned digits. | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
.. c:function:: int PyLong_AsDigitArray(PyObject *obj, PyLong_DigitArray *array) | ||||||||||||||||
|
||||||||||||||||
Export a Python :class:`int` object as an array of digits. | ||||||||||||||||
|
||||||||||||||||
On success, set *\*array* and return 0. | ||||||||||||||||
On error, set an exception and return -1. | ||||||||||||||||
|
||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
This function always succeeds if *obj* is a Python :class:`int` object or a | ||||||||||||||||
subclass. | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vstinner, after some thinking I believe we should drop this contract, if we take into account future changes in internals of CPython's integers. If single layout view will be invalid - this function, probably, will allocate temporary buffers. That might fail. Can we offer a different contract instead, something like this: "This function always succeeds if obj is a Python There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No fallback to |
||||||||||||||||
|
||||||||||||||||
:c:func:`PyLong_FreeDigitArray` must be called once done with using | ||||||||||||||||
*array*. | ||||||||||||||||
|
||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Lets see if we can restore this in a that way. It might be helpful for e.g. Sage, which doesn't support PyPy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to not add this note. It was controversial during PEP 757 design. @serhiy-storchaka @encukou: What do you think? Would you be ok to declare that the PyLong_Export() function cannot fail if the argument is a Python int? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It was proposed unconditionally, not as CPython's implementation detail. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with it, as an implementation detail. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||
|
||||||||||||||||
.. c:function:: void PyLong_FreeDigitArray(PyLong_DigitArray *array) | ||||||||||||||||
|
||||||||||||||||
Release the export *array* created by :c:func:`PyLong_AsDigitArray`. | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
PyLongWriter API | ||||||||||||||||
^^^^^^^^^^^^^^^^ | ||||||||||||||||
|
||||||||||||||||
The :c:type:`PyLongWriter` API can be used to import an integer. | ||||||||||||||||
|
||||||||||||||||
.. versionadded:: 3.14 | ||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
.. c:struct:: PyLongWriter | ||||||||||||||||
|
||||||||||||||||
A Python :class:`int` writer instance. | ||||||||||||||||
|
||||||||||||||||
The instance must be destroyed by :c:func:`PyLongWriter_Finish`. | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
.. c:function:: PyLongWriter* PyLongWriter_Create(int negative, Py_ssize_t ndigits, void **digits) | ||||||||||||||||
|
||||||||||||||||
Create a :c:type:`PyLongWriter`. | ||||||||||||||||
|
||||||||||||||||
On success, set *\*digits* and return a writer. | ||||||||||||||||
On error, set an exception and return ``NULL``. | ||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
||||||||||||||||
*negative* is ``1`` if the number is negative, or ``0`` otherwise. | ||||||||||||||||
|
||||||||||||||||
*ndigits* is the number of digits in the *digits* array. It must be | ||||||||||||||||
greater than or equal to 0. | ||||||||||||||||
|
||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
The caller must initialize the array of digits *digits* and then call | ||||||||||||||||
:c:func:`PyLongWriter_Finish` to get a Python :class:`int`. Digits must be | ||||||||||||||||
in the range [``0``; ``PyLong_BASE - 1``]. Unused digits must be set to | ||||||||||||||||
``0``. | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
.. c:function:: PyObject* PyLongWriter_Finish(PyLongWriter *writer) | ||||||||||||||||
|
||||||||||||||||
Finish a :c:type:`PyLongWriter` created by :c:func:`PyLongWriter_Create`. | ||||||||||||||||
|
||||||||||||||||
On success, return a Python :class:`int` object. | ||||||||||||||||
On error, set an exception and return ``NULL``. | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
.. c:function:: void PyLongWriter_Discard(PyLongWriter *writer) | ||||||||||||||||
|
||||||||||||||||
Discard the internal object and destroy the writer instance. | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
Example creating an integer from an array of digits:: | ||||||||||||||||
|
||||||||||||||||
PyObject * | ||||||||||||||||
long_import(int negative, Py_ssize_t ndigits, Py_digit *digits) | ||||||||||||||||
{ | ||||||||||||||||
void *writer_digits; | ||||||||||||||||
PyLongWriter *writer = PyLongWriter_Create(negative, ndigits, | ||||||||||||||||
&writer_digits); | ||||||||||||||||
if (writer == NULL) { | ||||||||||||||||
return NULL; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
assert(layout.digit_size == sizeof(Py_digit)); | ||||||||||||||||
memcpy(writer_digits, digits, ndigits * sizeof(Py_digit)); | ||||||||||||||||
return PyLongWriter_Finish(writer); | ||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Add a new import and export API for Python :class:`int` objects: | ||
|
||
* :c:func:`PyLong_GetNativeLayout`; | ||
* :c:func:`PyLong_AsDigitArray`; | ||
* :c:func:`PyLong_FreeDigitArray`; | ||
* :c:func:`PyLongWriter_Create`; | ||
* :c:func:`PyLongWriter_Finish`; | ||
* :c:func:`PyLongWriter_Discard`. | ||
|
||
Patch by Victor Stinner. |
Uh oh!
There was an error while loading. Please reload this page.