-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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 1 commit
f4fdbf2
c2e568e
f0d9525
b19764f
6f7fd11
080e079
1a7902f
b70a6dd
07552a7
0d0f942
762c33a
20be7a3
d92bf1e
b3b02a2
caca2d7
4221a49
8000
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
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -703,8 +703,8 @@ Export API | |||||||||||||||
|
||||||||||||||||
The function must not be called before Python initialization nor after | ||||||||||||||||
Python finalization. The returned layout is valid until Python is | ||||||||||||||||
finalized. The layout is the same for all Python sub-interpreters and | ||||||||||||||||
so it can be cached. | ||||||||||||||||
finalized. The layout is the same for all Python sub-interpreters | ||||||||||||||||
in a process, and so it can be cached. | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
.. c:struct:: PyLongExport | ||||||||||||||||
|
@@ -714,10 +714,8 @@ Export API | |||||||||||||||
There are two cases: | ||||||||||||||||
|
||||||||||||||||
* If :c:member:`digits` is ``NULL``, only use the :c:member:`value` member. | ||||||||||||||||
Calling :c:func:`PyLong_FreeExport` is optional in this case. | ||||||||||||||||
* If :c:member:`digits` is not ``NULL``, use :c:member:`negative`, | ||||||||||||||||
:c:member:`ndigits` and :c:member:`digits` members. | ||||||||||||||||
Calling :c:func:`PyLong_FreeExport` is mandatory in this case. | ||||||||||||||||
|
||||||||||||||||
.. c:member:: int64_t value | ||||||||||||||||
|
||||||||||||||||
|
@@ -743,20 +741,28 @@ Export API | |||||||||||||||
|
||||||||||||||||
Export a Python :class:`int` object. | ||||||||||||||||
|
||||||||||||||||
*export_long* must not be ``NULL``. | ||||||||||||||||
*export_long* must point to a :c:struct:`PyLongExport` structure allocated | ||||||||||||||||
by the caller. It must not be ``NULL``. | ||||||||||||||||
|
||||||||||||||||
On success, set *\*export_long* and return ``0``. | ||||||||||||||||
On success, fill in *\*export_long* and return ``0``. | ||||||||||||||||
On error, set an exception and return ``-1``. | ||||||||||||||||
|
||||||||||||||||
If *export_long->digits* is not ``NULL``, :c:func:`PyLong_FreeExport` must | ||||||||||||||||
be called when the export is no longer needed. Otherwise, calling | ||||||||||||||||
:c:func:`PyLong_FreeExport` is optional. | ||||||||||||||||
:c:func:`PyLong_FreeExport` must be called when the export is no longer | ||||||||||||||||
needed. | ||||||||||||||||
|
||||||||||||||||
.. impl-detail:: | ||||||||||||||||
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.
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_FreeExport(PyLongExport *export_long) | ||||||||||||||||
|
||||||||||||||||
Release the export *export_long* created by :c:func:`PyLong_Export`. | ||||||||||||||||
|
||||||||||||||||
.. impl-detail:: | ||||||||||||||||
Calling :c:func:`PyLong_FreeExport` is optional if *export_long->digits* | ||||||||||||||||
is ``NULL``. | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
PyLongWriter API | ||||||||||||||||
^^^^^^^^^^^^^^^^ | ||||||||||||||||
|
@@ -787,12 +793,18 @@ The :c:type:`PyLongWriter` API can be used to import an integer. | |||||||||||||||
|
||||||||||||||||
*digits* must not be NULL. | ||||||||||||||||
|
||||||||||||||||
The caller can either initialize the array of digits *digits* and then | ||||||||||||||||
either call :c:func:`PyLongWriter_Finish` to get a Python :class:`int` or | ||||||||||||||||
:c:func:`PyLongWriter_Discard` to destroy the writer instance. Digits must | ||||||||||||||||
be in the range [``0``; ``(1 << bits_per_digit) - 1``] (where the | ||||||||||||||||
:c:struct:`~PyLongLayout.bits_per_digit` is the number of bits per digit). | ||||||||||||||||
The unused most significant digits must be set to ``0``. | ||||||||||||||||
After a successful call to this function, the caller should fill in the | ||||||||||||||||
array of digits *digits* and then call :c:func:`PyLongWriter_Finish` to get | ||||||||||||||||
a Python :class:`int`. | ||||||||||||||||
The layout of *digits* is described by :c:func:`PyLong_GetNativeLayout`. | ||||||||||||||||
|
||||||||||||||||
Digits must be in the range [``0``; ``(1 << bits_per_digit) - 1``] | ||||||||||||||||
(where the :c:struct:`~PyLongLayout.bits_per_digit` is the number of bits | ||||||||||||||||
per digit). | ||||||||||||||||
Any unused most significant digits must be set to ``0``. | ||||||||||||||||
|
||||||||||||||||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
Alternately, call :c:func:`PyLongWriter_Discard` to destroy the writer | ||||||||||||||||
instance without creating an :class:`~int` object. | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
.. c:function:: PyObject* PyLongWriter_Finish(PyLongWriter *writer) | ||||||||||||||||
|
@@ -805,7 +817,7 @@ The :c:type:`PyLongWriter` API can be used to import an integer. | |||||||||||||||
The function takes care of normalizing the digits and converts the object | ||||||||||||||||
to a compact integer if needed. | ||||||||||||||||
|
||||||||||||||||
The writer instance is invalid after the call. | ||||||||||||||||
The writer instance and the *digits* array are invalid after the call. | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
.. c:function:: void PyLongWriter_Discard(PyLongWriter *writer) | ||||||||||||||||
|
@@ -814,4 +826,4 @@ The :c:type:`PyLongWriter` API can be used to import an integer. | |||||||||||||||
|
||||||||||||||||
*writer* must not be ``NULL``. | ||||||||||||||||
|
||||||||||||||||
The writer instance is invalid after the call. | ||||||||||||||||
The writer instance and the *digits* array are invalid after the call. |
Uh oh!
There was an error while loading. Please reload this page.