Description
Feature or enhancement
The private _PyLong_AsByteArray()
and _PyLong_FromByteArray()
functions were removed in Python 3.13: see PR #108429.
@scoder asked what is the intended replacement for _PyLong_FromByteArray()
.
The replacement for _PyLong_FromByteArray()
is PyObject_CallMethod((PyObject*)&PyList_Type, "from_bytes", "s#s", str, len, "big")
but I'm not sure what is the easy way to set the signed parameter to True (default: signed=False
).
The replacement for _PyLong_AsByteArray()
is PyObject_CallMethod(my_int, "to_bytes", "ns", length, "big")
. Same, I'm not sure how to easy set the signed parameter to True (default: signed=False
).
I propose to add public PyLong_AsByteArray() and PyLong_FromByteArray() functions to the C API.
Python 3.12 modified PyLongObject: it's no longer a simple array of digits, but it's now a more less straightforward _PyLongValue
structure which requires using unstable functions to access small "compact" values:
- PyUnstable_Long_IsCompact()
- PyUnstable_Long_CompactValue()
So having a reliable and simple way to import/export a Python int object as bytes became even more important.
A code search for _PyLong_AsByteArray
in PyPI top 5,000 projects found 12 projects using it:
- Cython (0.29.36)
- blspy (2.0.2)
- catboost (1.2)
- fastobo (0.12.2)
- gevent (22.10.2)
- guppy3 (3.1.3)
- line_profiler (4.0.3)
- msgspec (0.16.0)
- orjson (3.9.1)
- pickle5 (0.0.12)
- pyodbc (4.0.39)
- rlp (3.0.0)
Linked PRs
- gh-111140: Adds PyLong_AsNativeBytes and PyLong_FromNative[Unsigned]Bytes functions #114886
- gh-111140: Minor doc fixes for PyLong_AsNativeBytes #115375
- gh-111140: Improve PyLong_AsNativeBytes API doc example & improve the test #115380
- gh-111140: Fix edge case in PyLong_AsNativeBytes where large negative longs may require an extra byte #116053
- gh-111140: minor docs typos cleanup in the C example API calls. #118612