Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

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

.. c:struct:: PyLongLayout

Layout of an array of digits, used by Python :class:`int` object.
Layout of an array of "digits" ("limbs" in the GMP terminology), used to
represent absolute value for arbitrary precision integers.

Use :c:func:`PyLong_GetNativeLayout` to get the native layout of Python
:class:`int` objects.
:class:`int` objects, used internally for integers with "big enough"
absolute value.

See also :data:`sys.int_info` which exposes similar information to Python.

Expand DownExpand Up@@ -655,6 +657,11 @@ Export API

See the :c:struct:`PyLongLayout` structure.

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.


.. c:struct:: PyLongExport

Expand DownExpand Up@@ -695,9 +702,6 @@ Export API
On success, set *\*export_long* and return 0.
On error, set an exception and return -1.

This function always succeeds if *obj* is a Python :class:`int` object or a
subclass.

If *export_long.digits* is not ``NULL``, :c:func:`PyLong_FreeExport` must be
called when the export is no longer needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
..impl-detail::
This function always succeeds if *obj* is a Python:class:`int` object
or a subclass.

Lets see if we can restore this in a that way. It might be helpful for e.g. Sage, which doesn't support PyPy.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

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

It was controversial during PEP 757 design.

It was proposed unconditionally, not as CPython's implementation detail.

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with it, as an implementation detail.

Copy link
Contributor

Choose a reason for hiding this comment

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

Expand All@@ -718,7 +722,8 @@ The :c:type:`PyLongWriter` API can be used to import an integer.

A Python :class:`int` writer instance.

The instance must be destroyed by :c:func:`PyLongWriter_Finish`.
The instance must be destroyed by :c:func:`PyLongWriter_Finish` or
:c:func:`PyLongWriter_Discard`.


.. c:function:: PyLongWriter* PyLongWriter_Create(int negative, Py_ssize_t ndigits, void **digits)
Expand All@@ -733,10 +738,11 @@ The :c:type:`PyLongWriter` API can be used to import an integer.
*ndigits* is the number of digits in the *digits* array. It must be
greater than or equal to 0.

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``.
The caller can either initialize the array of digits *digits* and then call
:c:func:`PyLongWriter_Finish` to get a Python :class:`int`, or call
:c:func:`PyLongWriter_Discard` to destroy the writer instance. Digits must
be in the range [``0``; ``(1 << sys.int_info.bits_per_digit) - 1``]. Unused
digits must be set to ``0``.


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

.. c:function:: void PyLongWriter_Discard(PyLongWriter *writer)

Discardthe internal object and destroy the writer instance.
Discarda :c:type:`PyLongWriter` created by :c:func:`PyLongWriter_Create`.
10 changes: 5 additions & 5 deletionsInclude/cpython/longintrepr.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -148,15 +148,15 @@ typedef struct PyLongLayout {
// Digit size in bytes
uint8_t digit_size;

//Word endian:
// * 1 for most significantword first (big endian)
// * -1 for least significant first (little endian)
//Digits order:
// * 1 for most significantdigit first
// * -1 for least significant first
int8_t digits_order;

//Array endian:
//Digit endianness:
// * 1 for most significant byte first (big endian)
// * -1 for least significant first (little endian)
int8_tendian;
int8_tendianness;
} PyLongLayout;

PyAPI_FUNC(const PyLongLayout*) PyLong_GetNativeLayout(void);
Expand Down
2 changes: 1 addition & 1 deletionLib/test/test_capi/test_long.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -678,7 +678,7 @@ def test_long_layout(self):
'bits_per_digit': int_info.bits_per_digit,
'digit_size': int_info.sizeof_digit,
'digits_order': -1,
'endian': -1 if sys.byteorder == 'little' else 1,
'endianness': -1 if sys.byteorder == 'little' else 1,
}
self.assertEqual(layout, expected)

Expand Down
4 changes: 2 additions & 2 deletionsModules/_testcapi/long.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -155,11 +155,11 @@ layout_to_dict(const PyLongLayout *layout)
goto error;
}

value = PyLong_FromLong(layout->endian);
value = PyLong_FromLong(layout->endianness);
if (value == NULL) {
goto error;
}
res = PyDict_SetItemString(dict, "endian", value);
res = PyDict_SetItemString(dict, "endianness", value);
Py_DECREF(value);
if (res < 0) {
goto error;
Expand Down
2 changes: 1 addition & 1 deletionObjects/longobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6779,7 +6779,7 @@ int PyLong_AsUInt64(PyObject *obj, uint64_t *value)
static const PyLongLayout PyLong_LAYOUT = {
.bits_per_digit = PyLong_SHIFT,
.digits_order = -1, // least significant first
.endian = PY_LITTLE_ENDIAN ? -1 : 1,
.endianness = PY_LITTLE_ENDIAN ? -1 : 1,
.digit_size = sizeof(digit),
};

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp