PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
PyAPI_FUNC(void) PyInterpreterState_Clear(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
/* New in 3.7 */
PyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *);
boot = PyMem_NEW(struct bootstate, 1);
if (boot == NULL)
return PyErr_NoMemory();
- boot->interp = PyThreadState_GET()->interp;
+ boot->interp = _PyInterpreterState_Get();
boot->func = func;
boot->args = args;
boot->keyw = keyw;
static PyObject *
thread__count(PyObject *self, PyObject *Py_UNUSED(ignored))
{
- PyThreadState *tstate = PyThreadState_Get();
- return PyLong_FromLong(tstate->interp->num_threads);
+ PyInterpreterState *interp = _PyInterpreterState_Get();
+ return PyLong_FromLong(interp->num_threads);
}
PyDoc_STRVAR(_count_doc,
PyObject *m, *d, *v;
double time_max;
double timeout_max;
- PyThreadState *tstate = PyThreadState_Get();
+ PyInterpreterState *interp = _PyInterpreterState_Get();
/* Initialize types: */
if (PyType_Ready(&localdummytype) < 0)
if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
return NULL;
- tstate->interp->num_threads = 0;
+ interp->num_threads = 0;
str_dict = PyUnicode_InternFromString("__dict__");
if (str_dict == NULL)
static PyInterpreterState *
_get_current(void)
{
- PyThreadState *tstate = PyThreadState_Get();
- // PyThreadState_Get() aborts if lookup fails, so we don't need
+ // _PyInterpreterState_Get() aborts if lookup fails, so don't need
// to check the result for NULL.
- return tstate->interp;
+ return _PyInterpreterState_Get();
}
static int64_t
// Switch to interpreter.
PyThreadState *save_tstate = NULL;
- if (interp != PyThreadState_Get()->interp) {
+ if (interp != _PyInterpreterState_Get()) {
// XXX Using the "head" thread isn't strictly correct.
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
// XXX Possible GILState issues?
void
PyOS_BeforeFork(void)
{
- run_at_forkers(PyThreadState_Get()->interp->before_forkers, 1);
+ run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1);
_PyImport_AcquireLock();
}
if (_PyImport_ReleaseLock() <= 0)
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
_PyImport_ReInitLock();
_PySignal_AfterFork();
- run_at_forkers(PyThreadState_Get()->interp->after_forkers_child, 0);
+ run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0);
}
static int
check_null_or_callable(after_in_parent, "after_in_parent")) {
return NULL;
}
- interp = PyThreadState_Get()->interp;
+ interp = _PyInterpreterState_Get();
if (register_at_forker(&interp->before_forkers, before)) {
return NULL;
if (flags & 0x0800) {
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
package from a ZIP file. But the cp437 encoding is implemented
in Python in the encodings package.
uint32_t flags = get_uint32(buf + 4);
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
// pycs. We could validate hash-based pycs against the source, but it
// seems likely that most people putting hash-based pycs in a zipfile
#include "Python.h"
#include "code.h"
#include "structmember.h"
+#include "internal/pystate.h"
/* Holder for co_extra information */
typedef struct {
code_dealloc(PyCodeObject *co)
{
if (co->co_extra != NULL) {
- PyInterpreterState *interp = PyThreadState_Get()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
_PyCodeObjectExtra *co_extra = co->co_extra;
for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
int
_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 ||
index >= interp->co_extra_user_count) {
static void
show_alloc(void)
{
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_Get();
if (!interp->core_config.show_alloc_count) {
return;
}
PyObject *
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");
return _PyModule_CreateInitialized(module, module_api_version);
}
static PyObject *
module_repr(PyModuleObject *m)
{
- PyThreadState *tstate = PyThreadState_GET();
- PyInterpreterState *interp = tstate->interp;
+ PyInterpreterState *interp = _PyInterpreterState_Get();
return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
}
void
dump_counts(FILE* f)
{
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_Get();
if (!interp->core_config.show_alloc_count) {
return;
}
static void
show_track(void)
{
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_Get();
if (!interp->core_config.show_alloc_count) {
return;
}
#if defined(__APPLE__)
return _PyUnicode_AsUTF8String(unicode, Py_FileSystemDefaultEncodeErrors);
#else
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
/* Bootstrap check: if the filesystem codec is implemented in Python, we
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
#if defined(__APPLE__)
return PyUnicode_DecodeUTF8Stateful(s, size, Py_FileSystemDefaultEncodeErrors, NULL);
#else
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
/* Bootstrap check: if the filesystem codec is implemented in Python, we
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
exit(1); \
}
- PyInterpreterState *interp = PyThreadState_Get()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_Get();
_PyCoreConfig *config = &interp->core_config;
printf("install_signal_handlers = %i\n", config->install_signal_handlers);
gone, then we can't even use PyImport_GetModule without triggering
an interpreter abort.
*/
- if (!PyThreadState_GET()->interp->modules) {
+ if (!_PyInterpreterState_GET_UNSAFE()->modules) {
return NULL;
}
warnings_module = PyImport_GetModule(warnings_str);
}
if (f == NULL) {
- globals = PyThreadState_Get()->interp->sysdict;
+ globals = _PyInterpreterState_GET_UNSAFE()->sysdict;
*filename = PyUnicode_FromString("sys");
*lineno = 1;
}
PyObject *
PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
{
- PyThreadState *tstate = PyThreadState_GET();
- return tstate->interp->eval_frame(f, throwflag);
+ PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+ return interp->eval_frame(f, throwflag);
}
PyObject* _Py_HOT_FUNCTION
{
PyFrameObject *current_frame = PyEval_GetFrame();
if (current_frame == NULL)
- return PyThreadState_GET()->interp->builtins;
+ return _PyInterpreterState_GET_UNSAFE()->builtins;
else
return current_frame->f_builtins;
}
}
/* 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);
if (ilevel == -1 && PyErr_Occurred()) {
return NULL;
Py_ssize_t
_PyEval_RequestCodeExtraIndex(freefunc free)
{
- PyInterpreterState *interp = PyThreadState_Get()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Py_ssize_t new_index;
if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
int PyCodec_Register(PyObject *search_function)
{
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_Get();
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
goto onError;
if (search_function == NULL) {
PyObject *_PyCodec_Lookup(const char *encoding)
{
- PyInterpreterState *interp;
PyObject *result, *args = NULL, *v;
Py_ssize_t i, len;
goto onError;
}
- interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
goto onError;
int _PyCodec_Forget(const char *encoding)
{
- PyInterpreterState *interp;
PyObject *v;
int result;
- interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_Get();
if (interp->codec_search_path == NULL) {
return -1;
}
Return 0 on success, -1 on 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())
return -1;
if (!PyCallable_Check(error)) {
{
PyObject *handler = NULL;
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
return NULL;
}
};
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_Get();
PyObject *mod;
unsigned i;
}
}
- dlopenflags = PyThreadState_GET()->interp->dlopenflags;
+ dlopenflags = _PyInterpreterState_Get()->dlopenflags;
handle = dlopen(pathname, dlopenflags);
PyObject *
PyImport_GetModuleDict(void)
{
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
if (interp->modules == NULL) {
Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
}
{
Py_ssize_t pos;
PyObject *key, *value, *dict;
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_Get();
PyObject *modules = PyImport_GetModuleDict();
PyObject *weaklist = NULL;
const char * const *p;
PyImport_GetMagicNumber(void)
{
long res;
- PyInterpreterState *interp = PyThreadState_Get()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_Get();
PyObject *external, *pyc_magic;
external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external");
goto error;
}
else if (cpathobj != NULL) {
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
_Py_IDENTIFIER(_get_sourcefile);
if (interp == NULL) {
PyObject *cpathname)
{
PyObject *d, *external, *res;
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
_Py_IDENTIFIER(_fix_up_module);
d = module_dict_for_exec(name);
{
_Py_IDENTIFIER(_find_and_load);
PyObject *mod = NULL;
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
int import_time = interp->core_config.import_time;
static int import_level;
static _PyTime_t accumulated;
PyObject *final_mod = NULL;
PyObject *mod = NULL;
PyObject *package = NULL;
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
int has_from;
if (name == NULL) {
d = PyModule_GetDict(m);
if (d == NULL)
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);
if (pyc_mode == NULL) {
goto failure;
/* For the atexit module. */
void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module)
{
- PyThreadState *ts;
- PyInterpreterState *is;
-
- ts = PyThreadState_GET();
- is = ts->interp;
+ PyInterpreterState *is = _PyInterpreterState_Get();
/* Guard against API misuse (see bpo-17852) */
assert(is->pyexitfunc == NULL || is->pyexitfunc == func);
}
+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
PyInterpreterState_GetID(PyInterpreterState *interp)
{
int
_PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
{
- PyThreadState *tstate = PyThreadState_Get();
- // PyThreadState_Get() aborts if lookup fails, so we don't need
+ // _PyInterpreterState_Get() aborts if lookup fails, so we don't need
// to check the result for NULL.
- PyInterpreterState *interp = tstate->interp;
+ PyInterpreterState *interp = _PyInterpreterState_Get();
// Reset data before re-populating.
*data = (_PyCrossInterpreterData){0};
* naive approach.
*/
PyThreadState *save_tstate = NULL;
- if (interp != PyThreadState_Get()->interp) {
+ if (interp != _PyInterpreterState_Get()) {
// XXX Using the "head" thread isn't strictly correct.
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
// XXX Possible GILState issues?
PyCompilerFlags local_flags;
int nomem_count = 0;
#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
filename = PyUnicode_DecodeFSDefault(filename_str);
static int
set_main_loader(PyObject *d, const char *filename, const char *loader_name)
{
- PyInterpreterState *interp;
- PyThreadState *tstate;
PyObject *filename_obj, *bootstrap, *loader_type = NULL, *loader;
int result = 0;
filename_obj = PyUnicode_DecodeFSDefault(filename);
if (filename_obj == NULL)
return -1;
- /* Get current thread state and interpreter pointer */
- tstate = PyThreadState_GET();
- interp = tstate->interp;
+ PyInterpreterState *interp = _PyInterpreterState_Get();
bootstrap = PyObject_GetAttrString(interp->importlib,
"_bootstrap_external");
if (bootstrap != NULL) {
PyObject *
_PySys_GetObjectId(_Py_Identifier *key)
{
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *sd = tstate->interp->sysdict;
- if (sd == NULL)
+ PyObject *sd = _PyInterpreterState_GET_UNSAFE()->sysdict;
+ if (sd == NULL) {
return NULL;
+ }
return _PyDict_GetItemId(sd, key);
}
PyObject *
PySys_GetObject(const char *name)
{
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *sd = tstate->interp->sysdict;
- if (sd == NULL)
+ PyObject *sd = _PyInterpreterState_GET_UNSAFE()->sysdict;
+ if (sd == NULL) {
return NULL;
+ }
return PyDict_GetItemString(sd, name);
}
int
_PySys_SetObjectId(_Py_Identifier *key, PyObject *v)
{
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *sd = tstate->interp->sysdict;
+ PyObject *sd = _PyInterpreterState_GET_UNSAFE()->sysdict;
if (v == NULL) {
- if (_PyDict_GetItemId(sd, key) == NULL)
+ if (_PyDict_GetItemId(sd, key) == NULL) {
return 0;
- else
+ }
+ else {
return _PyDict_DelItemId(sd, key);
+ }
}
- else
+ else {
return _PyDict_SetItemId(sd, key, v);
+ }
}
int
PySys_SetObject(const char *name, PyObject *v)
{
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *sd = tstate->interp->sysdict;
+ PyObject *sd = _PyInterpreterState_GET_UNSAFE()->sysdict;
if (v == NULL) {
- if (PyDict_GetItemString(sd, name) == NULL)
+ if (PyDict_GetItemString(sd, name) == NULL) {
return 0;
- else
+ }
+ else {
return PyDict_DelItemString(sd, name);
+ }
}
- else
+ else {
return PyDict_SetItemString(sd, name, v);
+ }
}
static PyObject *
"are deprecated. Use sys.setswitchinterval() "
"instead.", 1) < 0)
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;
+
+ PyInterpreterState *interp = _PyInterpreterState_Get();
+ interp->check_interval = check_interval;
Py_RETURN_NONE;
}
"are deprecated. Use sys.getswitchinterval() "
"instead.", 1) < 0)
return NULL;
- PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyInterpreterState *interp = _PyInterpreterState_Get();
return PyLong_FromLong(interp->check_interval);
}
sys_setdlopenflags(PyObject *self, PyObject *args)
{
int new_val;
- PyThreadState *tstate = PyThreadState_GET();
if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
return NULL;
- if (!tstate)
- return NULL;
- tstate->interp->dlopenflags = new_val;
+ PyInterpreterState *interp = _PyInterpreterState_Get();
+ interp->dlopenflags = new_val;
Py_RETURN_NONE;
}
static PyObject *
sys_getdlopenflags(PyObject *self, PyObject *args)
{
- PyThreadState *tstate = PyThreadState_GET();
- if (!tstate)
- return NULL;
- return PyLong_FromLong(tstate->interp->dlopenflags);
+ PyInterpreterState *interp = _PyInterpreterState_Get();
+ return PyLong_FromLong(interp->dlopenflags);
}
PyDoc_STRVAR(getdlopenflags_doc,
size_t
PyThread_get_stacksize(void)
{
- return PyThreadState_GET()->interp->pythread_stacksize;
+ return _PyInterpreterState_Get()->pythread_stacksize;
}
/* Only platforms defining a THREAD_SET_STACKSIZE() macro