Skip to content

Commit

Permalink
bpo-34301: Add _PyInterpreterState_Get() helper function (GH-8592)
Browse files Browse the repository at this point in the history
sys_setcheckinterval() now uses a local variable to parse arguments,
before writing into interp->check_interval.
  • Loading branch information
vstinner committed Aug 3, 2018
1 parent 2ebd381 commit caba55b
Show file tree
Hide file tree
Showing 22 changed files with 107 additions and 93 deletions.
7 changes: 7 additions & 0 deletions Include/pystate.h
Expand Up @@ -245,6 +245,13 @@ typedef struct _ts {
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 *);
Expand Down
10 changes: 5 additions & 5 deletions Modules/_threadmodule.c
Expand Up @@ -1049,7 +1049,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
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;
Expand Down Expand Up @@ -1154,8 +1154,8 @@ A thread's identity may be reused for another thread after it exits.");
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,
Expand Down Expand Up @@ -1348,7 +1348,7 @@ PyInit__thread(void)
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)
Expand Down Expand Up @@ -1395,7 +1395,7 @@ PyInit__thread(void)
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)
Expand Down
7 changes: 3 additions & 4 deletions Modules/_xxsubinterpretersmodule.c
Expand Up @@ -26,10 +26,9 @@ _copy_raw_string(PyObject *strobj)
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
Expand Down Expand Up @@ -1941,7 +1940,7 @@ _run_script_in_interpreter(PyInterpreterState *interp, const char *codestr,

// 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?
Expand Down
8 changes: 4 additions & 4 deletions Modules/posixmodule.c
Expand Up @@ -437,7 +437,7 @@ run_at_forkers(PyObject *lst, int reverse)
void
PyOS_BeforeFork(void)
{
run_at_forkers(PyThreadState_Get()->interp->before_forkers, 1);
run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1);

_PyImport_AcquireLock();
}
Expand All @@ -448,7 +448,7 @@ PyOS_AfterFork_Parent(void)
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
Expand All @@ -459,7 +459,7 @@ PyOS_AfterFork_Child(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
Expand Down Expand Up @@ -5655,7 +5655,7 @@ os_register_at_fork_impl(PyObject *module, PyObject *before,
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;
Expand Down
4 changes: 2 additions & 2 deletions Modules/zipimport.c
Expand Up @@ -1074,7 +1074,7 @@ read_directory(PyObject *archive)
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.
Expand Down Expand Up @@ -1351,7 +1351,7 @@ unmarshal_code(PyObject *pathname, PyObject *data, time_t mtime)

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
Expand Down
5 changes: 3 additions & 2 deletions Objects/codeobject.c
Expand Up @@ -3,6 +3,7 @@
#include "Python.h"
#include "code.h"
#include "structmember.h"
#include "internal/pystate.h"

/* Holder for co_extra information */
typedef struct {
Expand Down Expand Up @@ -428,7 +429,7 @@ static void
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++) {
Expand Down Expand Up @@ -871,7 +872,7 @@ _PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
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) {
Expand Down
2 changes: 1 addition & 1 deletion Objects/listobject.c
Expand Up @@ -85,7 +85,7 @@ static size_t count_reuse = 0;
static void
show_alloc(void)
{
PyInterpreterState *interp = PyThreadState_GET()->interp;
PyInterpreterState *interp = _PyInterpreterState_Get();
if (!interp->core_config.show_alloc_count) {
return;
}
Expand Down
5 changes: 2 additions & 3 deletions Objects/moduleobject.c
Expand Up @@ -173,7 +173,7 @@ _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
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);
}
Expand Down Expand Up @@ -693,8 +693,7 @@ module_dealloc(PyModuleObject *m)
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);
}
Expand Down
2 changes: 1 addition & 1 deletion Objects/object.c
Expand Up @@ -96,7 +96,7 @@ extern Py_ssize_t null_strings, one_strings;
void
dump_counts(FILE* f)
{
PyInterpreterState *interp = PyThreadState_GET()->interp;
PyInterpreterState *interp = _PyInterpreterState_Get();
if (!interp->core_config.show_alloc_count) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion Objects/tupleobject.c
Expand Up @@ -44,7 +44,7 @@ static Py_ssize_t count_tracked = 0;
static void
show_track(void)
{
PyInterpreterState *interp = PyThreadState_GET()->interp;
PyInterpreterState *interp = _PyInterpreterState_Get();
if (!interp->core_config.show_alloc_count) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions Objects/unicodeobject.c
Expand Up @@ -3413,7 +3413,7 @@ PyUnicode_EncodeFSDefault(PyObject *unicode)
#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
Expand Down Expand Up @@ -3639,7 +3639,7 @@ PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
#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
Expand Down
2 changes: 1 addition & 1 deletion Programs/_testembed.c
Expand Up @@ -306,7 +306,7 @@ dump_config(void)
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);
Expand Down
4 changes: 2 additions & 2 deletions Python/_warnings.c
Expand Up @@ -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
an interpreter abort.
*/
if (!PyThreadState_GET()->interp->modules) {
if (!_PyInterpreterState_GET_UNSAFE()->modules) {
return NULL;
}
warnings_module = PyImport_GetModule(warnings_str);
Expand Down Expand Up @@ -686,7 +686,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
}

if (f == NULL) {
globals = PyThreadState_Get()->interp->sysdict;
globals = _PyInterpreterState_GET_UNSAFE()->sysdict;
*filename = PyUnicode_FromString("sys");
*lineno = 1;
}
Expand Down
10 changes: 5 additions & 5 deletions Python/ceval.c
Expand Up @@ -532,8 +532,8 @@ PyEval_EvalFrame(PyFrameObject *f) {
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
Expand Down Expand Up @@ -4435,7 +4435,7 @@ PyEval_GetBuiltins(void)
{
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;
}
Expand Down Expand Up @@ -4769,7 +4769,7 @@ import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *leve
}

/* 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;
Expand Down Expand Up @@ -5136,7 +5136,7 @@ _Py_GetDXProfile(PyObject *self, PyObject *args)
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) {
Expand Down
14 changes: 6 additions & 8 deletions Python/codecs.c
Expand Up @@ -32,7 +32,7 @@ static int _PyCodecRegistry_Init(void); /* Forward */

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) {
Expand Down Expand Up @@ -99,7 +99,6 @@ PyObject *normalizestring(const char *string)

PyObject *_PyCodec_Lookup(const char *encoding)
{
PyInterpreterState *interp;
PyObject *result, *args = NULL, *v;
Py_ssize_t i, len;

Expand All @@ -108,7 +107,7 @@ PyObject *_PyCodec_Lookup(const char *encoding)
goto onError;
}

interp = PyThreadState_GET()->interp;
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
goto onError;

Expand Down Expand Up @@ -187,11 +186,10 @@ PyObject *_PyCodec_Lookup(const char *encoding)

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;
}
Expand Down Expand Up @@ -624,7 +622,7 @@ PyObject *_PyCodec_DecodeText(PyObject *object,
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)) {
Expand All @@ -642,7 +640,7 @@ PyObject *PyCodec_LookupError(const char *name)
{
PyObject *handler = NULL;

PyInterpreterState *interp = PyThreadState_GET()->interp;
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
return NULL;

Expand Down Expand Up @@ -1494,7 +1492,7 @@ static int _PyCodecRegistry_Init(void)
}
};

PyInterpreterState *interp = PyThreadState_GET()->interp;
PyInterpreterState *interp = _PyInterpreterState_Get();
PyObject *mod;
unsigned i;

Expand Down
2 changes: 1 addition & 1 deletion Python/dynload_shlib.c
Expand Up @@ -91,7 +91,7 @@ _PyImport_FindSharedFuncptr(const char *prefix,
}
}

dlopenflags = PyThreadState_GET()->interp->dlopenflags;
dlopenflags = _PyInterpreterState_Get()->dlopenflags;

handle = dlopen(pathname, dlopenflags);

Expand Down

0 comments on commit caba55b

Please sign in to comment.