gh-112026: Restore removed private C API (#112115) · python/cpython@5846924 · GitHub
Skip to content

Commit 5846924

Browse files
authored
gh-112026: Restore removed private C API (#112115)
Restore removed private C API functions, macros and structures which have no simple replacement for now: * _PyDict_GetItem_KnownHash() * _PyDict_NewPresized() * _PyHASH_BITS * _PyHASH_IMAG * _PyHASH_INF * _PyHASH_MODULUS * _PyHASH_MULTIPLIER * _PyLong_Copy() * _PyLong_FromDigits() * _PyLong_New() * _PyLong_Sign() * _PyObject_CallMethodId() * _PyObject_CallMethodNoArgs() * _PyObject_CallMethodOneArg() * _PyObject_CallOneArg() * _PyObject_EXTRA_INIT * _PyObject_FastCallDict() * _PyObject_GetAttrId() * _PyObject_Vectorcall() * _PyObject_VectorcallMethod() * _PyStack_AsDict() * _PyThread_CurrentFrames() * _PyUnicodeWriter structure * _PyUnicodeWriter_Dealloc() * _PyUnicodeWriter_Finish() * _PyUnicodeWriter_Init() * _PyUnicodeWriter_Prepare() * _PyUnicodeWriter_PrepareKind() * _PyUnicodeWriter_WriteASCIIString() * _PyUnicodeWriter_WriteChar() * _PyUnicodeWriter_WriteLatin1String() * _PyUnicodeWriter_WriteStr() * _PyUnicodeWriter_WriteSubstring() * _PyUnicode_AsString() * _PyUnicode_FromId() * _PyVectorcall_Function() * _Py_HashDouble() * _Py_HashPointer() * _Py_IDENTIFIER() * _Py_c_abs() * _Py_c_diff() * _Py_c_neg() * _Py_c_pow() * _Py_c_prod() * _Py_c_quot() * _Py_c_sum() * _Py_static_string() * _Py_static_string_init()
1 parent b338ffa commit 5846924

20 files changed

Lines changed: 315 additions & 236 deletions

Include/cpython/abstract.h

Lines changed: 32 additions & 0 deletions

Include/cpython/complexobject.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ typedef struct {
77
double imag;
88
} Py_complex;
99

10+
// Operations on complex numbers.
11+
PyAPI_FUNC(Py_complex) _Py_c_sum(Py_complex, Py_complex);
12+
PyAPI_FUNC(Py_complex) _Py_c_diff(Py_complex, Py_complex);
13+
PyAPI_FUNC(Py_complex) _Py_c_neg(Py_complex);
14+
PyAPI_FUNC(Py_complex) _Py_c_prod(Py_complex, Py_complex);
15+
PyAPI_FUNC(Py_complex) _Py_c_quot(Py_complex, Py_complex);
16+
PyAPI_FUNC(Py_complex) _Py_c_pow(Py_complex, Py_complex);
17+
PyAPI_FUNC(double) _Py_c_abs(Py_complex);
18+
19+
1020
/* Complex object interface */
1121

1222
/*

Include/cpython/dictobject.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ typedef struct {
3232
PyDictValues *ma_values;
3333
} PyDictObject;
3434

35+
PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
36+
Py_hash_t hash);
37+
3538
PyAPI_FUNC(PyObject *) PyDict_SetDefault(
3639
PyObject *mp, PyObject *key, PyObject *defaultobj);
3740

@@ -46,6 +49,8 @@ static inline Py_ssize_t PyDict_GET_SIZE(PyObject *op) {
4649

4750
PyAPI_FUNC(int) PyDict_ContainsString(PyObject *mp, const char *key);
4851

52+
PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
53+
4954
PyAPI_FUNC(int) PyDict_Pop(PyObject *dict, PyObject *key, PyObject **result);
5055
PyAPI_FUNC(int) PyDict_PopString(PyObject *dict, const char *key, PyObject **result);
5156
PyAPI_FUNC(PyObject *) _PyDict_Pop(PyObject *dict, PyObject *key, PyObject *default_value);

Include/cpython/longintrepr.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ struct _longobject {
8989
_PyLongValue long_value;
9090
};
9191

92+
PyAPI_FUNC(PyLongObject*) _PyLong_New(Py_ssize_t);
93+
94+
// Return a copy of src.
95+
PyAPI_FUNC(PyObject*) _PyLong_Copy(PyLongObject *src);
96+
97+
PyAPI_FUNC(PyLongObject*) _PyLong_FromDigits(
98+
int negative,
99+
Py_ssize_t digit_count,
100+
digit *digits);
101+
92102

93103
/* Inline some internals for speed. These should be in pycore_long.h
94104
* if user code didn't need them inlined. */

Include/cpython/longobject.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ 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_Sign. Return 0 if v is 0, -1 if v < 0, +1 if v > 0.
11+
// v must not be NULL, and must be a normalized long.
12+
// There are no error cases.
13+
PyAPI_FUNC(int) _PyLong_Sign(PyObject *v);
14+
1015
/* _PyLong_FromByteArray: View the n unsigned bytes as a binary integer in
1116
base 256, and return a Python int with the same numeric value.
1217
If n is 0, the integer is 0. Else:

Include/cpython/object.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,44 @@ PyAPI_FUNC(Py_ssize_t) _PyInterpreterState_GetRefTotal(PyInterpreterState *);
1414
#endif
1515

1616

17+
/********************* String Literals ****************************************/
18+
/* This structure helps managing static strings. The basic usage goes like this:
19+
Instead of doing
20+
21+
r = PyObject_CallMethod(o, "foo", "args", ...);
22+
23+
do
24+
25+
_Py_IDENTIFIER(foo);
26+
...
27+
r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...);
28+
29+
PyId_foo is a static variable, either on block level or file level. On first
30+
usage, the string "foo" is interned, and the structures are linked. On interpreter
31+
shutdown, all strings are released.
32+
33+
Alternatively, _Py_static_string allows choosing the variable name.
34+
_PyUnicode_FromId returns a borrowed reference to the interned string.
35+
_PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*.
36+
*/
37+
typedef struct _Py_Identifier {
38+
const char* string;
39+
// Index in PyInterpreterState.unicode.ids.array. It is process-wide
40+
// unique and must be initialized to -1.
41+
Py_ssize_t index;
42+
} _Py_Identifier;
43+
44+
#ifndef Py_BUILD_CORE
45+
// For now we are keeping _Py_IDENTIFIER for continued use
46+
// in non-builtin extensions (and naughty PyPI modules).
47+
48+
#define _Py_static_string_init(value) { .string = (value), .index = -1 }
49+
#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
50+
#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
51+
52+
#endif /* !Py_BUILD_CORE */
53+
54+
1755
typedef struct {
1856
/* Number implementations must check *both*
1957
arguments for proper type and implement the necessary conversions
@@ -238,6 +276,8 @@ PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
238276
PyAPI_FUNC(void) _Py_BreakPoint(void);
239277
PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
240278

279+
PyAPI_FUNC(PyObject*) _PyObject_GetAttrId(PyObject *, _Py_Identifier *);
280+
241281
PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
242282
PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *);
243283
PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);

Include/cpython/pyhash.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22
# error "this header file must not be included directly"
33
#endif
44

5+
/* Prime multiplier used in string and various other hashes. */
6+
#define _PyHASH_MULTIPLIER 1000003UL /* 0xf4243 */
7+
8+
/* Parameters used for the numeric hash implementation. See notes for
9+
_Py_HashDouble in Python/pyhash.c. Numeric hashes are based on
10+
reduction modulo the prime 2**_PyHASH_BITS - 1. */
11+
12+
#if SIZEOF_VOID_P >= 8
13+
# define _PyHASH_BITS 61
14+
#else
15+
# define _PyHASH_BITS 31
16+
#endif
17+
18+
#define _PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
19+
#define _PyHASH_INF 314159
20+
#define _PyHASH_IMAG _PyHASH_MULTIPLIER
21+
22+
/* Helpers for hash functions */
23+
PyAPI_FUNC(Py_hash_t) _Py_HashDouble(PyObject *, double);
24+
PyAPI_FUNC(Py_hash_t) _Py_HashPointer(const void*);
25+
26+
527
/* hash function definition */
628
typedef struct {
729
Py_hash_t (*const hash)(const void *, Py_ssize_t);

Include/cpython/pystate.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ PyAPI_FUNC(void) PyThreadState_LeaveTracing(PyThreadState *tstate);
247247
The function returns 1 if _PyGILState_check_enabled is non-zero. */
248248
PyAPI_FUNC(int) PyGILState_Check(void);
249249

250+
/* The implementation of sys._current_frames() Returns a dict mapping
251+
thread id to that thread's current frame.
252+
*/
253+
PyAPI_FUNC(PyObject*) _PyThread_CurrentFrames(void);
254+
250255
/* Routines for advanced debuggers, requested by David Beazley.
251256
Don't use unless you know what you are doing! */
252257
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void);

Include/cpython/unicodeobject.h

Lines changed: 131 additions & 0 deletions

0 commit comments

Comments
 (0)