Skip to content

Commit ff4584c

Browse files
authored
bpo-39947: Use _PyInterpreterState_GET_UNSAFE() (GH-18978)
Replace _PyInterpreterState_Get() function call with _PyInterpreterState_GET_UNSAFE() macro which is more efficient but don't check if tstate or interp is NULL. _Py_GetConfigsAsDict() now uses _PyThreadState_GET().
1 parent 6d674a1 commit ff4584c

File tree

9 files changed

+27
-27
lines changed

9 files changed

+27
-27
lines changed

Modules/_threadmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
10611061
boot = PyMem_NEW(struct bootstate, 1);
10621062
if (boot == NULL)
10631063
return PyErr_NoMemory();
1064-
boot->interp = _PyInterpreterState_Get();
1064+
boot->interp = _PyInterpreterState_GET_UNSAFE();
10651065
boot->func = func;
10661066
boot->args = args;
10671067
boot->keyw = keyw;
@@ -1183,7 +1183,7 @@ particular thread within a system.");
11831183
static PyObject *
11841184
thread__count(PyObject *self, PyObject *Py_UNUSED(ignored))
11851185
{
1186-
PyInterpreterState *interp = _PyInterpreterState_Get();
1186+
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
11871187
return PyLong_FromLong(interp->num_threads);
11881188
}
11891189

@@ -1542,7 +1542,7 @@ PyInit__thread(void)
15421542
PyObject *m, *d, *v;
15431543
double time_max;
15441544
double timeout_max;
1545-
PyInterpreterState *interp = _PyInterpreterState_Get();
1545+
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
15461546

15471547
/* Initialize types: */
15481548
if (PyType_Ready(&localdummytype) < 0)

Modules/posixmodule.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ run_at_forkers(PyObject *lst, int reverse)
451451
void
452452
PyOS_BeforeFork(void)
453453
{
454-
run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1);
454+
run_at_forkers(_PyInterpreterState_GET_UNSAFE()->before_forkers, 1);
455455

456456
_PyImport_AcquireLock();
457457
}
@@ -462,7 +462,7 @@ PyOS_AfterFork_Parent(void)
462462
if (_PyImport_ReleaseLock() <= 0)
463463
Py_FatalError("failed releasing import lock after fork");
464464

465-
run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0);
465+
run_at_forkers(_PyInterpreterState_GET_UNSAFE()->after_forkers_parent, 0);
466466
}
467467

468468
void
@@ -476,7 +476,7 @@ PyOS_AfterFork_Child(void)
476476
_PyRuntimeState_ReInitThreads(runtime);
477477
_PyInterpreterState_DeleteExceptMain(runtime);
478478

479-
run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0);
479+
run_at_forkers(_PyInterpreterState_GET_UNSAFE()->after_forkers_child, 0);
480480
}
481481

482482
static int
@@ -6177,7 +6177,7 @@ os_register_at_fork_impl(PyObject *module, PyObject *before,
61776177
check_null_or_callable(after_in_parent, "after_in_parent")) {
61786178
return NULL;
61796179
}
6180-
interp = _PyInterpreterState_Get();
6180+
interp = _PyInterpreterState_GET_UNSAFE();
61816181

61826182
if (register_at_forker(&interp->before_forkers, before)) {
61836183
return NULL;
@@ -6208,7 +6208,7 @@ os_fork1_impl(PyObject *module)
62086208
{
62096209
pid_t pid;
62106210

6211-
if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6211+
if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) {
62126212
PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
62136213
return NULL;
62146214
}
@@ -6243,7 +6243,7 @@ os_fork_impl(PyObject *module)
62436243
{
62446244
pid_t pid;
62456245

6246-
if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6246+
if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) {
62476247
PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
62486248
return NULL;
62496249
}
@@ -6851,7 +6851,7 @@ os_forkpty_impl(PyObject *module)
68516851
int master_fd = -1;
68526852
pid_t pid;
68536853

6854-
if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6854+
if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) {
68556855
PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
68566856
return NULL;
68576857
}

Objects/moduleobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
174174
PyObject *
175175
PyModule_Create2(struct PyModuleDef* module, int module_api_version)
176176
{
177-
if (!_PyImport_IsInitialized(_PyInterpreterState_Get())) {
177+
if (!_PyImport_IsInitialized(_PyInterpreterState_GET_UNSAFE())) {
178178
PyErr_SetString(PyExc_SystemError,
179179
"Python import machinery not initialized");
180180
return NULL;
@@ -699,7 +699,7 @@ module_dealloc(PyModuleObject *m)
699699
static PyObject *
700700
module_repr(PyModuleObject *m)
701701
{
702-
PyInterpreterState *interp = _PyInterpreterState_Get();
702+
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
703703

704704
return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
705705
}

Python/codecs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static int _PyCodecRegistry_Init(void); /* Forward */
3232

3333
int PyCodec_Register(PyObject *search_function)
3434
{
35-
PyInterpreterState *interp = _PyInterpreterState_Get();
35+
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
3636
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
3737
goto onError;
3838
if (search_function == NULL) {
@@ -187,7 +187,7 @@ int _PyCodec_Forget(const char *encoding)
187187
PyObject *v;
188188
int result;
189189

190-
PyInterpreterState *interp = _PyInterpreterState_Get();
190+
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
191191
if (interp->codec_search_path == NULL) {
192192
return -1;
193193
}
@@ -620,7 +620,7 @@ PyObject *_PyCodec_DecodeText(PyObject *object,
620620
Return 0 on success, -1 on error */
621621
int PyCodec_RegisterError(const char *name, PyObject *error)
622622
{
623-
PyInterpreterState *interp = _PyInterpreterState_Get();
623+
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
624624
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
625625
return -1;
626626
if (!PyCallable_Check(error)) {
@@ -1492,7 +1492,7 @@ static int _PyCodecRegistry_Init(void)
14921492
}
14931493
};
14941494

1495-
PyInterpreterState *interp = _PyInterpreterState_Get();
1495+
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
14961496
PyObject *mod;
14971497

14981498
if (interp->codec_search_path != NULL)

Python/dynload_shlib.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* Support for dynamic loading of extension modules */
33

44
#include "Python.h"
5-
#include "pycore_pystate.h"
5+
#include "pycore_pystate.h" // _PyInterpreterState_GET_UNSAFE()
66
#include "importdl.h"
77

88
#include <sys/types.h>
@@ -94,7 +94,7 @@ _PyImport_FindSharedFuncptr(const char *prefix,
9494
}
9595
}
9696

97-
dlopenflags = _PyInterpreterState_Get()->dlopenflags;
97+
dlopenflags = _PyInterpreterState_GET_UNSAFE()->dlopenflags;
9898

9999
handle = dlopen(pathname, dlopenflags);
100100

Python/import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ long
642642
PyImport_GetMagicNumber(void)
643643
{
644644
long res;
645-
PyInterpreterState *interp = _PyInterpreterState_Get();
645+
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
646646
PyObject *external, *pyc_magic;
647647

648648
external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external");
@@ -2412,7 +2412,7 @@ PyInit__imp(void)
24122412
goto failure;
24132413
}
24142414

2415-
const wchar_t *mode = _PyInterpreterState_Get()->config.check_hash_pycs_mode;
2415+
const wchar_t *mode = _PyInterpreterState_GET_UNSAFE()->config.check_hash_pycs_mode;
24162416
PyObject *pyc_mode = PyUnicode_FromWideChar(mode, -1);
24172417
if (pyc_mode == NULL) {
24182418
goto failure;

Python/initconfig.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,8 +2572,8 @@ _Py_GetConfigsAsDict(void)
25722572
Py_CLEAR(dict);
25732573

25742574
/* pre config */
2575-
PyInterpreterState *interp = _PyInterpreterState_Get();
2576-
const PyPreConfig *pre_config = &_PyRuntime.preconfig;
2575+
PyThreadState *tstate = _PyThreadState_GET();
2576+
const PyPreConfig *pre_config = &tstate->interp->runtime->preconfig;
25772577
dict = _PyPreConfig_AsDict(pre_config);
25782578
if (dict == NULL) {
25792579
goto error;
@@ -2584,7 +2584,7 @@ _Py_GetConfigsAsDict(void)
25842584
Py_CLEAR(dict);
25852585

25862586
/* core config */
2587-
const PyConfig *config = &interp->config;
2587+
const PyConfig *config = &tstate->interp->config;
25882588
dict = config_as_dict(config);
25892589
if (dict == NULL) {
25902590
goto error;

Python/pythonrun.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *
9595
PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
9696
int nomem_count = 0;
9797
#ifdef Py_REF_DEBUG
98-
int show_ref_count = _PyInterpreterState_Get()->config.show_ref_count;
98+
int show_ref_count = _PyInterpreterState_GET_UNSAFE()->config.show_ref_count;
9999
#endif
100100

101101
filename = PyUnicode_DecodeFSDefault(filename_str);
@@ -346,7 +346,7 @@ set_main_loader(PyObject *d, const char *filename, const char *loader_name)
346346
filename_obj = PyUnicode_DecodeFSDefault(filename);
347347
if (filename_obj == NULL)
348348
return -1;
349-
PyInterpreterState *interp = _PyInterpreterState_Get();
349+
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
350350
bootstrap = PyObject_GetAttrString(interp->importlib,
351351
"_bootstrap_external");
352352
if (bootstrap != NULL) {
@@ -1117,7 +1117,7 @@ run_eval_code_obj(PyCodeObject *co, PyObject *globals, PyObject *locals)
11171117

11181118
/* Set globals['__builtins__'] if it doesn't exist */
11191119
if (globals != NULL && PyDict_GetItemString(globals, "__builtins__") == NULL) {
1120-
PyInterpreterState *interp = _PyInterpreterState_Get();
1120+
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
11211121
if (PyDict_SetItemString(globals, "__builtins__", interp->builtins) < 0) {
11221122
return NULL;
11231123
}

Python/thread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ PyThread_init_thread(void)
9292
size_t
9393
PyThread_get_stacksize(void)
9494
{
95-
return _PyInterpreterState_Get()->pythread_stacksize;
95+
return _PyInterpreterState_GET_UNSAFE()->pythread_stacksize;
9696
}
9797

9898
/* Only platforms defining a THREAD_SET_STACKSIZE() macro

0 commit comments

Comments
 (0)