bpo-34301: Add _PyInterpreterState_Get() helper function (GH-8592)

sys_setcheckinterval() now uses a local variable to parse arguments,
before writing into interp->check_interval.
This commit is contained in:
Victor Stinner 2018-08-03 15:33:52 +02:00 committed by GitHub
parent 2ebd3813af
commit caba55b3b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 107 additions and 93 deletions

View File

@ -245,6 +245,13 @@ typedef struct _ts {
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void); PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *); PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *); PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
#if !defined(Py_LIMITED_API)
PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_Get(void);
#endif
#ifdef Py_BUILD_CORE
/* Macro which should only be used for performance critical code */
# define _PyInterpreterState_GET_UNSAFE() (PyThreadState_GET()->interp)
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000
/* New in 3.7 */ /* New in 3.7 */
PyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *); PyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *);

View File

@ -1049,7 +1049,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
boot = PyMem_NEW(struct bootstate, 1); boot = PyMem_NEW(struct bootstate, 1);
if (boot == NULL) if (boot == NULL)
return PyErr_NoMemory(); return PyErr_NoMemory();
boot->interp = PyThreadState_GET()->interp; boot->interp = _PyInterpreterState_Get();
boot->func = func; boot->func = func;
boot->args = args; boot->args = args;
boot->keyw = keyw; boot->keyw = keyw;
@ -1154,8 +1154,8 @@ A thread's identity may be reused for another thread after it exits.");
static PyObject * static PyObject *
thread__count(PyObject *self, PyObject *Py_UNUSED(ignored)) thread__count(PyObject *self, PyObject *Py_UNUSED(ignored))
{ {
PyThreadState *tstate = PyThreadState_Get(); PyInterpreterState *interp = _PyInterpreterState_Get();
return PyLong_FromLong(tstate->interp->num_threads); return PyLong_FromLong(interp->num_threads);
} }
PyDoc_STRVAR(_count_doc, PyDoc_STRVAR(_count_doc,
@ -1348,7 +1348,7 @@ PyInit__thread(void)
PyObject *m, *d, *v; PyObject *m, *d, *v;
double time_max; double time_max;
double timeout_max; double timeout_max;
PyThreadState *tstate = PyThreadState_Get(); PyInterpreterState *interp = _PyInterpreterState_Get();
/* Initialize types: */ /* Initialize types: */
if (PyType_Ready(&localdummytype) < 0) if (PyType_Ready(&localdummytype) < 0)
@ -1395,7 +1395,7 @@ PyInit__thread(void)
if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0) if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
return NULL; return NULL;
tstate->interp->num_threads = 0; interp->num_threads = 0;
str_dict = PyUnicode_InternFromString("__dict__"); str_dict = PyUnicode_InternFromString("__dict__");
if (str_dict == NULL) if (str_dict == NULL)

View File

@ -26,10 +26,9 @@ _copy_raw_string(PyObject *strobj)
static PyInterpreterState * static PyInterpreterState *
_get_current(void) _get_current(void)
{ {
PyThreadState *tstate = PyThreadState_Get(); // _PyInterpreterState_Get() aborts if lookup fails, so don't need
// PyThreadState_Get() aborts if lookup fails, so we don't need
// to check the result for NULL. // to check the result for NULL.
return tstate->interp; return _PyInterpreterState_Get();
} }
static int64_t static int64_t
@ -1941,7 +1940,7 @@ _run_script_in_interpreter(PyInterpreterState *interp, const char *codestr,
// Switch to interpreter. // Switch to interpreter.
PyThreadState *save_tstate = NULL; PyThreadState *save_tstate = NULL;
if (interp != PyThreadState_Get()->interp) { if (interp != _PyInterpreterState_Get()) {
// XXX Using the "head" thread isn't strictly correct. // XXX Using the "head" thread isn't strictly correct.
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp); PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
// XXX Possible GILState issues? // XXX Possible GILState issues?

View File

@ -437,7 +437,7 @@ run_at_forkers(PyObject *lst, int reverse)
void void
PyOS_BeforeFork(void) PyOS_BeforeFork(void)
{ {
run_at_forkers(PyThreadState_Get()->interp->before_forkers, 1); run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1);
_PyImport_AcquireLock(); _PyImport_AcquireLock();
} }
@ -448,7 +448,7 @@ PyOS_AfterFork_Parent(void)
if (_PyImport_ReleaseLock() <= 0) if (_PyImport_ReleaseLock() <= 0)
Py_FatalError("failed releasing import lock after fork"); Py_FatalError("failed releasing import lock after fork");
run_at_forkers(PyThreadState_Get()->interp->after_forkers_parent, 0); run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0);
} }
void void
@ -459,7 +459,7 @@ PyOS_AfterFork_Child(void)
_PyImport_ReInitLock(); _PyImport_ReInitLock();
_PySignal_AfterFork(); _PySignal_AfterFork();
run_at_forkers(PyThreadState_Get()->interp->after_forkers_child, 0); run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0);
} }
static int static int
@ -5655,7 +5655,7 @@ os_register_at_fork_impl(PyObject *module, PyObject *before,
check_null_or_callable(after_in_parent, "after_in_parent")) { check_null_or_callable(after_in_parent, "after_in_parent")) {
return NULL; return NULL;
} }
interp = PyThreadState_Get()->interp; interp = _PyInterpreterState_Get();
if (register_at_forker(&interp->before_forkers, before)) { if (register_at_forker(&interp->before_forkers, before)) {
return NULL; return NULL;

View File

@ -1074,7 +1074,7 @@ read_directory(PyObject *archive)
if (flags & 0x0800) { if (flags & 0x0800) {
charset = "utf-8"; charset = "utf-8";
} }
else if (!PyThreadState_GET()->interp->codecs_initialized) { else if (!_PyInterpreterState_Get()->codecs_initialized) {
/* During bootstrap, we may need to load the encodings /* During bootstrap, we may need to load the encodings
package from a ZIP file. But the cp437 encoding is implemented package from a ZIP file. But the cp437 encoding is implemented
in Python in the encodings package. in Python in the encodings package.
@ -1351,7 +1351,7 @@ unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime)
uint32_t flags = get_uint32(buf + 4); uint32_t flags = get_uint32(buf + 4);
if (flags != 0) { if (flags != 0) {
_PyCoreConfig *config = &PyThreadState_GET()->interp->core_config; _PyCoreConfig *config = &_PyInterpreterState_Get()->core_config;
// Hash-based pyc. We currently refuse to handle checked hash-based // Hash-based pyc. We currently refuse to handle checked hash-based
// pycs. We could validate hash-based pycs against the source, but it // pycs. We could validate hash-based pycs against the source, but it
// seems likely that most people putting hash-based pycs in a zipfile // seems likely that most people putting hash-based pycs in a zipfile

View File

@ -3,6 +3,7 @@
#include "Python.h" #include "Python.h"
#include "code.h" #include "code.h"
#include "structmember.h" #include "structmember.h"
#include "internal/pystate.h"
/* Holder for co_extra information */ /* Holder for co_extra information */
typedef struct { typedef struct {
@ -428,7 +429,7 @@ static void
code_dealloc(PyCodeObject *co) code_dealloc(PyCodeObject *co)
{ {
if (co->co_extra != NULL) { if (co->co_extra != NULL) {
PyInterpreterState *interp = PyThreadState_Get()->interp; PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
_PyCodeObjectExtra *co_extra = co->co_extra; _PyCodeObjectExtra *co_extra = co->co_extra;
for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) { for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
@ -871,7 +872,7 @@ _PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
int int
_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra) _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
{ {
PyInterpreterState *interp = PyThreadState_Get()->interp; PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
if (!PyCode_Check(code) || index < 0 || if (!PyCode_Check(code) || index < 0 ||
index >= interp->co_extra_user_count) { index >= interp->co_extra_user_count) {

View File

@ -85,7 +85,7 @@ static size_t count_reuse = 0;
static void static void
show_alloc(void) show_alloc(void)
{ {
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_Get();
if (!interp->core_config.show_alloc_count) { if (!interp->core_config.show_alloc_count) {
return; return;
} }

View File

@ -173,7 +173,7 @@ _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
PyObject * PyObject *
PyModule_Create2(struct PyModuleDef* module, int module_api_version) PyModule_Create2(struct PyModuleDef* module, int module_api_version)
{ {
if (!_PyImport_IsInitialized(PyThreadState_GET()->interp)) if (!_PyImport_IsInitialized(_PyInterpreterState_Get()))
Py_FatalError("Python import machinery not initialized"); Py_FatalError("Python import machinery not initialized");
return _PyModule_CreateInitialized(module, module_api_version); return _PyModule_CreateInitialized(module, module_api_version);
} }
@ -693,8 +693,7 @@ module_dealloc(PyModuleObject *m)
static PyObject * static PyObject *
module_repr(PyModuleObject *m) module_repr(PyModuleObject *m)
{ {
PyThreadState *tstate = PyThreadState_GET(); PyInterpreterState *interp = _PyInterpreterState_Get();
PyInterpreterState *interp = tstate->interp;
return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m); return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
} }

View File

@ -96,7 +96,7 @@ extern Py_ssize_t null_strings, one_strings;
void void
dump_counts(FILE* f) dump_counts(FILE* f)
{ {
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_Get();
if (!interp->core_config.show_alloc_count) { if (!interp->core_config.show_alloc_count) {
return; return;
} }

View File

@ -44,7 +44,7 @@ static Py_ssize_t count_tracked = 0;
static void static void
show_track(void) show_track(void)
{ {
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_Get();
if (!interp->core_config.show_alloc_count) { if (!interp->core_config.show_alloc_count) {
return; return;
} }

View File

@ -3413,7 +3413,7 @@ PyUnicode_EncodeFSDefault(PyObject *unicode)
#if defined(__APPLE__) #if defined(__APPLE__)
return _PyUnicode_AsUTF8String(unicode, Py_FileSystemDefaultEncodeErrors); return _PyUnicode_AsUTF8String(unicode, Py_FileSystemDefaultEncodeErrors);
#else #else
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
/* Bootstrap check: if the filesystem codec is implemented in Python, we /* Bootstrap check: if the filesystem codec is implemented in Python, we
cannot use it to encode and decode filenames before it is loaded. Load cannot use it to encode and decode filenames before it is loaded. Load
the Python codec requires to encode at least its own filename. Use the C the Python codec requires to encode at least its own filename. Use the C
@ -3639,7 +3639,7 @@ PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
#if defined(__APPLE__) #if defined(__APPLE__)
return PyUnicode_DecodeUTF8Stateful(s, size, Py_FileSystemDefaultEncodeErrors, NULL); return PyUnicode_DecodeUTF8Stateful(s, size, Py_FileSystemDefaultEncodeErrors, NULL);
#else #else
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
/* Bootstrap check: if the filesystem codec is implemented in Python, we /* Bootstrap check: if the filesystem codec is implemented in Python, we
cannot use it to encode and decode filenames before it is loaded. Load cannot use it to encode and decode filenames before it is loaded. Load
the Python codec requires to encode at least its own filename. Use the C the Python codec requires to encode at least its own filename. Use the C

View File

@ -306,7 +306,7 @@ dump_config(void)
exit(1); \ exit(1); \
} }
PyInterpreterState *interp = PyThreadState_Get()->interp; PyInterpreterState *interp = _PyInterpreterState_Get();
_PyCoreConfig *config = &interp->core_config; _PyCoreConfig *config = &interp->core_config;
printf("install_signal_handlers = %i\n", config->install_signal_handlers); printf("install_signal_handlers = %i\n", config->install_signal_handlers);

View File

@ -78,7 +78,7 @@ get_warnings_attr(_Py_Identifier *attr_id, int try_import)
gone, then we can't even use PyImport_GetModule without triggering gone, then we can't even use PyImport_GetModule without triggering
an interpreter abort. an interpreter abort.
*/ */
if (!PyThreadState_GET()->interp->modules) { if (!_PyInterpreterState_GET_UNSAFE()->modules) {
return NULL; return NULL;
} }
warnings_module = PyImport_GetModule(warnings_str); warnings_module = PyImport_GetModule(warnings_str);
@ -686,7 +686,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
} }
if (f == NULL) { if (f == NULL) {
globals = PyThreadState_Get()->interp->sysdict; globals = _PyInterpreterState_GET_UNSAFE()->sysdict;
*filename = PyUnicode_FromString("sys"); *filename = PyUnicode_FromString("sys");
*lineno = 1; *lineno = 1;
} }

View File

@ -532,8 +532,8 @@ PyEval_EvalFrame(PyFrameObject *f) {
PyObject * PyObject *
PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
{ {
PyThreadState *tstate = PyThreadState_GET(); PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
return tstate->interp->eval_frame(f, throwflag); return interp->eval_frame(f, throwflag);
} }
PyObject* _Py_HOT_FUNCTION PyObject* _Py_HOT_FUNCTION
@ -4435,7 +4435,7 @@ PyEval_GetBuiltins(void)
{ {
PyFrameObject *current_frame = PyEval_GetFrame(); PyFrameObject *current_frame = PyEval_GetFrame();
if (current_frame == NULL) if (current_frame == NULL)
return PyThreadState_GET()->interp->builtins; return _PyInterpreterState_GET_UNSAFE()->builtins;
else else
return current_frame->f_builtins; return current_frame->f_builtins;
} }
@ -4769,7 +4769,7 @@ import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *leve
} }
/* Fast path for not overloaded __import__. */ /* Fast path for not overloaded __import__. */
if (import_func == PyThreadState_GET()->interp->import_func) { if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
int ilevel = _PyLong_AsInt(level); int ilevel = _PyLong_AsInt(level);
if (ilevel == -1 && PyErr_Occurred()) { if (ilevel == -1 && PyErr_Occurred()) {
return NULL; return NULL;
@ -5136,7 +5136,7 @@ _Py_GetDXProfile(PyObject *self, PyObject *args)
Py_ssize_t Py_ssize_t
_PyEval_RequestCodeExtraIndex(freefunc free) _PyEval_RequestCodeExtraIndex(freefunc free)
{ {
PyInterpreterState *interp = PyThreadState_Get()->interp; PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Py_ssize_t new_index; Py_ssize_t new_index;
if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {

View File

@ -32,7 +32,7 @@ static int _PyCodecRegistry_Init(void); /* Forward */
int PyCodec_Register(PyObject *search_function) int PyCodec_Register(PyObject *search_function)
{ {
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_Get();
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
goto onError; goto onError;
if (search_function == NULL) { if (search_function == NULL) {
@ -99,7 +99,6 @@ PyObject *normalizestring(const char *string)
PyObject *_PyCodec_Lookup(const char *encoding) PyObject *_PyCodec_Lookup(const char *encoding)
{ {
PyInterpreterState *interp;
PyObject *result, *args = NULL, *v; PyObject *result, *args = NULL, *v;
Py_ssize_t i, len; Py_ssize_t i, len;
@ -108,7 +107,7 @@ PyObject *_PyCodec_Lookup(const char *encoding)
goto onError; goto onError;
} }
interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
goto onError; goto onError;
@ -187,11 +186,10 @@ PyObject *_PyCodec_Lookup(const char *encoding)
int _PyCodec_Forget(const char *encoding) int _PyCodec_Forget(const char *encoding)
{ {
PyInterpreterState *interp;
PyObject *v; PyObject *v;
int result; int result;
interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_Get();
if (interp->codec_search_path == NULL) { if (interp->codec_search_path == NULL) {
return -1; return -1;
} }
@ -624,7 +622,7 @@ PyObject *_PyCodec_DecodeText(PyObject *object,
Return 0 on success, -1 on error */ Return 0 on success, -1 on error */
int PyCodec_RegisterError(const char *name, PyObject *error) int PyCodec_RegisterError(const char *name, PyObject *error)
{ {
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_Get();
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
return -1; return -1;
if (!PyCallable_Check(error)) { if (!PyCallable_Check(error)) {
@ -642,7 +640,7 @@ PyObject *PyCodec_LookupError(const char *name)
{ {
PyObject *handler = NULL; PyObject *handler = NULL;
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
return NULL; return NULL;
@ -1494,7 +1492,7 @@ static int _PyCodecRegistry_Init(void)
} }
}; };
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_Get();
PyObject *mod; PyObject *mod;
unsigned i; unsigned i;

View File

@ -91,7 +91,7 @@ _PyImport_FindSharedFuncptr(const char *prefix,
} }
} }
dlopenflags = PyThreadState_GET()->interp->dlopenflags; dlopenflags = _PyInterpreterState_Get()->dlopenflags;
handle = dlopen(pathname, dlopenflags); handle = dlopen(pathname, dlopenflags);

View File

@ -304,7 +304,7 @@ _PyImport_Fini2(void)
PyObject * PyObject *
PyImport_GetModuleDict(void) PyImport_GetModuleDict(void)
{ {
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
if (interp->modules == NULL) { if (interp->modules == NULL) {
Py_FatalError("PyImport_GetModuleDict: no module dictionary!"); Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
} }
@ -397,7 +397,7 @@ PyImport_Cleanup(void)
{ {
Py_ssize_t pos; Py_ssize_t pos;
PyObject *key, *value, *dict; PyObject *key, *value, *dict;
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_Get();
PyObject *modules = PyImport_GetModuleDict(); PyObject *modules = PyImport_GetModuleDict();
PyObject *weaklist = NULL; PyObject *weaklist = NULL;
const char * const *p; const char * const *p;
@ -592,7 +592,7 @@ long
PyImport_GetMagicNumber(void) PyImport_GetMagicNumber(void)
{ {
long res; long res;
PyInterpreterState *interp = PyThreadState_Get()->interp; PyInterpreterState *interp = _PyInterpreterState_Get();
PyObject *external, *pyc_magic; PyObject *external, *pyc_magic;
external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external"); external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external");
@ -892,7 +892,7 @@ PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co,
goto error; goto error;
} }
else if (cpathobj != NULL) { else if (cpathobj != NULL) {
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
_Py_IDENTIFIER(_get_sourcefile); _Py_IDENTIFIER(_get_sourcefile);
if (interp == NULL) { if (interp == NULL) {
@ -972,7 +972,7 @@ PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
PyObject *cpathname) PyObject *cpathname)
{ {
PyObject *d, *external, *res; PyObject *d, *external, *res;
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
_Py_IDENTIFIER(_fix_up_module); _Py_IDENTIFIER(_fix_up_module);
d = module_dict_for_exec(name); d = module_dict_for_exec(name);
@ -1619,7 +1619,7 @@ import_find_and_load(PyObject *abs_name)
{ {
_Py_IDENTIFIER(_find_and_load); _Py_IDENTIFIER(_find_and_load);
PyObject *mod = NULL; PyObject *mod = NULL;
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
int import_time = interp->core_config.import_time; int import_time = interp->core_config.import_time;
static int import_level; static int import_level;
static _PyTime_t accumulated; static _PyTime_t accumulated;
@ -1680,7 +1680,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
PyObject *final_mod = NULL; PyObject *final_mod = NULL;
PyObject *mod = NULL; PyObject *mod = NULL;
PyObject *package = NULL; PyObject *package = NULL;
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
int has_from; int has_from;
if (name == NULL) { if (name == NULL) {
@ -2289,7 +2289,7 @@ PyInit__imp(void)
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
if (d == NULL) if (d == NULL)
goto failure; goto failure;
_PyCoreConfig *config = &PyThreadState_GET()->interp->core_config; _PyCoreConfig *config = &_PyInterpreterState_Get()->core_config;
PyObject *pyc_mode = PyUnicode_FromString(config->_check_hash_pycs_mode); PyObject *pyc_mode = PyUnicode_FromString(config->_check_hash_pycs_mode);
if (pyc_mode == NULL) { if (pyc_mode == NULL) {
goto failure; goto failure;

View File

@ -2123,11 +2123,7 @@ _Py_FatalInitError(_PyInitError err)
/* For the atexit module. */ /* For the atexit module. */
void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module) void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module)
{ {
PyThreadState *ts; PyInterpreterState *is = _PyInterpreterState_Get();
PyInterpreterState *is;
ts = PyThreadState_GET();
is = ts->interp;
/* Guard against API misuse (see bpo-17852) */ /* Guard against API misuse (see bpo-17852) */
assert(is->pyexitfunc == NULL || is->pyexitfunc == func); assert(is->pyexitfunc == NULL || is->pyexitfunc == func);

View File

@ -264,6 +264,21 @@ PyInterpreterState_Delete(PyInterpreterState *interp)
} }
PyInterpreterState *
_PyInterpreterState_Get(void)
{
PyThreadState *tstate = GET_TSTATE();
if (tstate == NULL) {
Py_FatalError("_PyInterpreterState_Get(): no current thread state");
}
PyInterpreterState *interp = tstate->interp;
if (interp == NULL) {
Py_FatalError("_PyInterpreterState_Get(): no current interpreter");
}
return interp;
}
int64_t int64_t
PyInterpreterState_GetID(PyInterpreterState *interp) PyInterpreterState_GetID(PyInterpreterState *interp)
{ {
@ -1184,10 +1199,9 @@ _check_xidata(_PyCrossInterpreterData *data)
int int
_PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data) _PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
{ {
PyThreadState *tstate = PyThreadState_Get(); // _PyInterpreterState_Get() aborts if lookup fails, so we don't need
// PyThreadState_Get() aborts if lookup fails, so we don't need
// to check the result for NULL. // to check the result for NULL.
PyInterpreterState *interp = tstate->interp; PyInterpreterState *interp = _PyInterpreterState_Get();
// Reset data before re-populating. // Reset data before re-populating.
*data = (_PyCrossInterpreterData){0}; *data = (_PyCrossInterpreterData){0};
@ -1235,7 +1249,7 @@ _call_in_interpreter(PyInterpreterState *interp,
* naive approach. * naive approach.
*/ */
PyThreadState *save_tstate = NULL; PyThreadState *save_tstate = NULL;
if (interp != PyThreadState_Get()->interp) { if (interp != _PyInterpreterState_Get()) {
// XXX Using the "head" thread isn't strictly correct. // XXX Using the "head" thread isn't strictly correct.
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp); PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
// XXX Possible GILState issues? // XXX Possible GILState issues?

View File

@ -92,7 +92,7 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *
PyCompilerFlags local_flags; PyCompilerFlags local_flags;
int nomem_count = 0; int nomem_count = 0;
#ifdef Py_REF_DEBUG #ifdef Py_REF_DEBUG
int show_ref_count = PyThreadState_GET()->interp->core_config.show_ref_count; int show_ref_count = _PyInterpreterState_Get()->core_config.show_ref_count;
#endif #endif
filename = PyUnicode_DecodeFSDefault(filename_str); filename = PyUnicode_DecodeFSDefault(filename_str);
@ -336,17 +336,13 @@ maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
static int static int
set_main_loader(PyObject *d, const char *filename, const char *loader_name) set_main_loader(PyObject *d, const char *filename, const char *loader_name)
{ {
PyInterpreterState *interp;
PyThreadState *tstate;
PyObject *filename_obj, *bootstrap, *loader_type = NULL, *loader; PyObject *filename_obj, *bootstrap, *loader_type = NULL, *loader;
int result = 0; int result = 0;
filename_obj = PyUnicode_DecodeFSDefault(filename); filename_obj = PyUnicode_DecodeFSDefault(filename);
if (filename_obj == NULL) if (filename_obj == NULL)
return -1; return -1;
/* Get current thread state and interpreter pointer */ PyInterpreterState *interp = _PyInterpreterState_Get();
tstate = PyThreadState_GET();
interp = tstate->interp;
bootstrap = PyObject_GetAttrString(interp->importlib, bootstrap = PyObject_GetAttrString(interp->importlib,
"_bootstrap_external"); "_bootstrap_external");
if (bootstrap != NULL) { if (bootstrap != NULL) {

View File

@ -56,52 +56,56 @@ _Py_IDENTIFIER(write);
PyObject * PyObject *
_PySys_GetObjectId(_Py_Identifier *key) _PySys_GetObjectId(_Py_Identifier *key)
{ {
PyThreadState *tstate = PyThreadState_GET(); PyObject *sd = _PyInterpreterState_GET_UNSAFE()->sysdict;
PyObject *sd = tstate->interp->sysdict; if (sd == NULL) {
if (sd == NULL)
return NULL; return NULL;
}
return _PyDict_GetItemId(sd, key); return _PyDict_GetItemId(sd, key);
} }
PyObject * PyObject *
PySys_GetObject(const char *name) PySys_GetObject(const char *name)
{ {
PyThreadState *tstate = PyThreadState_GET(); PyObject *sd = _PyInterpreterState_GET_UNSAFE()->sysdict;
PyObject *sd = tstate->interp->sysdict; if (sd == NULL) {
if (sd == NULL)
return NULL; return NULL;
}
return PyDict_GetItemString(sd, name); return PyDict_GetItemString(sd, name);
} }
int int
_PySys_SetObjectId(_Py_Identifier *key, PyObject *v) _PySys_SetObjectId(_Py_Identifier *key, PyObject *v)
{ {
PyThreadState *tstate = PyThreadState_GET(); PyObject *sd = _PyInterpreterState_GET_UNSAFE()->sysdict;
PyObject *sd = tstate->interp->sysdict;
if (v == NULL) { if (v == NULL) {
if (_PyDict_GetItemId(sd, key) == NULL) if (_PyDict_GetItemId(sd, key) == NULL) {
return 0; return 0;
else }
else {
return _PyDict_DelItemId(sd, key); return _PyDict_DelItemId(sd, key);
} }
else }
else {
return _PyDict_SetItemId(sd, key, v); return _PyDict_SetItemId(sd, key, v);
} }
}
int int
PySys_SetObject(const char *name, PyObject *v) PySys_SetObject(const char *name, PyObject *v)
{ {
PyThreadState *tstate = PyThreadState_GET(); PyObject *sd = _PyInterpreterState_GET_UNSAFE()->sysdict;
PyObject *sd = tstate->interp->sysdict;
if (v == NULL) { if (v == NULL) {
if (PyDict_GetItemString(sd, name) == NULL) if (PyDict_GetItemString(sd, name) == NULL) {
return 0; return 0;
else }
else {
return PyDict_DelItemString(sd, name); return PyDict_DelItemString(sd, name);
} }
else }
else {
return PyDict_SetItemString(sd, name, v); return PyDict_SetItemString(sd, name, v);
} }
}
static PyObject * static PyObject *
sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords) sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
@ -626,9 +630,13 @@ sys_setcheckinterval(PyObject *self, PyObject *args)
"are deprecated. Use sys.setswitchinterval() " "are deprecated. Use sys.setswitchinterval() "
"instead.", 1) < 0) "instead.", 1) < 0)
return NULL; return NULL;
PyInterpreterState *interp = PyThreadState_GET()->interp;
if (!PyArg_ParseTuple(args, "i:setcheckinterval", &interp->check_interval)) int check_interval;
if (!PyArg_ParseTuple(args, "i:setcheckinterval", &check_interval))
return NULL; return NULL;
PyInterpreterState *interp = _PyInterpreterState_Get();
interp->check_interval = check_interval;
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -647,7 +655,7 @@ sys_getcheckinterval(PyObject *self, PyObject *args)
"are deprecated. Use sys.getswitchinterval() " "are deprecated. Use sys.getswitchinterval() "
"instead.", 1) < 0) "instead.", 1) < 0)
return NULL; return NULL;
PyInterpreterState *interp = PyThreadState_GET()->interp; PyInterpreterState *interp = _PyInterpreterState_Get();
return PyLong_FromLong(interp->check_interval); return PyLong_FromLong(interp->check_interval);
} }
@ -1154,12 +1162,10 @@ static PyObject *
sys_setdlopenflags(PyObject *self, PyObject *args) sys_setdlopenflags(PyObject *self, PyObject *args)
{ {
int new_val; int new_val;
PyThreadState *tstate = PyThreadState_GET();
if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val)) if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
return NULL; return NULL;
if (!tstate) PyInterpreterState *interp = _PyInterpreterState_Get();
return NULL; interp->dlopenflags = new_val;
tstate->interp->dlopenflags = new_val;
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -1176,10 +1182,8 @@ can be found in the os module (RTLD_xxx constants, e.g. os.RTLD_LAZY).");
static PyObject * static PyObject *
sys_getdlopenflags(PyObject *self, PyObject *args) sys_getdlopenflags(PyObject *self, PyObject *args)
{ {
PyThreadState *tstate = PyThreadState_GET(); PyInterpreterState *interp = _PyInterpreterState_Get();
if (!tstate) return PyLong_FromLong(interp->dlopenflags);
return NULL;
return PyLong_FromLong(tstate->interp->dlopenflags);
} }
PyDoc_STRVAR(getdlopenflags_doc, PyDoc_STRVAR(getdlopenflags_doc,

View File

@ -92,7 +92,7 @@ PyThread_init_thread(void)
size_t size_t
PyThread_get_stacksize(void) PyThread_get_stacksize(void)
{ {
return PyThreadState_GET()->interp->pythread_stacksize; return _PyInterpreterState_Get()->pythread_stacksize;
} }
/* Only platforms defining a THREAD_SET_STACKSIZE() macro /* Only platforms defining a THREAD_SET_STACKSIZE() macro