Skip to content

Commit 85ea281

Browse files
committed
gh-106320: Use _PyInterpreterState_GET()
Replace PyInterpreterState_Get() with inlined _PyInterpreterState_GET().
1 parent feb51f3 commit 85ea281

11 files changed

+34
-35
lines changed

Modules/_asynciomodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ future_init(FutureObj *fut, PyObject *loop)
527527
if (is_true < 0) {
528528
return -1;
529529
}
530-
if (is_true && !_Py_IsInterpreterFinalizing(PyInterpreterState_Get())) {
530+
if (is_true && !_Py_IsInterpreterFinalizing(_PyInterpreterState_GET())) {
531531
/* Only try to capture the traceback if the interpreter is not being
532532
finalized. The original motivation to add a `_Py_IsFinalizing()`
533533
call was to prevent SIGSEGV when a Future is created in a __del__

Modules/_io/bufferedio.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ _enter_buffered_busy(buffered *self)
293293
"reentrant call inside %R", self);
294294
return 0;
295295
}
296-
PyInterpreterState *interp = PyInterpreterState_Get();
296+
PyInterpreterState *interp = _PyInterpreterState_GET();
297297
relax_locking = _Py_IsInterpreterFinalizing(interp);
298298
Py_BEGIN_ALLOW_THREADS
299299
if (!relax_locking)

Modules/_posixsubprocess.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ subprocess_fork_exec_impl(PyObject *module, PyObject *process_args,
10251025
int *c_fds_to_keep = NULL;
10261026
Py_ssize_t fds_to_keep_len = PyTuple_GET_SIZE(py_fds_to_keep);
10271027

1028-
PyInterpreterState *interp = PyInterpreterState_Get();
1028+
PyInterpreterState *interp = _PyInterpreterState_GET();
10291029
if ((preexec_fn != Py_None) && interp->finalizing) {
10301030
PyErr_SetString(PyExc_RuntimeError,
10311031
"preexec_fn not supported at interpreter shutdown");

Modules/_testinternalcapi.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ static PyObject *
561561
set_eval_frame_default(PyObject *self, PyObject *Py_UNUSED(args))
562562
{
563563
module_state *state = get_module_state(self);
564-
_PyInterpreterState_SetEvalFrameFunc(PyInterpreterState_Get(), _PyEval_EvalFrameDefault);
564+
_PyInterpreterState_SetEvalFrameFunc(_PyInterpreterState_GET(), _PyEval_EvalFrameDefault);
565565
Py_CLEAR(state->record_list);
566566
Py_RETURN_NONE;
567567
}
@@ -589,7 +589,7 @@ set_eval_frame_record(PyObject *self, PyObject *list)
589589
return NULL;
590590
}
591591
Py_XSETREF(state->record_list, Py_NewRef(list));
592-
_PyInterpreterState_SetEvalFrameFunc(PyInterpreterState_Get(), record_eval);
592+
_PyInterpreterState_SetEvalFrameFunc(_PyInterpreterState_GET(), record_eval);
593593
Py_RETURN_NONE;
594594
}
595595

@@ -885,7 +885,7 @@ pending_threadfunc(PyObject *self, PyObject *args, PyObject *kwargs)
885885
{
886886
return NULL;
887887
}
888-
PyInterpreterState *interp = PyInterpreterState_Get();
888+
PyInterpreterState *interp = _PyInterpreterState_GET();
889889

890890
/* create the reference for the callbackwhile we hold the lock */
891891
Py_INCREF(callable);

Modules/_typingmodule.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
#endif
66

77
#include "Python.h"
8-
#include "internal/pycore_interp.h"
9-
#include "internal/pycore_typevarobject.h"
8+
#include "pycore_interp.h"
9+
#include "pycore_pystate.h" // _PyInterpreterState_GET()
10+
#include "pycore_typevarobject.h"
1011
#include "clinic/_typingmodule.c.h"
1112

1213
/*[clinic input]
@@ -44,7 +45,7 @@ PyDoc_STRVAR(typing_doc,
4445
static int
4546
_typing_exec(PyObject *m)
4647
{
47-
PyInterpreterState *interp = PyInterpreterState_Get();
48+
PyInterpreterState *interp = _PyInterpreterState_GET();
4849

4950
#define EXPORT_TYPE(name, typename) \
5051
if (PyModule_AddObjectRef(m, name, \

Modules/_winapi.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include "Python.h"
3838
#include "pycore_moduleobject.h" // _PyModule_GetState()
39+
#include "pycore_pystate.h" // _PyInterpreterState_GET
3940
#include "structmember.h" // PyMemberDef
4041

4142

@@ -133,7 +134,7 @@ overlapped_dealloc(OverlappedObject *self)
133134
{
134135
/* The operation is no longer pending -- nothing to do. */
135136
}
136-
else if (_Py_IsInterpreterFinalizing(PyInterpreterState_Get()))
137+
else if (_Py_IsInterpreterFinalizing(_PyInterpreterState_GET()))
137138
{
138139
/* The operation is still pending -- give a warning. This
139140
will probably only happen on Windows XP. */

Objects/typevarobject.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// TypeVar, TypeVarTuple, and ParamSpec
22
#include "Python.h"
3-
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
3+
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
44
#include "pycore_typevarobject.h"
55
#include "pycore_unionobject.h" // _Py_union_type_or
66
#include "structmember.h"
@@ -144,7 +144,7 @@ static int
144144
contains_typevartuple(PyTupleObject *params)
145145
{
146146
Py_ssize_t n = PyTuple_GET_SIZE(params);
147-
PyTypeObject *tp = PyInterpreterState_Get()->cached_objects.typevartuple_type;
147+
PyTypeObject *tp = _PyInterpreterState_GET()->cached_objects.typevartuple_type;
148148
for (Py_ssize_t i = 0; i < n; i++) {
149149
PyObject *param = PyTuple_GET_ITEM(params, i);
150150
if (Py_IS_TYPE(param, tp)) {
@@ -165,7 +165,7 @@ unpack_typevartuples(PyObject *params)
165165
if (new_params == NULL) {
166166
return NULL;
167167
}
168-
PyTypeObject *tp = PyInterpreterState_Get()->cached_objects.typevartuple_type;
168+
PyTypeObject *tp = _PyInterpreterState_GET()->cached_objects.typevartuple_type;
169169
for (Py_ssize_t i = 0; i < n; i++) {
170170
PyObject *param = PyTuple_GET_ITEM(params, i);
171171
if (Py_IS_TYPE(param, tp)) {
@@ -291,7 +291,7 @@ typevar_alloc(PyObject *name, PyObject *bound, PyObject *evaluate_bound,
291291
bool covariant, bool contravariant, bool infer_variance,
292292
PyObject *module)
293293
{
294-
PyTypeObject *tp = PyInterpreterState_Get()->cached_objects.typevar_type;
294+
PyTypeObject *tp = _PyInterpreterState_GET()->cached_objects.typevar_type;
295295
assert(tp != NULL);
296296
typevarobject *tv = PyObject_GC_New(typevarobject, tp);
297297
if (tv == NULL) {
@@ -576,7 +576,7 @@ paramspecargs_repr(PyObject *self)
576576
{
577577
paramspecattrobject *psa = (paramspecattrobject *)self;
578578

579-
PyTypeObject *tp = PyInterpreterState_Get()->cached_objects.paramspec_type;
579+
PyTypeObject *tp = _PyInterpreterState_GET()->cached_objects.paramspec_type;
580580
if (Py_IS_TYPE(psa->__origin__, tp)) {
581581
return PyUnicode_FromFormat("%U.args",
582582
((paramspecobject *)psa->__origin__)->name);
@@ -656,7 +656,7 @@ paramspeckwargs_repr(PyObject *self)
656656
{
657657
paramspecattrobject *psk = (paramspecattrobject *)self;
658658

659-
PyTypeObject *tp = PyInterpreterState_Get()->cached_objects.paramspec_type;
659+
PyTypeObject *tp = _PyInterpreterState_GET()->cached_objects.paramspec_type;
660660
if (Py_IS_TYPE(psk->__origin__, tp)) {
661661
return PyUnicode_FromFormat("%U.kwargs",
662662
((paramspecobject *)psk->__origin__)->name);
@@ -789,14 +789,14 @@ static PyMemberDef paramspec_members[] = {
789789
static PyObject *
790790
paramspec_args(PyObject *self, void *unused)
791791
{
792-
PyTypeObject *tp = PyInterpreterState_Get()->cached_objects.paramspecargs_type;
792+
PyTypeObject *tp = _PyInterpreterState_GET()->cached_objects.paramspecargs_type;
793793
return (PyObject *)paramspecattr_new(tp, self);
794794
}
795795

796796
static PyObject *
797797
paramspec_kwargs(PyObject *self, void *unused)
798798
{
799-
PyTypeObject *tp = PyInterpreterState_Get()->cached_objects.paramspeckwargs_type;
799+
PyTypeObject *tp = _PyInterpreterState_GET()->cached_objects.paramspeckwargs_type;
800800
return (PyObject *)paramspecattr_new(tp, self);
801801
}
802802

@@ -810,7 +810,7 @@ static paramspecobject *
810810
paramspec_alloc(PyObject *name, PyObject *bound, bool covariant,
811811
bool contravariant, bool infer_variance, PyObject *module)
812812
{
813-
PyTypeObject *tp = PyInterpreterState_Get()->cached_objects.paramspec_type;
813+
PyTypeObject *tp = _PyInterpreterState_GET()->cached_objects.paramspec_type;
814814
paramspecobject *ps = PyObject_GC_New(paramspecobject, tp);
815815
if (ps == NULL) {
816816
return NULL;
@@ -1059,7 +1059,7 @@ static PyMemberDef typevartuple_members[] = {
10591059
static typevartupleobject *
10601060
typevartuple_alloc(PyObject *name, PyObject *module)
10611061
{
1062-
PyTypeObject *tp = PyInterpreterState_Get()->cached_objects.typevartuple_type;
1062+
PyTypeObject *tp = _PyInterpreterState_GET()->cached_objects.typevartuple_type;
10631063
typevartupleobject *tvt = PyObject_GC_New(typevartupleobject, tp);
10641064
if (tvt == NULL) {
10651065
return NULL;
@@ -1598,7 +1598,7 @@ _Py_subscript_generic(PyThreadState* unused, PyObject *params)
15981598
{
15991599
params = unpack_typevartuples(params);
16001600

1601-
PyInterpreterState *interp = PyInterpreterState_Get();
1601+
PyInterpreterState *interp = _PyInterpreterState_GET();
16021602
if (interp->cached_objects.generic_type == NULL) {
16031603
PyErr_SetString(PyExc_SystemError, "Cannot find Generic type");
16041604
return NULL;

Python/codecs.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Copyright (c) Corporation for National Research Initiatives.
1111
#include "Python.h"
1212
#include "pycore_call.h" // _PyObject_CallNoArgs()
1313
#include "pycore_interp.h" // PyInterpreterState.codec_search_path
14-
#include "pycore_pyerrors.h" // _PyErr_FormatNote()
14+
#include "pycore_pyerrors.h" // _PyErr_FormatNote()
1515
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1616
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
1717
#include <ctype.h>
@@ -55,7 +55,7 @@ int PyCodec_Register(PyObject *search_function)
5555
int
5656
PyCodec_Unregister(PyObject *search_function)
5757
{
58-
PyInterpreterState *interp = PyInterpreterState_Get();
58+
PyInterpreterState *interp = _PyInterpreterState_GET();
5959
PyObject *codec_search_path = interp->codec_search_path;
6060
/* Do nothing if codec_search_path is not created yet or was cleared. */
6161
if (codec_search_path == NULL) {

Python/instrumentation.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
#include "Python.h"
42
#include "pycore_call.h"
53
#include "pycore_frame.h"
@@ -9,7 +7,7 @@
97
#include "pycore_object.h"
108
#include "pycore_opcode.h"
119
#include "pycore_pyerrors.h"
12-
#include "pycore_pystate.h"
10+
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1311

1412
/* Uncomment this to dump debugging output when assertions fail */
1513
// #define INSTRUMENT_DEBUG 1
@@ -390,7 +388,7 @@ dump_instrumentation_data(PyCodeObject *code, int star, FILE*out)
390388
fprintf(out, "NULL\n");
391389
return;
392390
}
393-
dump_monitors("Global", PyInterpreterState_Get()->monitors, out);
391+
dump_monitors("Global", _PyInterpreterState_GET()->monitors, out);
394392
dump_monitors("Code", data->local_monitors, out);
395393
dump_monitors("Active", data->active_monitors, out);
396394
int code_len = (int)Py_SIZE(code);
@@ -449,7 +447,7 @@ sanity_check_instrumentation(PyCodeObject *code)
449447
if (data == NULL) {
450448
return;
451449
}
452-
_Py_Monitors active_monitors = PyInterpreterState_Get()->monitors;
450+
_Py_Monitors active_monitors = _PyInterpreterState_GET()->monitors;
453451
if (code->_co_monitoring) {
454452
_Py_Monitors local_monitors = code->_co_monitoring->local_monitors;
455453
active_monitors = monitors_or(active_monitors, local_monitors);
@@ -740,7 +738,7 @@ remove_tools(PyCodeObject * code, int offset, int event, int tools)
740738
static bool
741739
tools_is_subset_for_event(PyCodeObject * code, int event, int tools)
742740
{
743-
int global_tools = PyInterpreterState_Get()->monitors.tools[event];
741+
int global_tools = _PyInterpreterState_GET()->monitors.tools[event];
744742
int local_tools = code->_co_monitoring->local_monitors.tools[event];
745743
return tools == ((global_tools | local_tools) & tools);
746744
}

Python/optimizer.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
21
#include "Python.h"
32
#include "opcode.h"
43
#include "pycore_interp.h"
54
#include "pycore_opcode.h"
65
#include "opcode_metadata.h"
7-
#include "pycore_pystate.h"
6+
#include "pycore_pystate.h" // _PyInterpreterState_GET()
87
#include "pycore_uops.h"
98
#include "cpython/optimizer.h"
109
#include <stdbool.h>
@@ -125,7 +124,7 @@ _PyOptimizerObject _PyOptimizer_Default = {
125124
_PyOptimizerObject *
126125
PyUnstable_GetOptimizer(void)
127126
{
128-
PyInterpreterState *interp = PyInterpreterState_Get();
127+
PyInterpreterState *interp = _PyInterpreterState_GET();
129128
if (interp->optimizer == &_PyOptimizer_Default) {
130129
return NULL;
131130
}
@@ -138,7 +137,7 @@ PyUnstable_GetOptimizer(void)
138137
void
139138
PyUnstable_SetOptimizer(_PyOptimizerObject *optimizer)
140139
{
141-
PyInterpreterState *interp = PyInterpreterState_Get();
140+
PyInterpreterState *interp = _PyInterpreterState_GET();
142141
if (optimizer == NULL) {
143142
optimizer = &_PyOptimizer_Default;
144143
}
@@ -155,7 +154,7 @@ _PyOptimizer_BackEdge(_PyInterpreterFrame *frame, _Py_CODEUNIT *src, _Py_CODEUNI
155154
{
156155
PyCodeObject *code = (PyCodeObject *)frame->f_executable;
157156
assert(PyCode_Check(code));
158-
PyInterpreterState *interp = PyInterpreterState_Get();
157+
PyInterpreterState *interp = _PyInterpreterState_GET();
159158
if (!has_space_for_executor(code, src)) {
160159
goto jump_to_destination;
161160
}

Python/pystate.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2835,7 +2835,7 @@ _PyInterpreterState_GetConfig(PyInterpreterState *interp)
28352835
int
28362836
_PyInterpreterState_GetConfigCopy(PyConfig *config)
28372837
{
2838-
PyInterpreterState *interp = PyInterpreterState_Get();
2838+
PyInterpreterState *interp = _PyInterpreterState_GET();
28392839

28402840
PyStatus status = _PyConfig_Copy(config, &interp->config);
28412841
if (PyStatus_Exception(status)) {

0 commit comments

Comments
 (0)