bpo-43268: Pass interp rather than tstate to internal functions (GH-24580)

Pass the current interpreter (interp) rather than the current Python
thread state (tstate) to internal functions which only use the
interpreter.

Modified functions:

* _PyXXX_Fini() and _PyXXX_ClearFreeList() functions
* _PyEval_SignalAsyncExc(), make_pending_calls()
* _PySys_GetObject(), sys_set_object(), sys_set_object_id(), sys_set_object_str()
* should_audit(), set_flags_from_config(), make_flags()
* _PyAtExit_Call()
* init_stdio_encoding()
* etc.
This commit is contained in:
Victor Stinner 2021-02-19 15:10:45 +01:00 committed by GitHub
parent a486054b24
commit bcb094b41f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 240 additions and 245 deletions

View File

@ -23,7 +23,7 @@ PyAPI_FUNC(int) _PyEval_AddPendingCall(
PyInterpreterState *interp, PyInterpreterState *interp,
int (*func)(void *), int (*func)(void *),
void *arg); void *arg);
PyAPI_FUNC(void) _PyEval_SignalAsyncExc(PyThreadState *tstate); PyAPI_FUNC(void) _PyEval_SignalAsyncExc(PyInterpreterState *interp);
#ifdef HAVE_FORK #ifdef HAVE_FORK
extern PyStatus _PyEval_ReInitThreads(PyThreadState *tstate); extern PyStatus _PyEval_ReInitThreads(PyThreadState *tstate);
#endif #endif
@ -55,7 +55,7 @@ extern int _PyEval_ThreadsInitialized(PyInterpreterState *interp);
extern int _PyEval_ThreadsInitialized(struct pyruntimestate *runtime); extern int _PyEval_ThreadsInitialized(struct pyruntimestate *runtime);
#endif #endif
extern PyStatus _PyEval_InitGIL(PyThreadState *tstate); extern PyStatus _PyEval_InitGIL(PyThreadState *tstate);
extern void _PyEval_FiniGIL(PyThreadState *tstate); extern void _PyEval_FiniGIL(PyInterpreterState *interp);
extern void _PyEval_ReleaseLock(PyThreadState *tstate); extern void _PyEval_ReleaseLock(PyThreadState *tstate);

View File

@ -37,6 +37,6 @@ struct _pycontexttokenobject {
int _PyContext_Init(void); int _PyContext_Init(void);
void _PyContext_Fini(PyThreadState *tstate); void _PyContext_Fini(PyInterpreterState *interp);
#endif /* !Py_INTERNAL_CONTEXT_H */ #endif /* !Py_INTERNAL_CONTEXT_H */

View File

@ -167,13 +167,13 @@ extern Py_ssize_t _PyGC_CollectNoFail(PyThreadState *tstate);
// Functions to clear types free lists // Functions to clear types free lists
extern void _PyFrame_ClearFreeList(PyThreadState *tstate); extern void _PyFrame_ClearFreeList(PyInterpreterState *interp);
extern void _PyTuple_ClearFreeList(PyThreadState *tstate); extern void _PyTuple_ClearFreeList(PyInterpreterState *interp);
extern void _PyFloat_ClearFreeList(PyThreadState *tstate); extern void _PyFloat_ClearFreeList(PyInterpreterState *interp);
extern void _PyList_ClearFreeList(PyThreadState *tstate); extern void _PyList_ClearFreeList(PyInterpreterState *interp);
extern void _PyDict_ClearFreeList(PyThreadState *tstate); extern void _PyDict_ClearFreeList(PyInterpreterState *interp);
extern void _PyAsyncGen_ClearFreeLists(PyThreadState *tstate); extern void _PyAsyncGen_ClearFreeLists(PyInterpreterState *interp);
extern void _PyContext_ClearFreeList(PyThreadState *tstate); extern void _PyContext_ClearFreeList(PyInterpreterState *interp);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -31,21 +31,21 @@ PyAPI_FUNC(int) _Py_IsLocaleCoercionTarget(const char *ctype_loc);
/* Various one-time initializers */ /* Various one-time initializers */
extern PyStatus _PyUnicode_Init(PyThreadState *tstate); extern PyStatus _PyUnicode_Init(PyInterpreterState *interp);
extern PyStatus _PyBytes_Init(PyThreadState *tstate); extern PyStatus _PyBytes_Init(PyInterpreterState *interp);
extern int _PyStructSequence_Init(void); extern int _PyStructSequence_Init(void);
extern int _PyLong_Init(PyThreadState *tstate); extern int _PyLong_Init(PyInterpreterState *interp);
extern PyStatus _PyTuple_Init(PyThreadState *tstate); extern PyStatus _PyTuple_Init(PyInterpreterState *interp);
extern PyStatus _PyFaulthandler_Init(int enable); extern PyStatus _PyFaulthandler_Init(int enable);
extern int _PyTraceMalloc_Init(int enable); extern int _PyTraceMalloc_Init(int enable);
extern PyObject * _PyBuiltin_Init(PyThreadState *tstate); extern PyObject * _PyBuiltin_Init(PyInterpreterState *interp);
extern PyStatus _PySys_Create( extern PyStatus _PySys_Create(
PyThreadState *tstate, PyThreadState *tstate,
PyObject **sysmod_p); PyObject **sysmod_p);
extern PyStatus _PySys_ReadPreinitWarnOptions(PyWideStringList *options); extern PyStatus _PySys_ReadPreinitWarnOptions(PyWideStringList *options);
extern PyStatus _PySys_ReadPreinitXOptions(PyConfig *config); extern PyStatus _PySys_ReadPreinitXOptions(PyConfig *config);
extern int _PySys_UpdateConfig(PyThreadState *tstate); extern int _PySys_UpdateConfig(PyThreadState *tstate);
extern PyStatus _PyExc_Init(PyThreadState *tstate); extern PyStatus _PyExc_Init(PyInterpreterState *interp);
extern PyStatus _PyErr_Init(void); extern PyStatus _PyErr_Init(void);
extern PyStatus _PyBuiltins_AddExceptions(PyObject * bltinmod); extern PyStatus _PyBuiltins_AddExceptions(PyObject * bltinmod);
extern int _PyFloat_Init(void); extern int _PyFloat_Init(void);
@ -54,33 +54,33 @@ extern PyStatus _Py_HashRandomization_Init(const PyConfig *);
extern PyStatus _PyTypes_Init(void); extern PyStatus _PyTypes_Init(void);
extern PyStatus _PyTypes_InitSlotDefs(void); extern PyStatus _PyTypes_InitSlotDefs(void);
extern PyStatus _PyImportZip_Init(PyThreadState *tstate); extern PyStatus _PyImportZip_Init(PyThreadState *tstate);
extern PyStatus _PyGC_Init(PyThreadState *tstate); extern PyStatus _PyGC_Init(PyInterpreterState *interp);
extern PyStatus _PyAtExit_Init(PyThreadState *tstate); extern PyStatus _PyAtExit_Init(PyInterpreterState *interp);
/* Various internal finalizers */ /* Various internal finalizers */
extern void _PyFrame_Fini(PyThreadState *tstate); extern void _PyFrame_Fini(PyInterpreterState *interp);
extern void _PyDict_Fini(PyThreadState *tstate); extern void _PyDict_Fini(PyInterpreterState *interp);
extern void _PyTuple_Fini(PyThreadState *tstate); extern void _PyTuple_Fini(PyInterpreterState *interp);
extern void _PyList_Fini(PyThreadState *tstate); extern void _PyList_Fini(PyInterpreterState *interp);
extern void _PyBytes_Fini(PyThreadState *tstate); extern void _PyBytes_Fini(PyInterpreterState *interp);
extern void _PyFloat_Fini(PyThreadState *tstate); extern void _PyFloat_Fini(PyInterpreterState *interp);
extern void _PySlice_Fini(PyThreadState *tstate); extern void _PySlice_Fini(PyInterpreterState *interp);
extern void _PyAsyncGen_Fini(PyThreadState *tstate); extern void _PyAsyncGen_Fini(PyInterpreterState *interp);
extern int _PySignal_Init(int install_signal_handlers); extern int _PySignal_Init(int install_signal_handlers);
extern void _PySignal_Fini(void); extern void _PySignal_Fini(void);
extern void _PyExc_Fini(PyThreadState *tstate); extern void _PyExc_Fini(PyInterpreterState *interp);
extern void _PyImport_Fini(void); extern void _PyImport_Fini(void);
extern void _PyImport_Fini2(void); extern void _PyImport_Fini2(void);
extern void _PyGC_Fini(PyThreadState *tstate); extern void _PyGC_Fini(PyInterpreterState *interp);
extern void _PyType_Fini(PyThreadState *tstate); extern void _PyType_Fini(PyInterpreterState *interp);
extern void _Py_HashRandomization_Fini(void); extern void _Py_HashRandomization_Fini(void);
extern void _PyUnicode_Fini(PyThreadState *tstate); extern void _PyUnicode_Fini(PyInterpreterState *interp);
extern void _PyUnicode_ClearInterned(PyThreadState *tstate); extern void _PyUnicode_ClearInterned(PyInterpreterState *interp);
extern void _PyLong_Fini(PyThreadState *tstate); extern void _PyLong_Fini(PyInterpreterState *interp);
extern void _PyFaulthandler_Fini(void); extern void _PyFaulthandler_Fini(void);
extern void _PyHash_Fini(void); extern void _PyHash_Fini(void);
extern void _PyTraceMalloc_Fini(void); extern void _PyTraceMalloc_Fini(void);
@ -89,9 +89,9 @@ extern void _PyAST_Fini(PyInterpreterState *interp);
extern void _PyAtExit_Fini(PyInterpreterState *interp); extern void _PyAtExit_Fini(PyInterpreterState *interp);
extern PyStatus _PyGILState_Init(PyThreadState *tstate); extern PyStatus _PyGILState_Init(PyThreadState *tstate);
extern void _PyGILState_Fini(PyThreadState *tstate); extern void _PyGILState_Fini(PyInterpreterState *interp);
PyAPI_FUNC(void) _PyGC_DumpShutdownStats(PyThreadState *tstate); PyAPI_FUNC(void) _PyGC_DumpShutdownStats(PyInterpreterState *interp);
PyAPI_FUNC(PyStatus) _Py_PreInitializeFromPyArgv( PyAPI_FUNC(PyStatus) _Py_PreInitializeFromPyArgv(
const PyPreConfig *src_config, const PyPreConfig *src_config,
@ -111,7 +111,7 @@ PyAPI_FUNC(void) _PyErr_Display(PyObject *file, PyObject *exception,
PyAPI_FUNC(void) _PyThreadState_DeleteCurrent(PyThreadState *tstate); PyAPI_FUNC(void) _PyThreadState_DeleteCurrent(PyThreadState *tstate);
extern void _PyAtExit_Call(PyThreadState *tstate); extern void _PyAtExit_Call(PyInterpreterState *interp);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -17,7 +17,7 @@ struct _warnings_runtime_state {
long filters_version; long filters_version;
}; };
extern int _PyWarnings_InitState(PyThreadState *tstate); extern int _PyWarnings_InitState(PyInterpreterState *interp);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -52,9 +52,9 @@ atexit_cleanup(struct atexit_state *state)
PyStatus PyStatus
_PyAtExit_Init(PyThreadState *tstate) _PyAtExit_Init(PyInterpreterState *interp)
{ {
struct atexit_state *state = &tstate->interp->atexit; struct atexit_state *state = &interp->atexit;
// _PyAtExit_Init() must only be called once // _PyAtExit_Init() must only be called once
assert(state->callbacks == NULL); assert(state->callbacks == NULL);
@ -109,9 +109,9 @@ atexit_callfuncs(struct atexit_state *state)
void void
_PyAtExit_Call(PyThreadState *tstate) _PyAtExit_Call(PyInterpreterState *interp)
{ {
struct atexit_state *state = &tstate->interp->atexit; struct atexit_state *state = &interp->atexit;
atexit_callfuncs(state); atexit_callfuncs(state);
} }

View File

@ -161,9 +161,9 @@ _PyGC_InitState(GCState *gcstate)
PyStatus PyStatus
_PyGC_Init(PyThreadState *tstate) _PyGC_Init(PyInterpreterState *interp)
{ {
GCState *gcstate = &tstate->interp->gc; GCState *gcstate = &interp->gc;
gcstate->garbage = PyList_New(0); gcstate->garbage = PyList_New(0);
if (gcstate->garbage == NULL) { if (gcstate->garbage == NULL) {
@ -1036,15 +1036,15 @@ delete_garbage(PyThreadState *tstate, GCState *gcstate,
* Clearing the free lists may give back memory to the OS earlier. * Clearing the free lists may give back memory to the OS earlier.
*/ */
static void static void
clear_freelists(PyThreadState *tstate) clear_freelists(PyInterpreterState *interp)
{ {
_PyFrame_ClearFreeList(tstate); _PyFrame_ClearFreeList(interp);
_PyTuple_ClearFreeList(tstate); _PyTuple_ClearFreeList(interp);
_PyFloat_ClearFreeList(tstate); _PyFloat_ClearFreeList(interp);
_PyList_ClearFreeList(tstate); _PyList_ClearFreeList(interp);
_PyDict_ClearFreeList(tstate); _PyDict_ClearFreeList(interp);
_PyAsyncGen_ClearFreeLists(tstate); _PyAsyncGen_ClearFreeLists(interp);
_PyContext_ClearFreeList(tstate); _PyContext_ClearFreeList(interp);
} }
// Show stats for objects in each generations // Show stats for objects in each generations
@ -1323,7 +1323,7 @@ gc_collect_main(PyThreadState *tstate, int generation,
/* Clear free list only during the collection of the highest /* Clear free list only during the collection of the highest
* generation */ * generation */
if (generation == NUM_GENERATIONS-1) { if (generation == NUM_GENERATIONS-1) {
clear_freelists(tstate); clear_freelists(tstate->interp);
} }
if (_PyErr_Occurred(tstate)) { if (_PyErr_Occurred(tstate)) {
@ -2092,9 +2092,9 @@ _PyGC_CollectNoFail(PyThreadState *tstate)
} }
void void
_PyGC_DumpShutdownStats(PyThreadState *tstate) _PyGC_DumpShutdownStats(PyInterpreterState *interp)
{ {
GCState *gcstate = &tstate->interp->gc; GCState *gcstate = &interp->gc;
if (!(gcstate->debug & DEBUG_SAVEALL) if (!(gcstate->debug & DEBUG_SAVEALL)
&& gcstate->garbage != NULL && PyList_GET_SIZE(gcstate->garbage) > 0) { && gcstate->garbage != NULL && PyList_GET_SIZE(gcstate->garbage) > 0) {
const char *message; const char *message;
@ -2129,9 +2129,9 @@ _PyGC_DumpShutdownStats(PyThreadState *tstate)
} }
void void
_PyGC_Fini(PyThreadState *tstate) _PyGC_Fini(PyInterpreterState *interp)
{ {
GCState *gcstate = &tstate->interp->gc; GCState *gcstate = &interp->gc;
Py_CLEAR(gcstate->garbage); Py_CLEAR(gcstate->garbage);
Py_CLEAR(gcstate->callbacks); Py_CLEAR(gcstate->callbacks);
} }

View File

@ -3063,9 +3063,9 @@ error:
PyStatus PyStatus
_PyBytes_Init(PyThreadState *tstate) _PyBytes_Init(PyInterpreterState *interp)
{ {
struct _Py_bytes_state *state = &tstate->interp->bytes; struct _Py_bytes_state *state = &interp->bytes;
if (bytes_create_empty_string_singleton(state) < 0) { if (bytes_create_empty_string_singleton(state) < 0) {
return _PyStatus_NO_MEMORY(); return _PyStatus_NO_MEMORY();
} }
@ -3074,9 +3074,9 @@ _PyBytes_Init(PyThreadState *tstate)
void void
_PyBytes_Fini(PyThreadState *tstate) _PyBytes_Fini(PyInterpreterState *interp)
{ {
struct _Py_bytes_state* state = &tstate->interp->bytes; struct _Py_bytes_state* state = &interp->bytes;
for (int i = 0; i < UCHAR_MAX + 1; i++) { for (int i = 0; i < UCHAR_MAX + 1; i++) {
Py_CLEAR(state->characters[i]); Py_CLEAR(state->characters[i]);
} }

View File

@ -260,9 +260,9 @@ get_dict_state(void)
void void
_PyDict_ClearFreeList(PyThreadState *tstate) _PyDict_ClearFreeList(PyInterpreterState *interp)
{ {
struct _Py_dict_state *state = &tstate->interp->dict_state; struct _Py_dict_state *state = &interp->dict_state;
while (state->numfree) { while (state->numfree) {
PyDictObject *op = state->free_list[--state->numfree]; PyDictObject *op = state->free_list[--state->numfree];
assert(PyDict_CheckExact(op)); assert(PyDict_CheckExact(op));
@ -275,11 +275,11 @@ _PyDict_ClearFreeList(PyThreadState *tstate)
void void
_PyDict_Fini(PyThreadState *tstate) _PyDict_Fini(PyInterpreterState *interp)
{ {
_PyDict_ClearFreeList(tstate); _PyDict_ClearFreeList(interp);
#ifdef Py_DEBUG #ifdef Py_DEBUG
struct _Py_dict_state *state = get_dict_state(); struct _Py_dict_state *state = &interp->dict_state;
state->numfree = -1; state->numfree = -1;
state->keys_numfree = -1; state->keys_numfree = -1;
#endif #endif

View File

@ -2529,9 +2529,9 @@ SimpleExtendsException(PyExc_Warning, ResourceWarning,
#endif /* MS_WINDOWS */ #endif /* MS_WINDOWS */
PyStatus PyStatus
_PyExc_Init(PyThreadState *tstate) _PyExc_Init(PyInterpreterState *interp)
{ {
struct _Py_exc_state *state = &tstate->interp->exc_state; struct _Py_exc_state *state = &interp->exc_state;
#define PRE_INIT(TYPE) \ #define PRE_INIT(TYPE) \
if (!(_PyExc_ ## TYPE.tp_flags & Py_TPFLAGS_READY)) { \ if (!(_PyExc_ ## TYPE.tp_flags & Py_TPFLAGS_READY)) { \
@ -2766,9 +2766,9 @@ _PyBuiltins_AddExceptions(PyObject *bltinmod)
} }
void void
_PyExc_Fini(PyThreadState *tstate) _PyExc_Fini(PyInterpreterState *interp)
{ {
struct _Py_exc_state *state = &tstate->interp->exc_state; struct _Py_exc_state *state = &interp->exc_state;
free_preallocated_memerrors(state); free_preallocated_memerrors(state);
Py_CLEAR(state->errnomap); Py_CLEAR(state->errnomap);
} }

View File

@ -2026,9 +2026,9 @@ _PyFloat_Init(void)
} }
void void
_PyFloat_ClearFreeList(PyThreadState *tstate) _PyFloat_ClearFreeList(PyInterpreterState *interp)
{ {
struct _Py_float_state *state = &tstate->interp->float_state; struct _Py_float_state *state = &interp->float_state;
PyFloatObject *f = state->free_list; PyFloatObject *f = state->free_list;
while (f != NULL) { while (f != NULL) {
PyFloatObject *next = (PyFloatObject*) Py_TYPE(f); PyFloatObject *next = (PyFloatObject*) Py_TYPE(f);
@ -2040,11 +2040,11 @@ _PyFloat_ClearFreeList(PyThreadState *tstate)
} }
void void
_PyFloat_Fini(PyThreadState *tstate) _PyFloat_Fini(PyInterpreterState *interp)
{ {
_PyFloat_ClearFreeList(tstate); _PyFloat_ClearFreeList(interp);
#ifdef Py_DEBUG #ifdef Py_DEBUG
struct _Py_float_state *state = &tstate->interp->float_state; struct _Py_float_state *state = &interp->float_state;
state->numfree = -1; state->numfree = -1;
#endif #endif
} }

View File

@ -1109,9 +1109,9 @@ PyFrame_LocalsToFast(PyFrameObject *f, int clear)
/* Clear out the free list */ /* Clear out the free list */
void void
_PyFrame_ClearFreeList(PyThreadState *tstate) _PyFrame_ClearFreeList(PyInterpreterState *interp)
{ {
struct _Py_frame_state *state = &tstate->interp->frame; struct _Py_frame_state *state = &interp->frame;
while (state->free_list != NULL) { while (state->free_list != NULL) {
PyFrameObject *f = state->free_list; PyFrameObject *f = state->free_list;
state->free_list = state->free_list->f_back; state->free_list = state->free_list->f_back;
@ -1122,11 +1122,11 @@ _PyFrame_ClearFreeList(PyThreadState *tstate)
} }
void void
_PyFrame_Fini(PyThreadState *tstate) _PyFrame_Fini(PyInterpreterState *interp)
{ {
_PyFrame_ClearFreeList(tstate); _PyFrame_ClearFreeList(interp);
#ifdef Py_DEBUG #ifdef Py_DEBUG
struct _Py_frame_state *state = &tstate->interp->frame; struct _Py_frame_state *state = &interp->frame;
state->numfree = -1; state->numfree = -1;
#endif #endif
} }

View File

@ -1489,9 +1489,9 @@ PyAsyncGen_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
void void
_PyAsyncGen_ClearFreeLists(PyThreadState *tstate) _PyAsyncGen_ClearFreeLists(PyInterpreterState *interp)
{ {
struct _Py_async_gen_state *state = &tstate->interp->async_gen; struct _Py_async_gen_state *state = &interp->async_gen;
while (state->value_numfree) { while (state->value_numfree) {
_PyAsyncGenWrappedValue *o; _PyAsyncGenWrappedValue *o;
@ -1509,11 +1509,11 @@ _PyAsyncGen_ClearFreeLists(PyThreadState *tstate)
} }
void void
_PyAsyncGen_Fini(PyThreadState *tstate) _PyAsyncGen_Fini(PyInterpreterState *interp)
{ {
_PyAsyncGen_ClearFreeLists(tstate); _PyAsyncGen_ClearFreeLists(interp);
#ifdef Py_DEBUG #ifdef Py_DEBUG
struct _Py_async_gen_state *state = &tstate->interp->async_gen; struct _Py_async_gen_state *state = &interp->async_gen;
state->value_numfree = -1; state->value_numfree = -1;
state->asend_numfree = -1; state->asend_numfree = -1;
#endif #endif

View File

@ -106,9 +106,9 @@ list_preallocate_exact(PyListObject *self, Py_ssize_t size)
} }
void void
_PyList_ClearFreeList(PyThreadState *tstate) _PyList_ClearFreeList(PyInterpreterState *interp)
{ {
struct _Py_list_state *state = &tstate->interp->list; struct _Py_list_state *state = &interp->list;
while (state->numfree) { while (state->numfree) {
PyListObject *op = state->free_list[--state->numfree]; PyListObject *op = state->free_list[--state->numfree];
assert(PyList_CheckExact(op)); assert(PyList_CheckExact(op));
@ -117,11 +117,11 @@ _PyList_ClearFreeList(PyThreadState *tstate)
} }
void void
_PyList_Fini(PyThreadState *tstate) _PyList_Fini(PyInterpreterState *interp)
{ {
_PyList_ClearFreeList(tstate); _PyList_ClearFreeList(interp);
#ifdef Py_DEBUG #ifdef Py_DEBUG
struct _Py_list_state *state = &tstate->interp->list; struct _Py_list_state *state = &interp->list;
state->numfree = -1; state->numfree = -1;
#endif #endif
} }

View File

@ -5702,7 +5702,7 @@ PyLong_GetInfo(void)
} }
int int
_PyLong_Init(PyThreadState *tstate) _PyLong_Init(PyInterpreterState *interp)
{ {
for (Py_ssize_t i=0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) { for (Py_ssize_t i=0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) {
sdigit ival = (sdigit)i - NSMALLNEGINTS; sdigit ival = (sdigit)i - NSMALLNEGINTS;
@ -5716,10 +5716,10 @@ _PyLong_Init(PyThreadState *tstate)
Py_SET_SIZE(v, size); Py_SET_SIZE(v, size);
v->ob_digit[0] = (digit)abs(ival); v->ob_digit[0] = (digit)abs(ival);
tstate->interp->small_ints[i] = v; interp->small_ints[i] = v;
} }
if (_Py_IsMainInterpreter(tstate->interp)) { if (_Py_IsMainInterpreter(interp)) {
/* initialize int_info */ /* initialize int_info */
if (Int_InfoType.tp_name == NULL) { if (Int_InfoType.tp_name == NULL) {
if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0) { if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0) {
@ -5732,9 +5732,9 @@ _PyLong_Init(PyThreadState *tstate)
} }
void void
_PyLong_Fini(PyThreadState *tstate) _PyLong_Fini(PyInterpreterState *interp)
{ {
for (Py_ssize_t i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) { for (Py_ssize_t i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) {
Py_CLEAR(tstate->interp->small_ints[i]); Py_CLEAR(interp->small_ints[i]);
} }
} }

View File

@ -97,9 +97,8 @@ PyObject _Py_EllipsisObject = {
/* Slice object implementation */ /* Slice object implementation */
void _PySlice_Fini(PyThreadState *tstate) void _PySlice_Fini(PyInterpreterState *interp)
{ {
PyInterpreterState *interp = tstate->interp;
PySliceObject *obj = interp->slice_cache; PySliceObject *obj = interp->slice_cache;
if (obj != NULL) { if (obj != NULL) {
interp->slice_cache = NULL; interp->slice_cache = NULL;

View File

@ -1007,10 +1007,10 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
} }
void void
_PyTuple_ClearFreeList(PyThreadState *tstate) _PyTuple_ClearFreeList(PyInterpreterState *interp)
{ {
#if PyTuple_MAXSAVESIZE > 0 #if PyTuple_MAXSAVESIZE > 0
struct _Py_tuple_state *state = &tstate->interp->tuple; struct _Py_tuple_state *state = &interp->tuple;
for (Py_ssize_t i = 1; i < PyTuple_MAXSAVESIZE; i++) { for (Py_ssize_t i = 1; i < PyTuple_MAXSAVESIZE; i++) {
PyTupleObject *p = state->free_list[i]; PyTupleObject *p = state->free_list[i];
state->free_list[i] = NULL; state->free_list[i] = NULL;
@ -1027,9 +1027,9 @@ _PyTuple_ClearFreeList(PyThreadState *tstate)
PyStatus PyStatus
_PyTuple_Init(PyThreadState *tstate) _PyTuple_Init(PyInterpreterState *interp)
{ {
struct _Py_tuple_state *state = &tstate->interp->tuple; struct _Py_tuple_state *state = &interp->tuple;
if (tuple_create_empty_tuple_singleton(state) < 0) { if (tuple_create_empty_tuple_singleton(state) < 0) {
return _PyStatus_NO_MEMORY(); return _PyStatus_NO_MEMORY();
} }
@ -1038,14 +1038,14 @@ _PyTuple_Init(PyThreadState *tstate)
void void
_PyTuple_Fini(PyThreadState *tstate) _PyTuple_Fini(PyInterpreterState *interp)
{ {
#if PyTuple_MAXSAVESIZE > 0 #if PyTuple_MAXSAVESIZE > 0
struct _Py_tuple_state *state = &tstate->interp->tuple; struct _Py_tuple_state *state = &interp->tuple;
// The empty tuple singleton must not be tracked by the GC // The empty tuple singleton must not be tracked by the GC
assert(!_PyObject_GC_IS_TRACKED(state->free_list[0])); assert(!_PyObject_GC_IS_TRACKED(state->free_list[0]));
Py_CLEAR(state->free_list[0]); Py_CLEAR(state->free_list[0]);
_PyTuple_ClearFreeList(tstate); _PyTuple_ClearFreeList(interp);
#ifdef Py_DEBUG #ifdef Py_DEBUG
state->numfree[0] = -1; state->numfree[0] = -1;
#endif #endif

View File

@ -283,10 +283,10 @@ PyType_ClearCache(void)
void void
_PyType_Fini(PyThreadState *tstate) _PyType_Fini(PyInterpreterState *interp)
{ {
_PyType_ClearCache(&tstate->interp->type_cache); _PyType_ClearCache(&interp->type_cache);
if (_Py_IsMainInterpreter(tstate->interp)) { if (_Py_IsMainInterpreter(interp)) {
clear_slotdefs(); clear_slotdefs();
} }
} }

View File

@ -15682,7 +15682,7 @@ PyTypeObject PyUnicode_Type = {
/* Initialize the Unicode implementation */ /* Initialize the Unicode implementation */
PyStatus PyStatus
_PyUnicode_Init(PyThreadState *tstate) _PyUnicode_Init(PyInterpreterState *interp)
{ {
/* XXX - move this array to unicodectype.c ? */ /* XXX - move this array to unicodectype.c ? */
const Py_UCS2 linebreak[] = { const Py_UCS2 linebreak[] = {
@ -15696,12 +15696,12 @@ _PyUnicode_Init(PyThreadState *tstate)
0x2029, /* PARAGRAPH SEPARATOR */ 0x2029, /* PARAGRAPH SEPARATOR */
}; };
struct _Py_unicode_state *state = &tstate->interp->unicode; struct _Py_unicode_state *state = &interp->unicode;
if (unicode_create_empty_string_singleton(state) < 0) { if (unicode_create_empty_string_singleton(state) < 0) {
return _PyStatus_NO_MEMORY(); return _PyStatus_NO_MEMORY();
} }
if (_Py_IsMainInterpreter(tstate->interp)) { if (_Py_IsMainInterpreter(interp)) {
/* initialize the linebreak bloom filter */ /* initialize the linebreak bloom filter */
bloom_linebreak = make_bloom_mask( bloom_linebreak = make_bloom_mask(
PyUnicode_2BYTE_KIND, linebreak, PyUnicode_2BYTE_KIND, linebreak,
@ -15813,9 +15813,9 @@ PyUnicode_InternFromString(const char *cp)
void void
_PyUnicode_ClearInterned(PyThreadState *tstate) _PyUnicode_ClearInterned(PyInterpreterState *interp)
{ {
struct _Py_unicode_state *state = &tstate->interp->unicode; struct _Py_unicode_state *state = &interp->unicode;
if (state->interned == NULL) { if (state->interned == NULL) {
return; return;
} }
@ -16093,10 +16093,10 @@ error:
static PyStatus static PyStatus
init_stdio_encoding(PyThreadState *tstate) init_stdio_encoding(PyInterpreterState *interp)
{ {
/* Update the stdio encoding to the normalized Python codec name. */ /* Update the stdio encoding to the normalized Python codec name. */
PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(tstate->interp); PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(interp);
if (config_get_codec_name(&config->stdio_encoding) < 0) { if (config_get_codec_name(&config->stdio_encoding) < 0) {
return _PyStatus_ERR("failed to get the Python codec name " return _PyStatus_ERR("failed to get the Python codec name "
"of the stdio encoding"); "of the stdio encoding");
@ -16189,7 +16189,7 @@ _PyUnicode_InitEncodings(PyThreadState *tstate)
return status; return status;
} }
return init_stdio_encoding(tstate); return init_stdio_encoding(tstate->interp);
} }
@ -16233,9 +16233,9 @@ _PyUnicode_EnableLegacyWindowsFSEncoding(void)
void void
_PyUnicode_Fini(PyThreadState *tstate) _PyUnicode_Fini(PyInterpreterState *interp)
{ {
struct _Py_unicode_state *state = &tstate->interp->unicode; struct _Py_unicode_state *state = &interp->unicode;
// _PyUnicode_ClearInterned() must be called before // _PyUnicode_ClearInterned() must be called before
assert(state->interned == NULL); assert(state->interned == NULL);

View File

@ -114,9 +114,9 @@ init_filters(void)
/* Initialize the given warnings module state. */ /* Initialize the given warnings module state. */
int int
_PyWarnings_InitState(PyThreadState *tstate) _PyWarnings_InitState(PyInterpreterState *interp)
{ {
WarningsState *st = &tstate->interp->warnings; WarningsState *st = &interp->warnings;
if (st->filters == NULL) { if (st->filters == NULL) {
st->filters = init_filters(); st->filters = init_filters();

View File

@ -2853,11 +2853,11 @@ static struct PyModuleDef builtinsmodule = {
PyObject * PyObject *
_PyBuiltin_Init(PyThreadState *tstate) _PyBuiltin_Init(PyInterpreterState *interp)
{ {
PyObject *mod, *dict, *debug; PyObject *mod, *dict, *debug;
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp); const PyConfig *config = _PyInterpreterState_GetConfig(interp);
if (PyType_Ready(&PyFilter_Type) < 0 || if (PyType_Ready(&PyFilter_Type) < 0 ||
PyType_Ready(&PyMap_Type) < 0 || PyType_Ready(&PyMap_Type) < 0 ||

View File

@ -323,10 +323,10 @@ _PyEval_InitGIL(PyThreadState *tstate)
} }
void void
_PyEval_FiniGIL(PyThreadState *tstate) _PyEval_FiniGIL(PyInterpreterState *interp)
{ {
#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS #ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
if (!_Py_IsMainInterpreter(tstate->interp)) { if (!_Py_IsMainInterpreter(interp)) {
/* Currently, the GIL is shared by all interpreters, /* Currently, the GIL is shared by all interpreters,
and only the main interpreter is responsible to create and only the main interpreter is responsible to create
and destroy it. */ and destroy it. */
@ -335,9 +335,9 @@ _PyEval_FiniGIL(PyThreadState *tstate)
#endif #endif
#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
struct _gil_runtime_state *gil = &tstate->interp->ceval.gil; struct _gil_runtime_state *gil = &interp->ceval.gil;
#else #else
struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil; struct _gil_runtime_state *gil = &interp->runtime->ceval.gil;
#endif #endif
if (!gil_created(gil)) { if (!gil_created(gil)) {
/* First Py_InitializeFromConfig() call: the GIL doesn't exist /* First Py_InitializeFromConfig() call: the GIL doesn't exist
@ -502,10 +502,9 @@ _PyEval_ReInitThreads(PyThreadState *tstate)
raised. */ raised. */
void void
_PyEval_SignalAsyncExc(PyThreadState *tstate) _PyEval_SignalAsyncExc(PyInterpreterState *interp)
{ {
assert(is_tstate_valid(tstate)); SIGNAL_ASYNC_EXC(interp);
SIGNAL_ASYNC_EXC(tstate->interp);
} }
PyThreadState * PyThreadState *
@ -690,10 +689,8 @@ handle_signals(PyThreadState *tstate)
} }
static int static int
make_pending_calls(PyThreadState *tstate) make_pending_calls(PyInterpreterState *interp)
{ {
assert(is_tstate_valid(tstate));
/* only execute pending calls on main thread */ /* only execute pending calls on main thread */
if (!_Py_ThreadCanHandlePendingCalls()) { if (!_Py_ThreadCanHandlePendingCalls()) {
return 0; return 0;
@ -708,11 +705,11 @@ make_pending_calls(PyThreadState *tstate)
/* unsignal before starting to call callbacks, so that any callback /* unsignal before starting to call callbacks, so that any callback
added in-between re-signals */ added in-between re-signals */
UNSIGNAL_PENDING_CALLS(tstate->interp); UNSIGNAL_PENDING_CALLS(interp);
int res = 0; int res = 0;
/* perform a bounded number of calls, in case of recursion */ /* perform a bounded number of calls, in case of recursion */
struct _pending_calls *pending = &tstate->interp->ceval.pending; struct _pending_calls *pending = &interp->ceval.pending;
for (int i=0; i<NPENDINGCALLS; i++) { for (int i=0; i<NPENDINGCALLS; i++) {
int (*func)(void *) = NULL; int (*func)(void *) = NULL;
void *arg = NULL; void *arg = NULL;
@ -737,7 +734,7 @@ make_pending_calls(PyThreadState *tstate)
error: error:
busy = 0; busy = 0;
SIGNAL_PENDING_CALLS(tstate->interp); SIGNAL_PENDING_CALLS(interp);
return res; return res;
} }
@ -745,6 +742,7 @@ void
_Py_FinishPendingCalls(PyThreadState *tstate) _Py_FinishPendingCalls(PyThreadState *tstate)
{ {
assert(PyGILState_Check()); assert(PyGILState_Check());
assert(is_tstate_valid(tstate));
struct _pending_calls *pending = &tstate->interp->ceval.pending; struct _pending_calls *pending = &tstate->interp->ceval.pending;
@ -752,7 +750,7 @@ _Py_FinishPendingCalls(PyThreadState *tstate)
return; return;
} }
if (make_pending_calls(tstate) < 0) { if (make_pending_calls(tstate->interp) < 0) {
PyObject *exc, *val, *tb; PyObject *exc, *val, *tb;
_PyErr_Fetch(tstate, &exc, &val, &tb); _PyErr_Fetch(tstate, &exc, &val, &tb);
PyErr_BadInternalCall(); PyErr_BadInternalCall();
@ -769,6 +767,7 @@ Py_MakePendingCalls(void)
assert(PyGILState_Check()); assert(PyGILState_Check());
PyThreadState *tstate = _PyThreadState_GET(); PyThreadState *tstate = _PyThreadState_GET();
assert(is_tstate_valid(tstate));
/* Python signal handler doesn't really queue a callback: it only signals /* Python signal handler doesn't really queue a callback: it only signals
that a signal was received, see _PyEval_SignalReceived(). */ that a signal was received, see _PyEval_SignalReceived(). */
@ -777,7 +776,7 @@ Py_MakePendingCalls(void)
return res; return res;
} }
res = make_pending_calls(tstate); res = make_pending_calls(tstate->interp);
if (res != 0) { if (res != 0) {
return res; return res;
} }
@ -950,7 +949,7 @@ eval_frame_handle_pending(PyThreadState *tstate)
/* Pending calls */ /* Pending calls */
struct _ceval_state *ceval2 = &tstate->interp->ceval; struct _ceval_state *ceval2 = &tstate->interp->ceval;
if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) { if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
if (make_pending_calls(tstate) != 0) { if (make_pending_calls(tstate->interp) != 0) {
return -1; return -1;
} }
} }

View File

@ -318,7 +318,7 @@ _ready:
/* Don't access tstate if the thread must exit */ /* Don't access tstate if the thread must exit */
if (tstate->async_exc != NULL) { if (tstate->async_exc != NULL) {
_PyEval_SignalAsyncExc(tstate); _PyEval_SignalAsyncExc(tstate->interp);
} }
MUTEX_UNLOCK(gil->mutex); MUTEX_UNLOCK(gil->mutex);

View File

@ -1287,9 +1287,9 @@ get_token_missing(void)
void void
_PyContext_ClearFreeList(PyThreadState *tstate) _PyContext_ClearFreeList(PyInterpreterState *interp)
{ {
struct _Py_context_state *state = &tstate->interp->context; struct _Py_context_state *state = &interp->context;
for (; state->numfree; state->numfree--) { for (; state->numfree; state->numfree--) {
PyContext *ctx = state->freelist; PyContext *ctx = state->freelist;
state->freelist = (PyContext *)ctx->ctx_weakreflist; state->freelist = (PyContext *)ctx->ctx_weakreflist;
@ -1300,14 +1300,14 @@ _PyContext_ClearFreeList(PyThreadState *tstate)
void void
_PyContext_Fini(PyThreadState *tstate) _PyContext_Fini(PyInterpreterState *interp)
{ {
if (_Py_IsMainInterpreter(tstate->interp)) { if (_Py_IsMainInterpreter(interp)) {
Py_CLEAR(_token_missing); Py_CLEAR(_token_missing);
} }
_PyContext_ClearFreeList(tstate); _PyContext_ClearFreeList(interp);
#ifdef Py_DEBUG #ifdef Py_DEBUG
struct _Py_context_state *state = &tstate->interp->context; struct _Py_context_state *state = &interp->context;
state->numfree = -1; state->numfree = -1;
#endif #endif
_PyHamt_Fini(); _PyHamt_Fini();

View File

@ -301,16 +301,16 @@ _PyImport_GetModuleId(struct _Py_Identifier *nameid)
int int
_PyImport_SetModule(PyObject *name, PyObject *m) _PyImport_SetModule(PyObject *name, PyObject *m)
{ {
PyThreadState *tstate = _PyThreadState_GET(); PyInterpreterState *interp = _PyInterpreterState_GET();
PyObject *modules = tstate->interp->modules; PyObject *modules = interp->modules;
return PyObject_SetItem(modules, name, m); return PyObject_SetItem(modules, name, m);
} }
int int
_PyImport_SetModuleString(const char *name, PyObject *m) _PyImport_SetModuleString(const char *name, PyObject *m)
{ {
PyThreadState *tstate = _PyThreadState_GET(); PyInterpreterState *interp = _PyInterpreterState_GET();
PyObject *modules = tstate->interp->modules; PyObject *modules = interp->modules;
return PyMapping_SetItemString(modules, name, m); return PyMapping_SetItemString(modules, name, m);
} }
@ -342,9 +342,8 @@ import_get_module(PyThreadState *tstate, PyObject *name)
static int static int
import_ensure_initialized(PyThreadState *tstate, PyObject *mod, PyObject *name) import_ensure_initialized(PyInterpreterState *interp, PyObject *mod, PyObject *name)
{ {
PyInterpreterState *interp = tstate->interp;
PyObject *spec; PyObject *spec;
_Py_IDENTIFIER(_lock_unlock_module); _Py_IDENTIFIER(_lock_unlock_module);
@ -1530,7 +1529,7 @@ PyImport_GetModule(PyObject *name)
mod = import_get_module(tstate, name); mod = import_get_module(tstate, name);
if (mod != NULL && mod != Py_None) { if (mod != NULL && mod != Py_None) {
if (import_ensure_initialized(tstate, mod, name) < 0) { if (import_ensure_initialized(tstate->interp, mod, name) < 0) {
Py_DECREF(mod); Py_DECREF(mod);
remove_importlib_frames(tstate); remove_importlib_frames(tstate);
return NULL; return NULL;
@ -1594,7 +1593,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
} }
if (mod != NULL && mod != Py_None) { if (mod != NULL && mod != Py_None) {
if (import_ensure_initialized(tstate, mod, name) < 0) { if (import_ensure_initialized(tstate->interp, mod, name) < 0) {
goto error; goto error;
} }
} }

View File

@ -2909,8 +2909,8 @@ _Py_GetConfigsAsDict(void)
Py_CLEAR(dict); Py_CLEAR(dict);
/* pre config */ /* pre config */
PyThreadState *tstate = _PyThreadState_GET(); PyInterpreterState *interp = _PyInterpreterState_GET();
const PyPreConfig *pre_config = &tstate->interp->runtime->preconfig; const PyPreConfig *pre_config = &interp->runtime->preconfig;
dict = _PyPreConfig_AsDict(pre_config); dict = _PyPreConfig_AsDict(pre_config);
if (dict == NULL) { if (dict == NULL) {
goto error; goto error;
@ -2921,7 +2921,7 @@ _Py_GetConfigsAsDict(void)
Py_CLEAR(dict); Py_CLEAR(dict);
/* core config */ /* core config */
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp); const PyConfig *config = _PyInterpreterState_GetConfig(interp);
dict = _PyConfig_AsDict(config); dict = _PyConfig_AsDict(config);
if (dict == NULL) { if (dict == NULL) {
goto error; goto error;

View File

@ -574,7 +574,7 @@ init_interp_create_gil(PyThreadState *tstate)
/* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is
only called here. */ only called here. */
_PyEval_FiniGIL(tstate); _PyEval_FiniGIL(tstate->interp);
/* Auto-thread-state API */ /* Auto-thread-state API */
status = _PyGILState_Init(tstate); status = _PyGILState_Init(tstate);
@ -624,12 +624,12 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
static PyStatus static PyStatus
pycore_init_types(PyThreadState *tstate) pycore_init_types(PyInterpreterState *interp)
{ {
PyStatus status; PyStatus status;
int is_main_interp = _Py_IsMainInterpreter(tstate->interp); int is_main_interp = _Py_IsMainInterpreter(interp);
status = _PyGC_Init(tstate); status = _PyGC_Init(interp);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
@ -637,7 +637,7 @@ pycore_init_types(PyThreadState *tstate)
// Create the empty tuple singleton. It must be created before the first // Create the empty tuple singleton. It must be created before the first
// PyType_Ready() call since PyType_Ready() creates tuples, for tp_bases // PyType_Ready() call since PyType_Ready() creates tuples, for tp_bases
// for example. // for example.
status = _PyTuple_Init(tstate); status = _PyTuple_Init(interp);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
@ -649,21 +649,21 @@ pycore_init_types(PyThreadState *tstate)
} }
} }
if (!_PyLong_Init(tstate)) { if (!_PyLong_Init(interp)) {
return _PyStatus_ERR("can't init longs"); return _PyStatus_ERR("can't init longs");
} }
status = _PyUnicode_Init(tstate); status = _PyUnicode_Init(interp);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
status = _PyBytes_Init(tstate); status = _PyBytes_Init(interp);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
status = _PyExc_Init(tstate); status = _PyExc_Init(interp);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
@ -689,11 +689,11 @@ pycore_init_types(PyThreadState *tstate)
} }
} }
if (_PyWarnings_InitState(tstate) < 0) { if (_PyWarnings_InitState(interp) < 0) {
return _PyStatus_ERR("can't initialize warnings"); return _PyStatus_ERR("can't initialize warnings");
} }
status = _PyAtExit_Init(tstate); status = _PyAtExit_Init(interp);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
@ -703,16 +703,13 @@ pycore_init_types(PyThreadState *tstate)
static PyStatus static PyStatus
pycore_init_builtins(PyThreadState *tstate) pycore_init_builtins(PyInterpreterState *interp)
{ {
assert(!_PyErr_Occurred(tstate)); PyObject *bimod = _PyBuiltin_Init(interp);
PyObject *bimod = _PyBuiltin_Init(tstate);
if (bimod == NULL) { if (bimod == NULL) {
goto error; goto error;
} }
PyInterpreterState *interp = tstate->interp;
if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) { if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) {
goto error; goto error;
} }
@ -743,8 +740,6 @@ pycore_init_builtins(PyThreadState *tstate)
} }
interp->import_func = Py_NewRef(import_func); interp->import_func = Py_NewRef(import_func);
assert(!_PyErr_Occurred(tstate));
return _PyStatus_OK(); return _PyStatus_OK();
error: error:
@ -759,7 +754,7 @@ pycore_interp_init(PyThreadState *tstate)
PyStatus status; PyStatus status;
PyObject *sysmod = NULL; PyObject *sysmod = NULL;
status = pycore_init_types(tstate); status = pycore_init_types(tstate->interp);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
goto done; goto done;
} }
@ -769,11 +764,15 @@ pycore_interp_init(PyThreadState *tstate)
goto done; goto done;
} }
status = pycore_init_builtins(tstate); assert(!_PyErr_Occurred(tstate));
status = pycore_init_builtins(tstate->interp);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
goto done; goto done;
} }
assert(!_PyErr_Occurred(tstate));
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp); const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
if (config->_install_importlib) { if (config->_install_importlib) {
/* This call sets up builtin and frozen import support */ /* This call sets up builtin and frozen import support */
@ -1464,7 +1463,7 @@ finalize_modules(PyThreadState *tstate)
// Dump GC stats before it's too late, since it uses the warnings // Dump GC stats before it's too late, since it uses the warnings
// machinery. // machinery.
_PyGC_DumpShutdownStats(tstate); _PyGC_DumpShutdownStats(interp);
if (weaklist != NULL) { if (weaklist != NULL) {
// Now, if there are any modules left alive, clear their globals to // Now, if there are any modules left alive, clear their globals to
@ -1570,27 +1569,27 @@ flush_std_files(void)
static void static void
finalize_interp_types(PyThreadState *tstate) finalize_interp_types(PyInterpreterState *interp)
{ {
_PyExc_Fini(tstate); _PyExc_Fini(interp);
_PyFrame_Fini(tstate); _PyFrame_Fini(interp);
_PyAsyncGen_Fini(tstate); _PyAsyncGen_Fini(interp);
_PyContext_Fini(tstate); _PyContext_Fini(interp);
_PyType_Fini(tstate); _PyType_Fini(interp);
// Call _PyUnicode_ClearInterned() before _PyDict_Fini() since it uses // Call _PyUnicode_ClearInterned() before _PyDict_Fini() since it uses
// a dict internally. // a dict internally.
_PyUnicode_ClearInterned(tstate); _PyUnicode_ClearInterned(interp);
_PyDict_Fini(tstate); _PyDict_Fini(interp);
_PyList_Fini(tstate); _PyList_Fini(interp);
_PyTuple_Fini(tstate); _PyTuple_Fini(interp);
_PySlice_Fini(tstate); _PySlice_Fini(interp);
_PyBytes_Fini(tstate); _PyBytes_Fini(interp);
_PyUnicode_Fini(tstate); _PyUnicode_Fini(interp);
_PyFloat_Fini(tstate); _PyFloat_Fini(interp);
_PyLong_Fini(tstate); _PyLong_Fini(interp);
} }
@ -1615,16 +1614,16 @@ finalize_interp_clear(PyThreadState *tstate)
_Py_ClearFileSystemEncoding(); _Py_ClearFileSystemEncoding();
} }
finalize_interp_types(tstate); finalize_interp_types(tstate->interp);
} }
static void static void
finalize_interp_delete(PyThreadState *tstate) finalize_interp_delete(PyInterpreterState *interp)
{ {
if (_Py_IsMainInterpreter(tstate->interp)) { if (_Py_IsMainInterpreter(interp)) {
/* Cleanup auto-thread-state */ /* Cleanup auto-thread-state */
_PyGILState_Fini(tstate); _PyGILState_Fini(interp);
} }
/* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can /* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can
@ -1633,7 +1632,7 @@ finalize_interp_delete(PyThreadState *tstate)
created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be
called multiple times. */ called multiple times. */
PyInterpreterState_Delete(tstate->interp); PyInterpreterState_Delete(interp);
} }
@ -1666,7 +1665,7 @@ Py_FinalizeEx(void)
* the threads created via Threading. * the threads created via Threading.
*/ */
_PyAtExit_Call(tstate); _PyAtExit_Call(tstate->interp);
/* Copy the core config, PyInterpreterState_Delete() free /* Copy the core config, PyInterpreterState_Delete() free
the core config memory */ the core config memory */
@ -1779,7 +1778,7 @@ Py_FinalizeEx(void)
#endif /* Py_TRACE_REFS */ #endif /* Py_TRACE_REFS */
finalize_interp_clear(tstate); finalize_interp_clear(tstate);
finalize_interp_delete(tstate); finalize_interp_delete(tstate->interp);
#ifdef Py_TRACE_REFS #ifdef Py_TRACE_REFS
/* Display addresses (& refcnts) of all objects still alive. /* Display addresses (& refcnts) of all objects still alive.
@ -1954,7 +1953,7 @@ Py_EndInterpreter(PyThreadState *tstate)
// Wrap up existing "threading"-module-created, non-daemon threads. // Wrap up existing "threading"-module-created, non-daemon threads.
wait_for_thread_shutdown(tstate); wait_for_thread_shutdown(tstate);
_PyAtExit_Call(tstate); _PyAtExit_Call(tstate->interp);
if (tstate != interp->tstate_head || tstate->next != NULL) { if (tstate != interp->tstate_head || tstate->next != NULL) {
Py_FatalError("not the last thread"); Py_FatalError("not the last thread");
@ -1963,7 +1962,7 @@ Py_EndInterpreter(PyThreadState *tstate)
finalize_modules(tstate); finalize_modules(tstate);
finalize_interp_clear(tstate); finalize_interp_clear(tstate);
finalize_interp_delete(tstate); finalize_interp_delete(tstate->interp);
} }
/* Add the __main__ module */ /* Add the __main__ module */

View File

@ -324,7 +324,7 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
/* Last garbage collection on this interpreter */ /* Last garbage collection on this interpreter */
_PyGC_CollectNoFail(tstate); _PyGC_CollectNoFail(tstate);
_PyGC_Fini(tstate); _PyGC_Fini(interp);
/* We don't clear sysdict and builtins until the end of this function. /* We don't clear sysdict and builtins until the end of this function.
Because clearing other attributes can execute arbitrary Python code Because clearing other attributes can execute arbitrary Python code
@ -1146,7 +1146,7 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
HEAD_UNLOCK(runtime); HEAD_UNLOCK(runtime);
Py_XDECREF(old_exc); Py_XDECREF(old_exc);
_PyEval_SignalAsyncExc(tstate); _PyEval_SignalAsyncExc(tstate->interp);
return 1; return 1;
} }
HEAD_UNLOCK(runtime); HEAD_UNLOCK(runtime);
@ -1357,9 +1357,9 @@ _PyGILState_GetInterpreterStateUnsafe(void)
} }
void void
_PyGILState_Fini(PyThreadState *tstate) _PyGILState_Fini(PyInterpreterState *interp)
{ {
struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate; struct _gilstate_runtime_state *gilstate = &interp->runtime->gilstate;
PyThread_tss_delete(&gilstate->autoTSSkey); PyThread_tss_delete(&gilstate->autoTSSkey);
gilstate->autoInterpreterState = NULL; gilstate->autoInterpreterState = NULL;
} }

View File

@ -86,9 +86,9 @@ _PySys_GetObjectId(_Py_Identifier *key)
} }
static PyObject * static PyObject *
_PySys_GetObject(PyThreadState *tstate, const char *name) _PySys_GetObject(PyInterpreterState *interp, const char *name)
{ {
PyObject *sysdict = tstate->interp->sysdict; PyObject *sysdict = interp->sysdict;
if (sysdict == NULL) { if (sysdict == NULL) {
return NULL; return NULL;
} }
@ -102,7 +102,7 @@ PySys_GetObject(const char *name)
PyObject *exc_type, *exc_value, *exc_tb; PyObject *exc_type, *exc_value, *exc_tb;
_PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb); _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
PyObject *value = _PySys_GetObject(tstate, name); PyObject *value = _PySys_GetObject(tstate->interp, name);
/* XXX Suppress a new exception if it was raised and restore /* XXX Suppress a new exception if it was raised and restore
* the old one. */ * the old one. */
_PyErr_Restore(tstate, exc_type, exc_value, exc_tb); _PyErr_Restore(tstate, exc_type, exc_value, exc_tb);
@ -110,12 +110,12 @@ PySys_GetObject(const char *name)
} }
static int static int
sys_set_object(PyThreadState *tstate, PyObject *key, PyObject *v) sys_set_object(PyInterpreterState *interp, PyObject *key, PyObject *v)
{ {
if (key == NULL) { if (key == NULL) {
return -1; return -1;
} }
PyObject *sd = tstate->interp->sysdict; PyObject *sd = interp->sysdict;
if (v == NULL) { if (v == NULL) {
v = _PyDict_Pop(sd, key, Py_None); v = _PyDict_Pop(sd, key, Py_None);
if (v == NULL) { if (v == NULL) {
@ -130,24 +130,24 @@ sys_set_object(PyThreadState *tstate, PyObject *key, PyObject *v)
} }
static int static int
sys_set_object_id(PyThreadState *tstate, _Py_Identifier *key, PyObject *v) sys_set_object_id(PyInterpreterState *interp, _Py_Identifier *key, PyObject *v)
{ {
return sys_set_object(tstate, _PyUnicode_FromId(key), v); return sys_set_object(interp, _PyUnicode_FromId(key), v);
} }
int int
_PySys_SetObjectId(_Py_Identifier *key, PyObject *v) _PySys_SetObjectId(_Py_Identifier *key, PyObject *v)
{ {
PyThreadState *tstate = _PyThreadState_GET(); PyInterpreterState *interp = _PyInterpreterState_GET();
return sys_set_object_id(tstate, key, v); return sys_set_object_id(interp, key, v);
} }
static int static int
sys_set_object_str(PyThreadState *tstate, const char *name, PyObject *v) sys_set_object_str(PyInterpreterState *interp, const char *name, PyObject *v)
{ {
PyObject *key = v ? PyUnicode_InternFromString(name) PyObject *key = v ? PyUnicode_InternFromString(name)
: PyUnicode_FromString(name); : PyUnicode_FromString(name);
int r = sys_set_object(tstate, key, v); int r = sys_set_object(interp, key, v);
Py_XDECREF(key); Py_XDECREF(key);
return r; return r;
} }
@ -155,22 +155,21 @@ sys_set_object_str(PyThreadState *tstate, const char *name, PyObject *v)
int int
PySys_SetObject(const char *name, PyObject *v) PySys_SetObject(const char *name, PyObject *v)
{ {
PyThreadState *tstate = _PyThreadState_GET(); PyInterpreterState *interp = _PyInterpreterState_GET();
return sys_set_object_str(tstate, name, v); return sys_set_object_str(interp, name, v);
} }
static int static int
should_audit(PyInterpreterState *is) should_audit(PyInterpreterState *interp)
{ {
/* tstate->interp cannot be NULL, but test it just in case /* interp must not be NULL, but test it just in case for extra safety */
for extra safety */ assert(interp != NULL);
assert(is != NULL); if (!interp) {
if (!is) {
return 0; return 0;
} }
return (is->runtime->audit_hook_head return (interp->runtime->audit_hook_head
|| is->audit_hooks || interp->audit_hooks
|| PyDTrace_AUDIT_ENABLED()); || PyDTrace_AUDIT_ENABLED());
} }
@ -455,15 +454,15 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
return NULL; return NULL;
} }
PyInterpreterState *is = tstate->interp; PyInterpreterState *interp = tstate->interp;
if (is->audit_hooks == NULL) { if (interp->audit_hooks == NULL) {
is->audit_hooks = PyList_New(0); interp->audit_hooks = PyList_New(0);
if (is->audit_hooks == NULL) { if (interp->audit_hooks == NULL) {
return NULL; return NULL;
} }
} }
if (PyList_Append(is->audit_hooks, hook) < 0) { if (PyList_Append(interp->audit_hooks, hook) < 0) {
return NULL; return NULL;
} }
@ -1607,8 +1606,8 @@ static PyObject *
sys_setdlopenflags_impl(PyObject *module, int new_val) sys_setdlopenflags_impl(PyObject *module, int new_val)
/*[clinic end generated code: output=ec918b7fe0a37281 input=4c838211e857a77f]*/ /*[clinic end generated code: output=ec918b7fe0a37281 input=4c838211e857a77f]*/
{ {
PyThreadState *tstate = _PyThreadState_GET(); PyInterpreterState *interp = _PyInterpreterState_GET();
tstate->interp->dlopenflags = new_val; interp->dlopenflags = new_val;
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -1625,8 +1624,8 @@ static PyObject *
sys_getdlopenflags_impl(PyObject *module) sys_getdlopenflags_impl(PyObject *module)
/*[clinic end generated code: output=e92cd1bc5005da6e input=dc4ea0899c53b4b6]*/ /*[clinic end generated code: output=e92cd1bc5005da6e input=dc4ea0899c53b4b6]*/
{ {
PyThreadState *tstate = _PyThreadState_GET(); PyInterpreterState *interp = _PyInterpreterState_GET();
return PyLong_FromLong(tstate->interp->dlopenflags); return PyLong_FromLong(interp->dlopenflags);
} }
#endif /* HAVE_DLOPEN */ #endif /* HAVE_DLOPEN */
@ -2217,7 +2216,7 @@ get_warnoptions(PyThreadState *tstate)
if (warnoptions == NULL) { if (warnoptions == NULL) {
return NULL; return NULL;
} }
if (sys_set_object_id(tstate, &PyId_warnoptions, warnoptions)) { if (sys_set_object_id(tstate->interp, &PyId_warnoptions, warnoptions)) {
Py_DECREF(warnoptions); Py_DECREF(warnoptions);
return NULL; return NULL;
} }
@ -2310,7 +2309,7 @@ get_xoptions(PyThreadState *tstate)
if (xoptions == NULL) { if (xoptions == NULL) {
return NULL; return NULL;
} }
if (sys_set_object_id(tstate, &PyId__xoptions, xoptions)) { if (sys_set_object_id(tstate->interp, &PyId__xoptions, xoptions)) {
Py_DECREF(xoptions); Py_DECREF(xoptions);
return NULL; return NULL;
} }
@ -2511,9 +2510,8 @@ static PyStructSequence_Desc flags_desc = {
}; };
static int static int
set_flags_from_config(PyObject *flags, PyThreadState *tstate) set_flags_from_config(PyInterpreterState *interp, PyObject *flags)
{ {
PyInterpreterState *interp = tstate->interp;
const PyPreConfig *preconfig = &interp->runtime->preconfig; const PyPreConfig *preconfig = &interp->runtime->preconfig;
const PyConfig *config = _PyInterpreterState_GetConfig(interp); const PyConfig *config = _PyInterpreterState_GetConfig(interp);
@ -2554,14 +2552,14 @@ set_flags_from_config(PyObject *flags, PyThreadState *tstate)
static PyObject* static PyObject*
make_flags(PyThreadState *tstate) make_flags(PyInterpreterState *interp)
{ {
PyObject *flags = PyStructSequence_New(&FlagsType); PyObject *flags = PyStructSequence_New(&FlagsType);
if (flags == NULL) { if (flags == NULL) {
return NULL; return NULL;
} }
if (set_flags_from_config(flags, tstate) < 0) { if (set_flags_from_config(interp, flags) < 0) {
Py_DECREF(flags); Py_DECREF(flags);
return NULL; return NULL;
} }
@ -2819,7 +2817,7 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict)
goto type_init_failed; goto type_init_failed;
} }
} }
SET_SYS("flags", make_flags(tstate)); SET_SYS("flags", make_flags(tstate->interp));
/* prevent user from creating new instances */ /* prevent user from creating new instances */
FlagsType.tp_init = NULL; FlagsType.tp_init = NULL;
FlagsType.tp_new = NULL; FlagsType.tp_new = NULL;
@ -2941,8 +2939,9 @@ sys_create_xoptions_dict(const PyConfig *config)
int int
_PySys_UpdateConfig(PyThreadState *tstate) _PySys_UpdateConfig(PyThreadState *tstate)
{ {
PyObject *sysdict = tstate->interp->sysdict; PyInterpreterState *interp = tstate->interp;
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp); PyObject *sysdict = interp->sysdict;
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
int res; int res;
#define COPY_LIST(KEY, VALUE) \ #define COPY_LIST(KEY, VALUE) \
@ -2985,11 +2984,11 @@ _PySys_UpdateConfig(PyThreadState *tstate)
#undef COPY_WSTR #undef COPY_WSTR
// sys.flags // sys.flags
PyObject *flags = _PySys_GetObject(tstate, "flags"); // borrowed ref PyObject *flags = _PySys_GetObject(interp, "flags"); // borrowed ref
if (flags == NULL) { if (flags == NULL) {
return -1; return -1;
} }
if (set_flags_from_config(flags, tstate) < 0) { if (set_flags_from_config(interp, flags) < 0) {
return -1; return -1;
} }
@ -3129,8 +3128,8 @@ PySys_SetPath(const wchar_t *path)
PyObject *v; PyObject *v;
if ((v = makepathobject(path, DELIM)) == NULL) if ((v = makepathobject(path, DELIM)) == NULL)
Py_FatalError("can't create sys.path"); Py_FatalError("can't create sys.path");
PyThreadState *tstate = _PyThreadState_GET(); PyInterpreterState *interp = _PyInterpreterState_GET();
if (sys_set_object_id(tstate, &PyId_path, v) != 0) { if (sys_set_object_id(interp, &PyId_path, v) != 0) {
Py_FatalError("can't assign sys.path"); Py_FatalError("can't assign sys.path");
} }
Py_DECREF(v); Py_DECREF(v);
@ -3171,7 +3170,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
if (av == NULL) { if (av == NULL) {
Py_FatalError("no mem for sys.argv"); Py_FatalError("no mem for sys.argv");
} }
if (sys_set_object_str(tstate, "argv", av) != 0) { if (sys_set_object_str(tstate->interp, "argv", av) != 0) {
Py_DECREF(av); Py_DECREF(av);
Py_FatalError("can't assign sys.argv"); Py_FatalError("can't assign sys.argv");
} }