Skip to content

Commit 4c7ec78

Browse files
skirpichevencukou
andauthored
gh-143869: Add PEP 757 functions to the limited API (#143906)
Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent 4ef30a5 commit 4c7ec78

File tree

8 files changed

+98
-45
lines changed

8 files changed

+98
-45
lines changed

Doc/c-api/long.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ Export API
687687
688688
.. versionadded:: 3.14
689689
690-
.. c:struct:: PyLongLayout
690+
.. c:type:: PyLongLayout
691691
692692
Layout of an array of "digits" ("limbs" in the GMP terminology), used to
693693
represent absolute value for arbitrary precision integers.
@@ -727,15 +727,15 @@ Export API
727727
728728
Get the native layout of Python :class:`int` objects.
729729
730-
See the :c:struct:`PyLongLayout` structure.
730+
See the :c:type:`PyLongLayout` structure.
731731
732732
The function must not be called before Python initialization nor after
733733
Python finalization. The returned layout is valid until Python is
734734
finalized. The layout is the same for all Python sub-interpreters
735735
in a process, and so it can be cached.
736736
737737
738-
.. c:struct:: PyLongExport
738+
.. c:type:: PyLongExport
739739
740740
Export of a Python :class:`int` object.
741741
@@ -769,7 +769,7 @@ Export API
769769
770770
Export a Python :class:`int` object.
771771
772-
*export_long* must point to a :c:struct:`PyLongExport` structure allocated
772+
*export_long* must point to a :c:type:`PyLongExport` structure allocated
773773
by the caller. It must not be ``NULL``.
774774
775775
On success, fill in *\*export_long* and return ``0``.
@@ -799,7 +799,7 @@ The :c:type:`PyLongWriter` API can be used to import an integer.
799799
800800
.. versionadded:: 3.14
801801
802-
.. c:struct:: PyLongWriter
802+
.. c:type:: PyLongWriter
803803
804804
A Python :class:`int` writer instance.
805805
@@ -827,7 +827,7 @@ The :c:type:`PyLongWriter` API can be used to import an integer.
827827
The layout of *digits* is described by :c:func:`PyLong_GetNativeLayout`.
828828
829829
Digits must be in the range [``0``; ``(1 << bits_per_digit) - 1``]
830-
(where the :c:struct:`~PyLongLayout.bits_per_digit` is the number of bits
830+
(where the :c:type:`~PyLongLayout.bits_per_digit` is the number of bits
831831
per digit).
832832
Any unused most significant digits must be set to ``0``.
833833

Doc/data/stable_abi.dat

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/cpython/longintrepr.h

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -138,45 +138,6 @@ _PyLong_CompactValue(const PyLongObject *op)
138138

139139
#define PyUnstable_Long_CompactValue _PyLong_CompactValue
140140

141-
142-
/* --- Import/Export API -------------------------------------------------- */
143-
144-
typedef struct PyLongLayout {
145-
uint8_t bits_per_digit;
146-
uint8_t digit_size;
147-
int8_t digits_order;
148-
int8_t digit_endianness;
149-
} PyLongLayout;
150-
151-
PyAPI_FUNC(const PyLongLayout*) PyLong_GetNativeLayout(void);
152-
153-
typedef struct PyLongExport {
154-
int64_t value;
155-
uint8_t negative;
156-
Py_ssize_t ndigits;
157-
const void *digits;
158-
// Member used internally, must not be used for other purpose.
159-
Py_uintptr_t _reserved;
160-
} PyLongExport;
161-
162-
PyAPI_FUNC(int) PyLong_Export(
163-
PyObject *obj,
164-
PyLongExport *export_long);
165-
PyAPI_FUNC(void) PyLong_FreeExport(
166-
PyLongExport *export_long);
167-
168-
169-
/* --- PyLongWriter API --------------------------------------------------- */
170-
171-
typedef struct PyLongWriter PyLongWriter;
172-
173-
PyAPI_FUNC(PyLongWriter*) PyLongWriter_Create(
174-
int negative,
175-
Py_ssize_t ndigits,
176-
void **digits);
177-
PyAPI_FUNC(PyObject*) PyLongWriter_Finish(PyLongWriter *writer);
178-
PyAPI_FUNC(void) PyLongWriter_Discard(PyLongWriter *writer);
179-
180141
#ifdef __cplusplus
181142
}
182143
#endif

Include/longobject.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,44 @@ PyAPI_FUNC(PyObject *) PyLong_FromString(const char *, char **, int);
166166
PyAPI_FUNC(unsigned long) PyOS_strtoul(const char *, char **, int);
167167
PyAPI_FUNC(long) PyOS_strtol(const char *, char **, int);
168168

169+
/* --- Import/Export API -------------------------------------------------- */
170+
171+
typedef struct PyLongLayout {
172+
uint8_t bits_per_digit;
173+
uint8_t digit_size;
174+
int8_t digits_order;
175+
int8_t digit_endianness;
176+
} PyLongLayout;
177+
178+
PyAPI_FUNC(const PyLongLayout*) PyLong_GetNativeLayout(void);
179+
180+
typedef struct PyLongExport {
181+
int64_t value;
182+
uint8_t negative;
183+
Py_ssize_t ndigits;
184+
const void *digits;
185+
// Member used internally, must not be used for other purpose.
186+
Py_uintptr_t _reserved;
187+
} PyLongExport;
188+
189+
PyAPI_FUNC(int) PyLong_Export(
190+
PyObject *obj,
191+
PyLongExport *export_long);
192+
PyAPI_FUNC(void) PyLong_FreeExport(
193+
PyLongExport *export_long);
194+
195+
196+
/* --- PyLongWriter API --------------------------------------------------- */
197+
198+
typedef struct PyLongWriter PyLongWriter;
199+
200+
PyAPI_FUNC(PyLongWriter*) PyLongWriter_Create(
201+
int negative,
202+
Py_ssize_t ndigits,
203+
void **digits);
204+
PyAPI_FUNC(PyObject*) PyLongWriter_Finish(PyLongWriter *writer);
205+
PyAPI_FUNC(void) PyLongWriter_Discard(PyLongWriter *writer);
206+
169207
#ifndef Py_LIMITED_API
170208
# define Py_CPYTHON_LONGOBJECT_H
171209
# include "cpython/longobject.h"

Lib/test/test_stable_abi_ctypes.py

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Added :c:func:`PyLong_GetNativeLayout`, :c:struct:`PyLongLayout`,
2+
:c:struct:`PyLongExport`, :c:func:`PyLong_Export`,
3+
:c:func:`PyLong_FreeExport`, :c:struct:`PyLongWriter`,
4+
:c:func:`PyLongWriter_Create`, :c:func:`PyLongWriter_Finish` and
5+
:c:func:`PyLongWriter_Discard` to the limited API.

Misc/stable_abi.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
# - 'full-abi': All of the struct is part of the ABI, including the size
4242
# (users may define arrays of these structs).
4343
# Typically used for initialization, rather than at runtime.
44+
# Any members whose names start with an underscore are not part of the
45+
# limited API; they're for CPython's use only.
4446
# - 'opaque': No members are part of the ABI, nor is the size. The Limited
4547
# API only handles these via pointers. The C definition should be
4648
# incomplete (opaque).
@@ -2664,3 +2666,29 @@
26642666
[function.Py_SET_SIZE]
26652667
# Before 3.15, this was a macro that accessed the PyObject member
26662668
added = '3.15'
2669+
2670+
# PEP 757 import/export API.
2671+
2672+
[function.PyLong_GetNativeLayout]
2673+
added = '3.15'
2674+
[function.PyLong_Export]
2675+
added = '3.15'
2676+
[function.PyLong_FreeExport]
2677+
added = '3.15'
2678+
[function.PyLongWriter_Create]
2679+
added = '3.15'
2680+
[function.PyLongWriter_Finish]
2681+
added = '3.15'
2682+
[function.PyLongWriter_Discard]
2683+
added = '3.15'
2684+
[struct.PyLongWriter]
2685+
added = '3.15'
2686+
struct_abi_kind = 'opaque'
2687+
[struct.PyLongLayout]
2688+
added = '3.15'
2689+
struct_abi_kind = 'full-abi'
2690+
[struct.PyLongExport]
2691+
added = '3.15'
2692+
# Note: The `_reserved` member of this struct is for interal use only.
2693+
# (The definition of 'full-abi' was clarified when this entry was added.)
2694+
struct_abi_kind = 'full-abi'

PC/python3dll.c

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)