8000 gh-102471, PEP 757: Add PyLong import and export API by vstinner · Pull Request #121339 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 54 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
f4fdbf2
gh-102471: Add PyLong import and export API
vstinner Jul 2, 2024
c2e568e
Add layout
vstinner Jul 23, 2024
f0d9525
Merge branch 'main' into long_export
vstinner Sep 3, 2024
b19764f
Rename word_endian to digits_order
vstinner Sep 3, 2024
6f7fd11
Replace Py_digit* type with void*
vstinner Sep 3, 2024
080e079
Add PyLongWriter_Discard() function
vstinner Sep 4, 2024
1a7902f
Fixes
vstinner Sep 5, 2024
b70a6dd
Use unsigned type for ndigits
vstinner Sep 5, 2024
07552a7
Remove again layout
vstinner Sep 5, 2024
0d0f942
Revert "Use unsigned type for ndigits"
vstinner Sep 6, 2024
762c33a
doc: adjust ndigits documentation
vstinner Sep 6, 2024
20be7a3
Update doc
vstinner Sep 13, 2024
d92bf1e
Make PyLong_DigitArray.obj private
vstinner Sep 16, 2024
b3b02a2
Remove reserved documentation
vstinner Sep 16, 2024
caca2d7
PyLong_FreeDigitArray() only clears _reserved
vstinner Sep 16, 2024
4221a49
Make PyLong_LAYOUT static
vstinner Sep 16, 2024
37b1d49
Add PyLong_AsDigitArray.value
vstinner Sep 16, 2024
d70a121
Inline PyLong_AsInt64() to avoid the exception
vstinner Sep 17 8000 , 2024
4aa25f6
Remove Py_digit type; update the doc
vstinner Sep 17, 2024
90973d4
Merge branch 'main' into long_export
vstinner Sep 17, 2024
5d3e224
Add long_asnativebytes() function
vstinner Sep 17, 2024
c7d7cb2
Remove reference to removed Py_digit type
vstinner Sep 17, 2024
a3d601a
Address Antoine's review
vstinner Sep 17, 2024
c049268
Merge branch 'main' into long_export
vstinner Sep 17, 2024
06b196b
Merge branch 'main' into long_export
skirpichev Sep 18, 2024
3e8d296
Apply suggestions from code review
skirpichev Sep 18, 2024
86c68c2
Merge branch 'main' into long_export
skirpichev Sep 18, 2024
a8fd669
Revert "Add long_asnativebytes() function"
vstinner Sep 18, 2024
a04f9d0
Use PyLong_AsLongAndOverflow()
vstinner Sep 18, 2024
b2be94a
Try PyLong_AsLongLongAndOverflow() first
vstinner Sep 18, 2024
ca98ad1
Merge branch 'main' into long_export
vstinner Sep 18, 2024
167d75e
Update Doc/c-api/long.rst
vstinner Sep 19, 2024
5e53a5b
Sync implementation with PEP (#8)
skirpichev Oct 16, 2024
c24789f
Merge branch 'main' into long_export
skirpichev Nov 13, 2024
0422f9d
fix NL in Doc/c-api/long.rst (sorry, damn web editor)
skirpichev Nov 13, 2024
a529a48
rename news
skirpichev Nov 13, 2024
3db44f3
Address Erlend's review
vstinner Nov 13, 2024
1d2863e
Address Sergey's review
vstinner Nov 13, 2024
d663511
Merge branch 'main' into long_export
vstinner Nov 13, 2024
816798d
Merge branch 'main' into long_export
vstinner Nov 28, 2024
033bd65
Update documentation from PEP 757
vstinner Nov 28, 2024
36b87d4
Update Modules/_testcapi/long.c
vstinner Nov 28, 2024
94d852e
Sync implementation with PEP (#9)
skirpichev Dec 9, 2024
a72ff83
Merge branch 'main' into long_export
vstinner Dec 9, 2024
53d584b
Cleanup
vstinner Dec 9, 2024
577598a
Update Doc/c-api/long.rst
vstinner Dec 9, 2024
b08cd55
Address Steve's review
vstinner Dec 9, 2024
eaebef3
Address Bénédikt's review
vstinner Dec 10, 2024
03248c7
Apply suggestions from code review
vstinner Dec 10, 2024
0a26f97
Address Steve's review
vstinner Dec 11, 2024
88a62fe
Add PyLong_Export to Doc/data/refcounts.dat
vstinner Dec 12, 2024
45517ab
Address Serhiy's review
vstinner Dec 12, 2024
92007d1
Address Petr's review
vstinner Dec 12, 2024
6d3cb80
Add PyLongWriter to Doc/data/refcounts.dat
vstinner Dec 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address Bénédikt's review
  • Loading branch information
vstinner committed Dec 10, 2024
commit eaebef39b0c727b9c9eb57791141618bad61c46b
12 changes: 8 additions & 4 deletions Doc/c-api/long.rst
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
Export API
^^^^^^^^^^

.. versionadded:: 3.14
.. versionadded:: next

.. c:struct:: PyLongLayout

Expand Down Expand Up @@ -692,7 +692,7 @@ Export API
Digit endianness:

- ``1`` for most significant byte first (big endian)
- ``-1`` for least significant first (little endian)
- ``-1`` for least significant byte first (little endian)


.. c:function:: const PyLongLayout* PyLong_GetNativeLayout(void)
Expand Down Expand Up @@ -743,6 +743,8 @@ Export API

Export a Python :class:`int` object.

*export_long* must not be ``NULL``.

On success, set *\*export_long* and return ``0``.
On error, set an exception and return ``-1``.

Expand All @@ -760,7 +762,7 @@ PyLongWriter API

The :c:type:`PyLongWriter` API can be used to import an integer.

.. versionadded:: 3.14
.. versionadded:: next

.. c:struct:: PyLongWriter

Expand Down Expand Up @@ -789,7 +791,7 @@ The :c:type:`PyLongWriter` API can be used to import an integer.
: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``.
The unused most significant digits must be set to ``0``.


.. c:function:: PyObject* PyLongWriter_Finish(PyLongWriter *writer)
Expand All @@ -809,4 +811,6 @@ The :c:type:`PyLongWriter` API can be used to import an integer.

Discard a :c:type:`PyLongWriter` created by :c:func:`PyLongWriter_Create`.

*writer* must not be ``NULL``.

The writer instance is invalid after the call.
3 changes: 3 additions & 0 deletions Lib/test/test_capi/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,12 +738,15 @@ def test_long_export(self):
self.assertEqual(pylong_export(0), 0)
self.assertEqual(pylong_export(123), 123)
self.assertEqual(pylong_export(-123), -123)
self.assertEqual(pylong_export(IntSubclass(123)), 123)

# use an array, doesn't fit into int64_t
self.assertEqual(pylong_export(base**10 * 2 + 1),
(0, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]))
self.assertEqual(pylong_export(-(base**10 * 2 + 1)),
(1, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]))
self.assertEqual(pylong_export(IntSubclass(base**10 * 2 + 1)),
(0, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]))

for value in (1.0, 0+1j, "abc"):
with self.subTest(value=value):
Expand Down
8 changes: 5 additions & 3 deletions Modules/_testcapi/long.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ pylong_export(PyObject *module, PyObject *obj)
const digit *export_long_digits = export_long.digits;

PyObject *digits = PyList_New(0);
if (digits == NULL) {
goto error;
}
for (Py_ssize_t i = 0; i < export_long.ndigits; i++) {
PyObject *item = PyLong_FromUnsignedLong(export_long_digits[i]);
if (item == NULL) {
Expand All @@ -214,7 +217,7 @@ pylong_export(PyObject *module, PyObject *obj)
return res;

error:
Py_DECREF(digits);
Py_XDECREF(digits);
PyLong_FreeExport(&export_long);
return NULL;
}
Expand All @@ -225,8 +228,7 @@ pylongwriter_create(PyObject *module, PyObject *args)
{
int negative;
PyObject *list;
if (!PyArg_ParseTuple(args, "iO!", &negative, &PyList_Type, &list))
{
if (!PyArg_ParseTuple(args, "iO!", &negative, &PyList_Type, &list)) {
return NULL;
}
Py_ssize_t ndigits = PyList_GET_SIZE(list);
Expand Down
8 changes: 6 additions & 2 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6853,20 +6853,24 @@ PyLongWriter_Create(int negative, Py_ssize_t ndigits, void **digits)
{
if (ndigits <= 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not allow 0?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, he asked to document that case (i.e. when ndigits==0).

We don't think it's a good idea to overbloat docs with this edge case (different functions should be used for small integers). PEP has a dedicated section to discuss import for small integers, suggesting different functions. (In fact, the whole API is about import/export for big integers.)

PyErr_SetString(PyExc_ValueError, "ndigits must be positive");
return NULL;
goto error;
}
assert(digits != NULL);

PyLongObject *obj = _PyLong_New(ndigits);
if (obj == NULL) {
return NULL;
goto error;
}
if (negative) {
_PyLong_FlipSign(obj);
}

*digits = obj->long_value.ob_digit;
return (PyLongWriter*)obj;

error:
*digits = NULL;
return NULL;
}


Expand Down
Loading
0