Skip to content

Commit 2a07ff9

Browse files
authored
gh-148659: Export some internal functions for the JIT (PEP-523) (#148634)
Export (as internal functions, not public ones) C API functions necessary to implement a JIT as a separate extension module.
1 parent 2faceee commit 2a07ff9

File tree

11 files changed

+63
-34
lines changed

11 files changed

+63
-34
lines changed

Include/internal/pycore_abstract.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ _PyIndex_Check(PyObject *obj)
1616
return (tp_as_number != NULL && tp_as_number->nb_index != NULL);
1717
}
1818

19-
PyObject *_PyNumber_PowerNoMod(PyObject *lhs, PyObject *rhs);
20-
PyObject *_PyNumber_InPlacePowerNoMod(PyObject *lhs, PyObject *rhs);
19+
// Exported for external JIT support
20+
PyAPI_FUNC(PyObject *) _PyNumber_PowerNoMod(PyObject *lhs, PyObject *rhs);
21+
PyAPI_FUNC(PyObject *) _PyNumber_InPlacePowerNoMod(PyObject *lhs, PyObject *rhs);
2122

22-
extern int _PyObject_HasLen(PyObject *o);
23+
PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
2324

2425
/* === Sequence protocol ================================================ */
2526

Include/internal/pycore_call.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ _PyStack_UnpackDict(PyThreadState *tstate,
166166
PyObject *const *args, Py_ssize_t nargs,
167167
PyObject *kwargs, PyObject **p_kwnames);
168168

169-
extern void _PyStack_UnpackDict_Free(
169+
// Exported for external JIT support
170+
PyAPI_FUNC(void) _PyStack_UnpackDict_Free(
170171
PyObject *const *stack,
171172
Py_ssize_t nargs,
172173
PyObject *kwnames);

Include/internal/pycore_dict.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ PyAPI_FUNC(int) _PyDict_SetItem_KnownHash(PyObject *mp, PyObject *key,
3131
PyAPI_FUNC(int) _PyDict_DelItem_KnownHash(PyObject *mp, PyObject *key,
3232
Py_hash_t hash);
3333

34-
extern int _PyDict_DelItem_KnownHash_LockHeld(PyObject *mp, PyObject *key,
34+
// Exported for external JIT support
35+
PyAPI_FUNC(int) _PyDict_DelItem_KnownHash_LockHeld(PyObject *mp, PyObject *key,
3536
Py_hash_t hash);
3637

3738
extern int _PyDict_Contains_KnownHash(PyObject *, PyObject *, Py_hash_t);
@@ -93,8 +94,9 @@ extern PyObject *_PyDict_Or(PyObject *self, PyObject *other);
9394
extern PyObject *_PyDict_IOr(PyObject *self, PyObject *other);
9495

9596
/* Gets a version number unique to the current state of the keys of dict, if possible.
96-
* Returns the version number, or zero if it was not possible to get a version number. */
97-
extern uint32_t _PyDictKeys_GetVersionForCurrentState(
97+
* Returns the version number, or zero if it was not possible to get a version number.
98+
* Exported for external JIT support */
99+
PyAPI_FUNC(uint32_t) _PyDictKeys_GetVersionForCurrentState(
98100
PyInterpreterState *interp, PyDictKeysObject *dictkeys);
99101

100102
/* Gets a version number unique to the current state of the keys of dict, if possible.
@@ -104,8 +106,9 @@ extern uint32_t _PyDictKeys_GetVersionForCurrentState(
104106
*
105107
* The caller must hold the per-object lock on dict.
106108
*
107-
* Returns the version number, or zero if it was not possible to get a version number. */
108-
extern uint32_t _PyDict_GetKeysVersionForCurrentState(
109+
* Returns the version number, or zero if it was not possible to get a version number.
110+
* Exported for external JIT support */
111+
PyAPI_FUNC(uint32_t) _PyDict_GetKeysVersionForCurrentState(
109112
PyInterpreterState *interp, PyDictObject *dict);
110113

111114
extern size_t _PyDict_KeysSize(PyDictKeysObject *keys);
@@ -114,16 +117,18 @@ extern void _PyDictKeys_DecRef(PyDictKeysObject *keys);
114117

115118
/* _Py_dict_lookup() returns index of entry which can be used like DK_ENTRIES(dk)[index].
116119
* -1 when no entry found, -3 when compare raises error.
120+
* Exported for external JIT support
117121
*/
118-
extern Py_ssize_t _Py_dict_lookup(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject **value_addr);
122+
PyAPI_FUNC(Py_ssize_t) _Py_dict_lookup(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject **value_addr);
119123
extern Py_ssize_t _Py_dict_lookup_threadsafe(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject **value_addr);
120124
extern Py_ssize_t _Py_dict_lookup_threadsafe_stackref(PyDictObject *mp, PyObject *key, Py_hash_t hash, _PyStackRef *value_addr);
121125

122126
extern int _PyDict_GetMethodStackRef(PyDictObject *dict, PyObject *name, _PyStackRef *method);
123127

124-
extern Py_ssize_t _PyDict_LookupIndexAndValue(PyDictObject *, PyObject *, PyObject **);
125-
extern Py_ssize_t _PyDict_LookupIndex(PyDictObject *, PyObject *);
126-
extern Py_ssize_t _PyDictKeys_StringLookup(PyDictKeysObject* dictkeys, PyObject *key);
128+
// Exported for external JIT support
129+
PyAPI_FUNC(Py_ssize_t) _PyDict_LookupIndexAndValue(PyDictObject *, PyObject *, PyObject **);
130+
PyAPI_FUNC(Py_ssize_t) _PyDict_LookupIndex(PyDictObject *, PyObject *);
131+
PyAPI_FUNC(Py_ssize_t) _PyDictKeys_StringLookup(PyDictKeysObject* dictkeys, PyObject *key);
127132

128133
/* Look up a string key in an all unicode dict keys, assign the keys object a version, and
129134
* store it in version.
@@ -132,9 +137,11 @@ extern Py_ssize_t _PyDictKeys_StringLookup(PyDictKeysObject* dictkeys, PyObject
132137
* strings.
133138
*
134139
* Returns DKIX_EMPTY if the key is not present.
140+
*
141+
* Exported for external JIT support
135142
*/
136-
extern Py_ssize_t _PyDictKeys_StringLookupAndVersion(PyDictKeysObject* dictkeys, PyObject *key, uint32_t *version);
137-
extern Py_ssize_t _PyDictKeys_StringLookupSplit(PyDictKeysObject* dictkeys, PyObject *key);
143+
PyAPI_FUNC(Py_ssize_t) _PyDictKeys_StringLookupAndVersion(PyDictKeysObject* dictkeys, PyObject *key, uint32_t *version);
144+
PyAPI_FUNC(Py_ssize_t) _PyDictKeys_StringLookupSplit(PyDictKeysObject* dictkeys, PyObject *key);
138145
PyAPI_FUNC(PyObject *)_PyDict_LoadGlobal(PyDictObject *, PyDictObject *, PyObject *);
139146
PyAPI_FUNC(void) _PyDict_LoadGlobalStackRef(PyDictObject *, PyDictObject *, PyObject *, _PyStackRef *);
140147

@@ -144,7 +151,8 @@ extern PyObject *_PyDict_LoadBuiltinsFromGlobals(PyObject *globals);
144151
/* Consumes references to key and value */
145152
PyAPI_FUNC(int) _PyDict_SetItem_Take2(PyDictObject *op, PyObject *key, PyObject *value);
146153
PyAPI_FUNC(int) _PyDict_SetItem_Take2_KnownHash(PyDictObject *op, PyObject *key, PyObject *value, Py_hash_t hash);
147-
extern int _PyDict_SetItem_LockHeld(PyDictObject *dict, PyObject *name, PyObject *value);
154+
// Exported for external JIT support
155+
PyAPI_FUNC(int) _PyDict_SetItem_LockHeld(PyDictObject *dict, PyObject *name, PyObject *value);
148156
// Export for '_asyncio' shared extension
149157
PyAPI_FUNC(int) _PyDict_SetItem_KnownHash_LockHeld(PyDictObject *mp, PyObject *key,
150158
PyObject *value, Py_hash_t hash);
@@ -329,6 +337,10 @@ _PyDictValues_AddToInsertionOrder(PyDictValues *values, Py_ssize_t ix)
329337
values->size = size+1;
330338
}
331339

340+
// Exported for external JIT support
341+
PyAPI_FUNC(void)
342+
_PyDict_InsertSplitValue(PyDictObject *mp, PyObject *key, PyObject *value, Py_ssize_t ix);
343+
332344
static inline size_t
333345
shared_keys_usable_size(PyDictKeysObject *keys)
334346
{

Include/internal/pycore_frame.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ struct _frame {
3838
PyObject *_f_frame_data[1];
3939
};
4040

41-
extern PyFrameObject* _PyFrame_New_NoTrack(PyCodeObject *code);
41+
// Exported for external JIT support
42+
PyAPI_FUNC(PyFrameObject *) _PyFrame_New_NoTrack(PyCodeObject *code);
4243

4344

4445
/* other API */

Include/internal/pycore_function.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ _PyFunction_IsVersionValid(uint32_t version)
2727
return version >= FUNC_VERSION_FIRST_VALID;
2828
}
2929

30-
extern uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func);
30+
// Exported for external JIT support
31+
PyAPI_FUNC(uint32_t) _PyFunction_GetVersionForCurrentState(PyFunctionObject *func);
3132
PyAPI_FUNC(void) _PyFunction_SetVersion(PyFunctionObject *func, uint32_t version);
3233
void _PyFunction_ClearCodeByVersion(uint32_t version);
3334

Include/internal/pycore_interpframe.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ _PyFrame_GetFrameObject(_PyInterpreterFrame *frame)
302302
return _PyFrame_MakeAndSetFrameObject(frame);
303303
}
304304

305-
void
305+
// Exported for external JIT support
306+
PyAPI_FUNC(void)
306307
_PyFrame_ClearLocals(_PyInterpreterFrame *frame);
307308

308309
/* Clears all references in the frame.
@@ -313,8 +314,10 @@ _PyFrame_ClearLocals(_PyInterpreterFrame *frame);
313314
* in the frame.
314315
* take should be set to 1 for heap allocated
315316
* frames like the ones in generators and coroutines.
317+
*
318+
* Exported for external JIT support
316319
*/
317-
void
320+
PyAPI_FUNC(void)
318321
_PyFrame_ClearExceptCode(_PyInterpreterFrame * frame);
319322

320323
int
@@ -338,7 +341,8 @@ _PyThreadState_HasStackSpace(PyThreadState *tstate, int size)
338341
size < tstate->datastack_limit - tstate->datastack_top;
339342
}
340343

341-
extern _PyInterpreterFrame *
344+
// Exported for external JIT support
345+
PyAPI_FUNC(_PyInterpreterFrame *)
342346
_PyThreadState_PushFrame(PyThreadState *tstate, size_t size);
343347

344348
PyAPI_FUNC(void) _PyThreadState_PopFrame(PyThreadState *tstate, _PyInterpreterFrame *frame);

Include/internal/pycore_object.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -879,14 +879,16 @@ PyAPI_FUNC(PyObject *) _PyType_NewManagedObject(PyTypeObject *type);
879879
extern PyTypeObject* _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
880880
extern PyObject* _PyType_GetDocFromInternalDoc(const char *, const char *);
881881
extern PyObject* _PyType_GetTextSignatureFromInternalDoc(const char *, const char *, int);
882-
extern int _PyObject_SetAttributeErrorContext(PyObject *v, PyObject* name);
882+
// Exported for external JIT support
883+
PyAPI_FUNC(int) _PyObject_SetAttributeErrorContext(PyObject *v, PyObject* name);
883884

884885
void _PyObject_InitInlineValues(PyObject *obj, PyTypeObject *tp);
885886
extern int _PyObject_StoreInstanceAttribute(PyObject *obj,
886887
PyObject *name, PyObject *value);
887888
extern bool _PyObject_TryGetInstanceAttribute(PyObject *obj, PyObject *name,
888889
PyObject **attr);
889-
extern PyObject *_PyType_LookupRefAndVersion(PyTypeObject *, PyObject *,
890+
// Exported for external JIT support
891+
PyAPI_FUNC(PyObject *) _PyType_LookupRefAndVersion(PyTypeObject *, PyObject *,
890892
unsigned int *);
891893

892894
// Internal API to look for a name through the MRO.
@@ -910,7 +912,9 @@ PyAPI_FUNC(_PyStackRef) _PyObject_GetAttrStackRef(PyObject *obj, PyObject *name)
910912
// deferred reference counting.
911913
//
912914
// Returns 1 if the value was cached or 0 otherwise.
913-
extern int _PyType_CacheInitForSpecialization(PyHeapTypeObject *type,
915+
//
916+
// Exported for external JIT support
917+
PyAPI_FUNC(int) _PyType_CacheInitForSpecialization(PyHeapTypeObject *type,
914918
PyObject *init,
915919
unsigned int tp_version);
916920

Include/internal/pycore_pyerrors.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ extern PyObject* _PyErr_FormatFromCauseTstate(
170170
const char *format,
171171
...);
172172

173-
extern PyObject* _PyExc_CreateExceptionGroup(
173+
// Exported for external JIT support
174+
PyAPI_FUNC(PyObject *) _PyExc_CreateExceptionGroup(
174175
const char *msg,
175176
PyObject *excs);
176177

@@ -181,7 +182,8 @@ extern PyObject* _PyExc_PrepReraiseStar(
181182
extern int _PyErr_CheckSignalsTstate(PyThreadState *tstate);
182183

183184
extern void _Py_DumpExtensionModules(int fd, PyInterpreterState *interp);
184-
extern PyObject* _Py_CalculateSuggestions(PyObject *dir, PyObject *name);
185+
// Exported for external JIT support
186+
PyAPI_FUNC(PyObject *) _Py_CalculateSuggestions(PyObject *dir, PyObject *name);
185187
extern PyObject* _Py_Offer_Suggestions(PyObject* exception);
186188

187189
// Export for '_testinternalcapi' shared extension

Include/internal/pycore_traceback.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ extern void _Py_DumpHexadecimal(
8585
uintptr_t value,
8686
Py_ssize_t width);
8787

88-
extern PyObject* _PyTraceBack_FromFrame(
88+
// Exported for external JIT support
89+
PyAPI_FUNC(PyObject *) _PyTraceBack_FromFrame(
8990
PyObject *tb_next,
9091
PyFrameObject *frame);
9192

Include/internal/pycore_typeobject.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ extern void _PyStaticType_FiniBuiltin(
6060
extern void _PyStaticType_ClearWeakRefs(
6161
PyInterpreterState *interp,
6262
PyTypeObject *type);
63-
extern managed_static_type_state * _PyStaticType_GetState(
63+
// Exported for external JIT support
64+
PyAPI_FUNC(managed_static_type_state *) _PyStaticType_GetState(
6465
PyInterpreterState *interp,
6566
PyTypeObject *type);
6667

@@ -156,8 +157,9 @@ typedef int (*_py_validate_type)(PyTypeObject *);
156157
// It will verify the ``ty`` through user-defined validation function ``validate``,
157158
// and if the validation is passed, it will set the ``tp_version`` as valid
158159
// tp_version_tag from the ``ty``.
159-
extern int _PyType_Validate(PyTypeObject *ty, _py_validate_type validate, unsigned int *tp_version);
160-
extern int _PyType_CacheGetItemForSpecialization(PyHeapTypeObject *ht, PyObject *descriptor, uint32_t tp_version);
160+
// Exported for external JIT support
161+
int _PyType_Validate(PyTypeObject *ty, _py_validate_type validate, unsigned int *tp_version);
162+
int _PyType_CacheGetItemForSpecialization(PyHeapTypeObject *ht, PyObject *descriptor, uint32_t tp_version);
161163

162164
// Precalculates count of non-unique slots and fills wrapperbase.name_count.
163165
extern int _PyType_InitSlotDefs(PyInterpreterState *interp);

0 commit comments

Comments
 (0)