Skip to content

Commit c55e731

Browse files
authored
gh-106320: Remove private PyLong C API functions (#108429)
Remove private PyLong C API functions: * _PyLong_AsByteArray() * _PyLong_DivmodNear() * _PyLong_Format() * _PyLong_Frexp() * _PyLong_FromByteArray() * _PyLong_FromBytes() * _PyLong_GCD() * _PyLong_Lshift() * _PyLong_Rshift() Move these functions to the internal C API. No longer export _PyLong_FromBytes() function.
1 parent 7f31676 commit c55e731

File tree

10 files changed

+99
-78
lines changed

10 files changed

+99
-78
lines changed

Include/cpython/longobject.h

+1-69
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,7 @@ PyAPI_FUNC(int) _PyLong_UnsignedLong_Converter(PyObject *, void *);
1010
PyAPI_FUNC(int) _PyLong_UnsignedLongLong_Converter(PyObject *, void *);
1111
PyAPI_FUNC(int) _PyLong_Size_t_Converter(PyObject *, void *);
1212

13-
/* _PyLong_Frexp returns a double x and an exponent e such that the
14-
true value is approximately equal to x * 2**e. e is >= 0. x is
15-
0.0 if and only if the input is 0 (in which case, e and x are both
16-
zeroes); otherwise, 0.5 <= abs(x) < 1.0. On overflow, which is
17-
possible if the number of bits doesn't fit into a Py_ssize_t, sets
18-
OverflowError and returns -1.0 for x, 0 for e. */
19-
PyAPI_FUNC(double) _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e);
20-
21-
PyAPI_FUNC(PyObject *) PyLong_FromUnicodeObject(PyObject *u, int base);
22-
PyAPI_FUNC(PyObject *) _PyLong_FromBytes(const char *, Py_ssize_t, int);
13+
PyAPI_FUNC(PyObject*) PyLong_FromUnicodeObject(PyObject *u, int base);
2314

2415
/* _PyLong_Sign. Return 0 if v is 0, -1 if v < 0, +1 if v > 0.
2516
v must not be NULL, and must be a normalized long.
@@ -36,65 +27,6 @@ PyAPI_FUNC(int) _PyLong_Sign(PyObject *v);
3627
*/
3728
PyAPI_FUNC(size_t) _PyLong_NumBits(PyObject *v);
3829

39-
/* _PyLong_DivmodNear. Given integers a and b, compute the nearest
40-
integer q to the exact quotient a / b, rounding to the nearest even integer
41-
in the case of a tie. Return (q, r), where r = a - q*b. The remainder r
42-
will satisfy abs(r) <= abs(b)/2, with equality possible only if q is
43-
even.
44-
*/
45-
PyAPI_FUNC(PyObject *) _PyLong_DivmodNear(PyObject *, PyObject *);
46-
47-
/* _PyLong_FromByteArray: View the n unsigned bytes as a binary integer in
48-
base 256, and return a Python int with the same numeric value.
49-
If n is 0, the integer is 0. Else:
50-
If little_endian is 1/true, bytes[n-1] is the MSB and bytes[0] the LSB;
51-
else (little_endian is 0/false) bytes[0] is the MSB and bytes[n-1] the
52-
LSB.
53-
If is_signed is 0/false, view the bytes as a non-negative integer.
54-
If is_signed is 1/true, view the bytes as a 2's-complement integer,
55-
non-negative if bit 0x80 of the MSB is clear, negative if set.
56-
Error returns:
57-
+ Return NULL with the appropriate exception set if there's not
58-
enough memory to create the Python int.
59-
*/
60-
PyAPI_FUNC(PyObject *) _PyLong_FromByteArray(
61-
const unsigned char* bytes, size_t n,
62-
int little_endian, int is_signed);
63-
64-
/* _PyLong_AsByteArray: Convert the least-significant 8*n bits of long
65-
v to a base-256 integer, stored in array bytes. Normally return 0,
66-
return -1 on error.
67-
If little_endian is 1/true, store the MSB at bytes[n-1] and the LSB at
68-
bytes[0]; else (little_endian is 0/false) store the MSB at bytes[0] and
69-
the LSB at bytes[n-1].
70-
If is_signed is 0/false, it's an error if v < 0; else (v >= 0) n bytes
71-
are filled and there's nothing special about bit 0x80 of the MSB.
72-
If is_signed is 1/true, bytes is filled with the 2's-complement
73-
representation of v's value. Bit 0x80 of the MSB is the sign bit.
74-
Error returns (-1):
75-
+ is_signed is 0 and v < 0. TypeError is set in this case, and bytes
76-
isn't altered.
77-
+ n isn't big enough to hold the full mathematical value of v. For
78-
example, if is_signed is 0 and there are more digits in the v than
79-
fit in n; or if is_signed is 1, v < 0, and n is just 1 bit shy of
80-
being large enough to hold a sign bit. OverflowError is set in this
81-
case, but bytes holds the least-significant n bytes of the true value.
82-
*/
83-
PyAPI_FUNC(int) _PyLong_AsByteArray(PyLongObject* v,
84-
unsigned char* bytes, size_t n,
85-
int little_endian, int is_signed);
86-
87-
/* _PyLong_Format: Convert the long to a string object with given base,
88-
appending a base prefix of 0[box] if base is 2, 8 or 16. */
89-
PyAPI_FUNC(PyObject *) _PyLong_Format(PyObject *obj, int base);
90-
91-
/* For use by the gcd function in mathmodule.c */
92-
PyAPI_FUNC(PyObject *) _PyLong_GCD(PyObject *, PyObject *);
93-
94-
PyAPI_FUNC(PyObject *) _PyLong_Rshift(PyObject *, size_t);
95-
PyAPI_FUNC(PyObject *) _PyLong_Lshift(PyObject *, size_t);
96-
97-
9830
PyAPI_FUNC(int) PyUnstable_Long_IsCompact(const PyLongObject* op);
9931
PyAPI_FUNC(Py_ssize_t) PyUnstable_Long_CompactValue(const PyLongObject* op);
10032

Include/internal/pycore_long.h

+81-3
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,87 @@ static inline PyObject* _PyLong_FromUnsignedChar(unsigned char i)
7979
return (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS+i];
8080
}
8181

82-
extern PyObject *_PyLong_Add(PyLongObject *left, PyLongObject *right);
83-
extern PyObject *_PyLong_Multiply(PyLongObject *left, PyLongObject *right);
84-
extern PyObject *_PyLong_Subtract(PyLongObject *left, PyLongObject *right);
82+
// _PyLong_Frexp returns a double x and an exponent e such that the
83+
// true value is approximately equal to x * 2**e. e is >= 0. x is
84+
// 0.0 if and only if the input is 0 (in which case, e and x are both
85+
// zeroes); otherwise, 0.5 <= abs(x) < 1.0. On overflow, which is
86+
// possible if the number of bits doesn't fit into a Py_ssize_t, sets
87+
// OverflowError and returns -1.0 for x, 0 for e.
88+
//
89+
// Export for 'math' shared extension
90+
PyAPI_DATA(double) _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e);
91+
92+
extern PyObject* _PyLong_FromBytes(const char *, Py_ssize_t, int);
93+
94+
// _PyLong_DivmodNear. Given integers a and b, compute the nearest
95+
// integer q to the exact quotient a / b, rounding to the nearest even integer
96+
// in the case of a tie. Return (q, r), where r = a - q*b. The remainder r
97+
// will satisfy abs(r) <= abs(b)/2, with equality possible only if q is
98+
// even.
99+
//
100+
// Export for '_datetime' shared extension.
101+
PyAPI_DATA(PyObject*) _PyLong_DivmodNear(PyObject *, PyObject *);
102+
103+
// _PyLong_FromByteArray: View the n unsigned bytes as a binary integer in
104+
// base 256, and return a Python int with the same numeric value.
105+
// If n is 0, the integer is 0. Else:
106+
// If little_endian is 1/true, bytes[n-1] is the MSB and bytes[0] the LSB;
107+
// else (little_endian is 0/false) bytes[0] is the MSB and bytes[n-1] the
108+
// LSB.
109+
// If is_signed is 0/false, view the bytes as a non-negative integer.
110+
// If is_signed is 1/true, view the bytes as a 2's-complement integer,
111+
// non-negative if bit 0x80 of the MSB is clear, negative if set.
112+
// Error returns:
113+
// + Return NULL with the appropriate exception set if there's not
114+
// enough memory to create the Python int.
115+
//
116+
// Export for '_multibytecodec' shared extension.
117+
PyAPI_DATA(PyObject*) _PyLong_FromByteArray(
118+
const unsigned char* bytes, size_t n,
119+
int little_endian, int is_signed);
120+
121+
// _PyLong_AsByteArray: Convert the least-significant 8*n bits of long
122+
// v to a base-256 integer, stored in array bytes. Normally return 0,
123+
// return -1 on error.
124+
// If little_endian is 1/true, store the MSB at bytes[n-1] and the LSB at
125+
// bytes[0]; else (little_endian is 0/false) store the MSB at bytes[0] and
126+
// the LSB at bytes[n-1].
127+
// If is_signed is 0/false, it's an error if v < 0; else (v >= 0) n bytes
128+
// are filled and there's nothing special about bit 0x80 of the MSB.
129+
// If is_signed is 1/true, bytes is filled with the 2's-complement
130+
// representation of v's value. Bit 0x80 of the MSB is the sign bit.
131+
// Error returns (-1):
132+
// + is_signed is 0 and v < 0. TypeError is set in this case, and bytes
133+
// isn't altered.
134+
// + n isn't big enough to hold the full mathematical value of v. For
135+
// example, if is_signed is 0 and there are more digits in the v than
136+
// fit in n; or if is_signed is 1, v < 0, and n is just 1 bit shy of
137+
// being large enough to hold a sign bit. OverflowError is set in this
138+
// case, but bytes holds the least-significant n bytes of the true value.
139+
//
140+
// Export for '_struct' shared extension.
141+
PyAPI_DATA(int) _PyLong_AsByteArray(PyLongObject* v,
142+
unsigned char* bytes, size_t n,
143+
int little_endian, int is_signed);
144+
145+
// _PyLong_Format: Convert the long to a string object with given base,
146+
// appending a base prefix of 0[box] if base is 2, 8 or 16.
147+
// Export for '_tkinter' shared extension.
148+
PyAPI_DATA(PyObject*) _PyLong_Format(PyObject *obj, int base);
149+
150+
// For use by the math.gcd() function.
151+
// Export for 'math' shared extension.
152+
PyAPI_DATA(PyObject*) _PyLong_GCD(PyObject *, PyObject *);
153+
154+
// Export for 'math' shared extension
155+
PyAPI_DATA(PyObject*) _PyLong_Rshift(PyObject *, size_t);
156+
157+
// Export for 'math' shared extension
158+
PyAPI_DATA(PyObject*) _PyLong_Lshift(PyObject *, size_t);
159+
160+
extern PyObject* _PyLong_Add(PyLongObject *left, PyLongObject *right);
161+
extern PyObject* _PyLong_Multiply(PyLongObject *left, PyLongObject *right);
162+
extern PyObject* _PyLong_Subtract(PyLongObject *left, PyLongObject *right);
85163

86164
// Export for 'binascii' shared extension.
87165
PyAPI_DATA(unsigned char) _PyLong_DigitValue[256];

Modules/_io/_iomodule.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
*/
99

1010
#include "Python.h"
11-
#include "_iomodule.h"
12-
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1311
#include "pycore_initconfig.h" // _PyStatus_OK()
12+
#include "pycore_pystate.h" // _PyInterpreterState_GET()
13+
14+
#include "_iomodule.h"
1415

1516
#ifdef HAVE_SYS_TYPES_H
1617
#include <sys/types.h>

Modules/_pickle.c

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "Python.h"
1212
#include "pycore_bytesobject.h" // _PyBytesWriter
1313
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
14+
#include "pycore_long.h" // _PyLong_AsByteArray()
1415
#include "pycore_moduleobject.h" // _PyModule_GetState()
1516
#include "pycore_object.h" // _PyNone_Type
1617
#include "pycore_pystate.h" // _PyThreadState_GET()

Modules/_randommodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@
7171
#endif
7272

7373
#include "Python.h"
74+
#include "pycore_long.h" // _PyLong_AsByteArray()
7475
#include "pycore_moduleobject.h" // _PyModule_GetState()
7576
#include "pycore_pylifecycle.h" // _PyOS_URandomNonblock()
76-
#include "pycore_runtime.h"
7777
#ifdef HAVE_PROCESS_H
7878
# include <process.h> // getpid()
7979
#endif

Modules/_sqlite/util.c

+5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
* 3. This notice may not be removed or altered from any source distribution.
2222
*/
2323

24+
#ifndef Py_BUILD_CORE_BUILTIN
25+
# define Py_BUILD_CORE_MODULE 1
26+
#endif
27+
2428
#include "module.h"
29+
#include "pycore_long.h" // _PyLong_AsByteArray()
2530
#include "connection.h"
2631

2732
// Returns non-NULL if a new exception should be raised

Modules/_struct.c

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "Python.h"
1111
#include "pycore_bytesobject.h" // _PyBytesWriter
12+
#include "pycore_long.h" // _PyLong_AsByteArray()
1213
#include "pycore_moduleobject.h" // _PyModule_GetState()
1314

1415
#include <ctype.h>

Modules/arraymodule.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
#endif
99

1010
#include "Python.h"
11+
#include "pycore_bytesobject.h" // _PyBytes_Repeat
1112
#include "pycore_call.h" // _PyObject_CallMethod()
13+
#include "pycore_long.h" // _PyLong_FromByteArray()
1214
#include "pycore_moduleobject.h" // _PyModule_GetState()
13-
#include "pycore_bytesobject.h" // _PyBytes_Repeat
1415

1516
#include <stddef.h> // offsetof()
1617
#include <stdbool.h>

Modules/cjkcodecs/multibytecodec.c

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#endif
1010

1111
#include "Python.h"
12+
#include "pycore_long.h" // _PyLong_FromByteArray()
1213

1314
#include "multibytecodec.h"
1415
#include "clinic/multibytecodec.c.h"

Python/hamt.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include "Python.h"
2-
3-
#include "pycore_bitutils.h" // _Py_popcount32
2+
#include "pycore_bitutils.h" // _Py_popcount32()
43
#include "pycore_hamt.h"
54
#include "pycore_initconfig.h" // _PyStatus_OK()
5+
#include "pycore_long.h" // _PyLong_Format()
66
#include "pycore_object.h" // _PyObject_GC_TRACK()
7+
78
#include <stddef.h> // offsetof()
89

910
/*

0 commit comments

Comments
 (0)