Skip to content

Commit a8a89fc

Browse files
authoredOct 25, 2023
gh-106320: Re-add some PyLong/PyDict C-API functions (GH-#111162)
* gh-106320: Re-add _PyLong_FromByteArray(), _PyLong_AsByteArray() and _PyLong_GCD() to the public header files since they are used by third-party packages and there is no efficient replacement. See #111140 See #111139 * gh-111262: Re-add _PyDict_Pop() to have a C-API until a new public one is designed.
1 parent b2ba298 commit a8a89fc

File tree

10 files changed

+44
-55
lines changed

10 files changed

+44
-55
lines changed
 

‎Include/cpython/dictobject.h

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static inline Py_ssize_t PyDict_GET_SIZE(PyObject *op) {
4646

4747
PyAPI_FUNC(int) PyDict_ContainsString(PyObject *mp, const char *key);
4848

49+
PyAPI_FUNC(PyObject *) _PyDict_Pop(PyObject *dict, PyObject *key, PyObject *default_value);
4950

5051
/* Dictionary watchers */
5152

‎Include/cpython/longobject.h

+42
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,45 @@ PyAPI_FUNC(PyObject*) PyLong_FromUnicodeObject(PyObject *u, int base);
77
PyAPI_FUNC(int) PyUnstable_Long_IsCompact(const PyLongObject* op);
88
PyAPI_FUNC(Py_ssize_t) PyUnstable_Long_CompactValue(const PyLongObject* op);
99

10+
/* _PyLong_FromByteArray: View the n unsigned bytes as a binary integer in
11+
base 256, and return a Python int with the same numeric value.
12+
If n is 0, the integer is 0. Else:
13+
If little_endian is 1/true, bytes[n-1] is the MSB and bytes[0] the LSB;
14+
else (little_endian is 0/false) bytes[0] is the MSB and bytes[n-1] the
15+
LSB.
16+
If is_signed is 0/false, view the bytes as a non-negative integer.
17+
If is_signed is 1/true, view the bytes as a 2's-complement integer,
18+
non-negative if bit 0x80 of the MSB is clear, negative if set.
19+
Error returns:
20+
+ Return NULL with the appropriate exception set if there's not
21+
enough memory to create the Python int.
22+
*/
23+
PyAPI_FUNC(PyObject *) _PyLong_FromByteArray(
24+
const unsigned char* bytes, size_t n,
25+
int little_endian, int is_signed);
26+
27+
/* _PyLong_AsByteArray: Convert the least-significant 8*n bits of long
28+
v to a base-256 integer, stored in array bytes. Normally return 0,
29+
return -1 on error.
30+
If little_endian is 1/true, store the MSB at bytes[n-1] and the LSB at
31+
bytes[0]; else (little_endian is 0/false) store the MSB at bytes[0] and
32+
the LSB at bytes[n-1].
33+
If is_signed is 0/false, it's an error if v < 0; else (v >= 0) n bytes
34+
are filled and there's nothing special about bit 0x80 of the MSB.
35+
If is_signed is 1/true, bytes is filled with the 2's-complement
36+
representation of v's value. Bit 0x80 of the MSB is the sign bit.
37+
Error returns (-1):
38+
+ is_signed is 0 and v < 0. TypeError is set in this case, and bytes
39+
isn't altered.
40+
+ n isn't big enough to hold the full mathematical value of v. For
41+
example, if is_signed is 0 and there are more digits in the v than
42+
fit in n; or if is_signed is 1, v < 0, and n is just 1 bit shy of
43+
being large enough to hold a sign bit. OverflowError is set in this
44+
case, but bytes holds the least-significant n bytes of the true value.
45+
*/
46+
PyAPI_FUNC(int) _PyLong_AsByteArray(PyLongObject* v,
47+
unsigned char* bytes, size_t n,
48+
int little_endian, int is_signed);
49+
50+
/* For use by the gcd function in mathmodule.c */
51+
PyAPI_FUNC(PyObject *) _PyLong_GCD(PyObject *, PyObject *);

‎Include/internal/pycore_dict.h

-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ extern PyObject* _PyDict_NewPresized(Py_ssize_t minused);
4949
// Export for '_ctypes' shared extension
5050
PyAPI_FUNC(Py_ssize_t) _PyDict_SizeOf(PyDictObject *);
5151

52-
// Export for '_socket' shared extension (Windows remove_unusable_flags())
53-
PyAPI_FUNC(PyObject*) _PyDict_Pop(PyObject *, PyObject *, PyObject *);
54-
5552
#define _PyDict_HasSplitTable(d) ((d)->ma_values != NULL)
5653

5754
/* Like PyDict_Merge, but override can be 0, 1 or 2. If override is 0,

‎Include/internal/pycore_long.h

-46
Original file line numberDiff line numberDiff line change
@@ -128,57 +128,11 @@ extern PyObject* _PyLong_FromBytes(const char *, Py_ssize_t, int);
128128
// Export for '_datetime' shared extension.
129129
PyAPI_DATA(PyObject*) _PyLong_DivmodNear(PyObject *, PyObject *);
130130

131-
// _PyLong_FromByteArray: View the n unsigned bytes as a binary integer in
132-
// base 256, and return a Python int with the same numeric value.
133-
// If n is 0, the integer is 0. Else:
134-
// If little_endian is 1/true, bytes[n-1] is the MSB and bytes[0] the LSB;
135-
// else (little_endian is 0/false) bytes[0] is the MSB and bytes[n-1] the
136-
// LSB.
137-
// If is_signed is 0/false, view the bytes as a non-negative integer.
138-
// If is_signed is 1/true, view the bytes as a 2's-complement integer,
139-
// non-negative if bit 0x80 of the MSB is clear, negative if set.
140-
// Error returns:
141-
// + Return NULL with the appropriate exception set if there's not
142-
// enough memory to create the Python int.
143-
//
144-
// Export for '_multibytecodec' shared extension.
145-
PyAPI_DATA(PyObject*) _PyLong_FromByteArray(
146-
const unsigned char* bytes, size_t n,
147-
int little_endian, int is_signed);
148-
149-
// _PyLong_AsByteArray: Convert the least-significant 8*n bits of long
150-
// v to a base-256 integer, stored in array bytes. Normally return 0,
151-
// return -1 on error.
152-
// If little_endian is 1/true, store the MSB at bytes[n-1] and the LSB at
153-
// bytes[0]; else (little_endian is 0/false) store the MSB at bytes[0] and
154-
// the LSB at bytes[n-1].
155-
// If is_signed is 0/false, it's an error if v < 0; else (v >= 0) n bytes
156-
// are filled and there's nothing special about bit 0x80 of the MSB.
157-
// If is_signed is 1/true, bytes is filled with the 2's-complement
158-
// representation of v's value. Bit 0x80 of the MSB is the sign bit.
159-
// Error returns (-1):
160-
// + is_signed is 0 and v < 0. TypeError is set in this case, and bytes
161-
// isn't altered.
162-
// + n isn't big enough to hold the full mathematical value of v. For
163-
// example, if is_signed is 0 and there are more digits in the v than
164-
// fit in n; or if is_signed is 1, v < 0, and n is just 1 bit shy of
165-
// being large enough to hold a sign bit. OverflowError is set in this
166-
// case, but bytes holds the least-significant n bytes of the true value.
167-
//
168-
// Export for '_struct' shared extension.
169-
PyAPI_DATA(int) _PyLong_AsByteArray(PyLongObject* v,
170-
unsigned char* bytes, size_t n,
171-
int little_endian, int is_signed);
172-
173131
// _PyLong_Format: Convert the long to a string object with given base,
174132
// appending a base prefix of 0[box] if base is 2, 8 or 16.
175133
// Export for '_tkinter' shared extension.
176134
PyAPI_DATA(PyObject*) _PyLong_Format(PyObject *obj, int base);
177135

178-
// For use by the math.gcd() function.
179-
// Export for 'math' shared extension.
180-
PyAPI_DATA(PyObject*) _PyLong_GCD(PyObject *, PyObject *);
181-
182136
// Export for 'math' shared extension
183137
PyAPI_DATA(PyObject*) _PyLong_Rshift(PyObject *, size_t);
184138

‎Modules/_randommodule.c

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

7373
#include "Python.h"
74-
#include "pycore_long.h" // _PyLong_AsByteArray()
74+
#include "pycore_long.h" // _PyLong_NumBits()
7575
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
7676
#include "pycore_moduleobject.h" // _PyModule_GetState()
7777
#include "pycore_pylifecycle.h" // _PyOS_URandomNonblock()

‎Modules/_threadmodule.c

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/* Interface to Sjoerd's portable C thread library */
44

55
#include "Python.h"
6-
#include "pycore_dict.h" // _PyDict_Pop()
76
#include "pycore_interp.h" // _PyInterpreterState.threads.count
87
#include "pycore_moduleobject.h" // _PyModule_GetState()
98
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()

‎Modules/arraymodule.c

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "pycore_bytesobject.h" // _PyBytes_Repeat
1212
#include "pycore_call.h" // _PyObject_CallMethod()
1313
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
14-
#include "pycore_long.h" // _PyLong_FromByteArray()
1514
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
1615
#include "pycore_moduleobject.h" // _PyModule_GetState()
1716

‎Modules/cjkcodecs/multibytecodec.c

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

1111
#include "Python.h"
12-
#include "pycore_long.h" // _PyLong_FromByteArray()
1312

1413
#include "multibytecodec.h"
1514
#include "clinic/multibytecodec.c.h"

‎Modules/socketmodule.c

-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ Local naming conventions:
107107

108108
#include "Python.h"
109109
#include "pycore_capsule.h" // _PyCapsule_SetTraverse()
110-
#include "pycore_dict.h" // _PyDict_Pop()
111110
#include "pycore_fileutils.h" // _Py_set_inheritable()
112111
#include "pycore_moduleobject.h" // _PyModule_GetState
113112

‎Python/import.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* Module definition and import implementation */
22

33
#include "Python.h"
4-
#include "pycore_dict.h" // _PyDict_Pop()
54
#include "pycore_hashtable.h" // _Py_hashtable_new_full()
65
#include "pycore_import.h" // _PyImport_BootstrapImp()
76
#include "pycore_initconfig.h" // _PyStatus_OK()

0 commit comments

Comments
 (0)
Please sign in to comment.