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, 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
Merge branch 'main' into long_export
  • Loading branch information
vstinner committed Sep 3, 2024
commit f0d9525f4ad03d3931246292b71e20c69c8f32d7
1 change: 1 addition & 0 deletions Doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
('c:type', 'time_t'),
('c:type', 'int8_t'),
('c:type', 'uint8_t'),
('c:type', 'uint32_t'),
('c:type', 'uint64_t'),
('c:type', 'uintmax_t'),
('c:type', 'uintptr_t'),
Expand Down
64 changes: 64 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,69 @@ New Features

(Contributed by Victor Stinner in :gh:`119182`.)

* Add :c:func:`PyIter_NextItem` to replace :c:func:`PyIter_Next`,
which has an ambiguous return value.
(Contributed by Irit Katriel and Erlend Aasland in :gh:`105201`.)

* :c:func:`Py_Finalize` now deletes all interned strings. This
is backwards incompatible to any C-Extension that holds onto an interned
string after a call to :c:func:`Py_Finalize` and is then reused after a
call to :c:func:`Py_Initialize`. Any issues arising from this behavior will
normally result in crashes during the exectuion of the subsequent call to
:c:func:`Py_Initialize` from accessing uninitialized memory. To fix, use
an address sanitizer to identify any use-after-free coming from
an interned string and deallocate it during module shutdown.
(Contribued by Eddie Elizondo in :gh:`113601`.)

* Add new functions to convert C ``<stdint.h>`` numbers from/to Python
:class:`int`:

* :c:func:`PyLong_FromInt32`
* :c:func:`PyLong_FromInt64`
* :c:func:`PyLong_FromUInt32`
* :c:func:`PyLong_FromUInt64`
* :c:func:`PyLong_AsInt32`
* :c:func:`PyLong_AsInt64`
* :c:func:`PyLong_AsUInt32`
* :c:func:`PyLong_AsUInt64`

(Contributed by Victor Stinner in :gh:`120389`.)

* Add :c:func:`PyBytes_Join(sep, iterable) <PyBytes_Join>` function,
similar to ``sep.join(iterable)`` in Python.
(Contributed by Victor Stinner in :gh:`121645`.)

* Add :c:func:`Py_HashBuffer` to compute and return the hash value of a buffer.
(Contributed by Antoine Pitrou and Victor Stinner in :gh:`122854`.)

* Add functions to get and set the current runtime Python configuration
(:pep:`741`):

* :c:func:`PyConfig_Get`
* :c:func:`PyConfig_GetInt`
* :c:func:`PyConfig_Set`
* :c:func:`PyConfig_Names`

(Contributed by Victor Stinner in :gh:`107954`.)

* Add functions to configure the Python initialization (:pep:`741`):

* :c:func:`PyInitConfig_Create`
* :c:func:`PyInitConfig_Free`
* :c:func:`PyInitConfig_GetError`
* :c:func:`PyInitConfig_GetExitCode`
* :c:func:`PyInitConfig_HasOption`
* :c:func:`PyInitConfig_GetInt`
* :c:func:`PyInitConfig_GetStr`
* :c:func:`PyInitConfig_GetStrList`
* :c:func:`PyInitConfig_FreeStrList`
* :c:func:`PyInitConfig_SetInt`
* :c:func:`PyInitConfig_SetStr`
* :c:func:`PyInitConfig_SetStrList`
* :c:func:`Py_InitializeFromInitConfig`

(Contributed by Victor Stinner in :gh:`107954`.)

* Add a new import and export API for Python :class:`int` objects:

* :c:func:`PyLong_GetNativeLayout`;
Expand All @@ -475,6 +538,7 @@ New Features

(Contributed by Victor Stinner in :gh:`102471`.)


Porting to Python 3.14
----------------------

Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_capi/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,32 @@ def test_long_getsign(self):

# CRASHES getsign(NULL)

def test_long_asint32(self):
# Test PyLong_AsInt32() and PyLong_FromInt32()
to_int32 = _testlimitedcapi.pylong_asint32
from _testcapi import INT32_MIN, INT32_MAX
self.check_long_asint(to_int32, INT32_MIN, INT32_MAX)

def test_long_asint64(self):
# Test PyLong_AsInt64() and PyLong_FromInt64()
as_int64 = _testlimitedcapi.pylong_asint64
from _testcapi import INT64_MIN, INT64_MAX
self.check_long_asint(as_int64, INT64_MIN, INT64_MAX)

def test_long_asuint32(self):
# Test PyLong_AsUInt32() and PyLong_FromUInt32()
as_uint32 = _testlimitedcapi.pylong_asuint32
from _testcapi import UINT32_MAX
self.check_long_asint(as_uint32, 0, UINT32_MAX,
negative_value_error=ValueError)

def test_long_asuint64(self):
# Test PyLong_AsUInt64() and PyLong_FromUInt64()
as_uint64 = _testlimitedcapi.pylong_asuint64
from _testcapi import UINT64_MAX
self.check_long_asint(as_uint64, 0, UINT64_MAX,
negative_value_error=ValueError)

def test_long_layout(self):
# Test PyLong_LAYOUT
int_info = sys.int_info
Expand Down
68 changes: 68 additions & 0 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6708,6 +6708,74 @@ PyUnstable_Long_CompactValue(const PyLongObject* op) {
return _PyLong_CompactValue((PyLongObject*)op);
}


PyObject* PyLong_FromInt32(int32_t value)
{ return PyLong_FromNativeBytes(&value, sizeof(value), -1); }

PyObject* PyLong_FromUInt32(uint32_t value)
{ return PyLong_FromUnsignedNativeBytes(&value, sizeof(value), -1); }

PyObject* PyLong_FromInt64(int64_t value)
{ return PyLong_FromNativeBytes(&value, sizeof(value), -1); }

PyObject* PyLong_FromUInt64(uint64_t value)
{ return PyLong_FromUnsignedNativeBytes(&value, sizeof(value), -1); }

#define LONG_TO_INT(obj, value, type_name) \
do { \
int flags = (Py_ASNATIVEBYTES_NATIVE_ENDIAN \
| Py_ASNATIVEBYTES_ALLOW_INDEX); \
Py_ssize_t bytes = PyLong_AsNativeBytes(obj, value, sizeof(*value), flags); \
if (bytes < 0) { \
return -1; \
} \
if ((size_t)bytes > sizeof(*value)) { \
PyErr_SetString(PyExc_OverflowError, \
"Python int too large to convert to " type_name); \
return -1; \
} \
return 0; \
} while (0)

int PyLong_AsInt32(PyObject *obj, int32_t *value)
{
LONG_TO_INT(obj, value, "C int32_t");
}

int PyLong_AsInt64(PyObject *obj, int64_t *value)
8ED1 {
LONG_TO_INT(obj, value, "C int64_t");
}

#define LONG_TO_UINT(obj, value, type_name) \
do { \
int flags = (Py_ASNATIVEBYTES_NATIVE_ENDIAN \
| Py_ASNATIVEBYTES_UNSIGNED_BUFFER \
| Py_ASNATIVEBYTES_REJECT_NEGATIVE \
| Py_ASNATIVEBYTES_ALLOW_INDEX); \
Py_ssize_t bytes = PyLong_AsNativeBytes(obj, value, sizeof(*value), flags); \
if (bytes < 0) { \
return -1; \
} \
if ((size_t)bytes > sizeof(*value)) { \
PyErr_SetString(PyExc_OverflowError, \
"Python int too large to convert to " type_name); \
return -1; \
} \
return 0; \
} while (0)

int PyLong_AsUInt32(PyObject *obj, uint32_t *value)
{
LONG_TO_UINT(obj, value, "C uint32_t");
}

int PyLong_AsUInt64(PyObject *obj, uint64_t *value)
{
LONG_TO_UINT(obj, value, "C uint64_t");
}


const PyLongLayout PyLong_LAYOUT = {
.bits_per_digit = PyLong_SHIFT,
.word_endian = PY_LITTLE_ENDIAN ? -1 : 1,
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0