Skip to content

Commit 1bcc32f

Browse files
authored
bpo-39465: Use _PyInterpreterState_GET() (GH-20788)
Replace _PyThreadState_GET() with _PyInterpreterState_GET() in: * get_small_int() * gcmodule.c: add also get_gc_state() function * _PyTrash_deposit_object() * _PyTrash_destroy_chain() * warnings_get_state() * Py_GetRecursionLimit() Cleanup listnode.c: add 'parser' variable.
1 parent 9c24e2e commit 1bcc32f

File tree

7 files changed

+55
-62
lines changed

7 files changed

+55
-62
lines changed

Include/internal/pycore_interp.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ extern "C" {
1313
#include "pycore_gc.h" /* struct _gc_runtime_state */
1414
#include "pycore_warnings.h" /* struct _warnings_runtime_state */
1515

16-
/* ceval state */
16+
struct _Py_parser_state {
17+
struct {
18+
int level;
19+
int atbol;
20+
} listnode;
21+
};
1722

1823
struct _pending_calls {
1924
PyThread_type_lock lock;
@@ -209,12 +214,7 @@ struct _is {
209214

210215
PyObject *audit_hooks;
211216

212-
struct {
213-
struct {
214-
int level;
215-
int atbol;
216-
} listnode;
217-
} parser;
217+
struct _Py_parser_state parser;
218218

219219
#if _PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS > 0
220220
/* Small integers are preallocated in this array so that they

Modules/gcmodule.c

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ gc_decref(PyGC_Head *g)
128128

129129
#define GEN_HEAD(gcstate, n) (&(gcstate)->generations[n].head)
130130

131+
132+
static GCState *
133+
get_gc_state(void)
134+
{
135+
PyInterpreterState *interp = _PyInterpreterState_GET();
136+
return &interp->gc;
137+
}
138+
139+
131140
void
132141
_PyGC_InitState(GCState *gcstate)
133142
{
@@ -1465,8 +1474,7 @@ static PyObject *
14651474
gc_enable_impl(PyObject *module)
14661475
/*[clinic end generated code: output=45a427e9dce9155c input=81ac4940ca579707]*/
14671476
{
1468-
PyThreadState *tstate = _PyThreadState_GET();
1469-
GCState *gcstate = &tstate->interp->gc;
1477+
GCState *gcstate = get_gc_state();
14701478
gcstate->enabled = 1;
14711479
Py_RETURN_NONE;
14721480
}
@@ -1481,8 +1489,7 @@ static PyObject *
14811489
gc_disable_impl(PyObject *module)
14821490
/*[clinic end generated code: output=97d1030f7aa9d279 input=8c2e5a14e800d83b]*/
14831491
{
1484-
PyThreadState *tstate = _PyThreadState_GET();
1485-
GCState *gcstate = &tstate->interp->gc;
1492+
GCState *gcstate = get_gc_state();
14861493
gcstate->enabled = 0;
14871494
Py_RETURN_NONE;
14881495
}
@@ -1497,8 +1504,7 @@ static int
14971504
gc_isenabled_impl(PyObject *module)
14981505
/*[clinic end generated code: output=1874298331c49130 input=30005e0422373b31]*/
14991506
{
1500-
PyThreadState *tstate = _PyThreadState_GET();
1501-
GCState *gcstate = &tstate->interp->gc;
1507+
GCState *gcstate = get_gc_state();
15021508
return gcstate->enabled;
15031509
}
15041510

@@ -1563,8 +1569,7 @@ static PyObject *
15631569
gc_set_debug_impl(PyObject *module, int flags)
15641570
/*[clinic end generated code: output=7c8366575486b228 input=5e5ce15e84fbed15]*/
15651571
{
1566-
PyThreadState *tstate = _PyThreadState_GET();
1567-
GCState *gcstate = &tstate->interp->gc;
1572+
GCState *gcstate = get_gc_state();
15681573
gcstate->debug = flags;
15691574
Py_RETURN_NONE;
15701575
}
@@ -1579,8 +1584,7 @@ static int
15791584
gc_get_debug_impl(PyObject *module)
15801585
/*[clinic end generated code: output=91242f3506cd1e50 input=91a101e1c3b98366]*/
15811586
{
1582-
PyThreadState *tstate = _PyThreadState_GET();
1583-
GCState *gcstate = &tstate->interp->gc;
1587+
GCState *gcstate = get_gc_state();
15841588
return gcstate->debug;
15851589
}
15861590

@@ -1593,8 +1597,7 @@ PyDoc_STRVAR(gc_set_thresh__doc__,
15931597
static PyObject *
15941598
gc_set_threshold(PyObject *self, PyObject *args)
15951599
{
1596-
PyThreadState *tstate = _PyThreadState_GET();
1597-
GCState *gcstate = &tstate->interp->gc;
1600+
GCState *gcstate = get_gc_state();
15981601
if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
15991602
&gcstate->generations[0].threshold,
16001603
&gcstate->generations[1].threshold,
@@ -1617,8 +1620,7 @@ static PyObject *
16171620
gc_get_threshold_impl(PyObject *module)
16181621
/*[clinic end generated code: output=7902bc9f41ecbbd8 input=286d79918034d6e6]*/
16191622
{
1620-
PyThreadState *tstate = _PyThreadState_GET();
1621-
GCState *gcstate = &tstate->interp->gc;
1623+
GCState *gcstate = get_gc_state();
16221624
return Py_BuildValue("(iii)",
16231625
gcstate->generations[0].threshold,
16241626
gcstate->generations[1].threshold,
@@ -1635,8 +1637,7 @@ static PyObject *
16351637
gc_get_count_impl(PyObject *module)
16361638
/*[clinic end generated code: output=354012e67b16398f input=a392794a08251751]*/
16371639
{
1638-
PyThreadState *tstate = _PyThreadState_GET();
1639-
GCState *gcstate = &tstate->interp->gc;
1640+
GCState *gcstate = get_gc_state();
16401641
return Py_BuildValue("(iii)",
16411642
gcstate->generations[0].count,
16421643
gcstate->generations[1].count,
@@ -1679,15 +1680,13 @@ Return the list of objects that directly refer to any of objs.");
16791680
static PyObject *
16801681
gc_get_referrers(PyObject *self, PyObject *args)
16811682
{
1682-
PyThreadState *tstate = _PyThreadState_GET();
1683-
int i;
16841683
PyObject *result = PyList_New(0);
16851684
if (!result) {
16861685
return NULL;
16871686
}
16881687

1689-
GCState *gcstate = &tstate->interp->gc;
1690-
for (i = 0; i < NUM_GENERATIONS; i++) {
1688+
GCState *gcstate = get_gc_state();
1689+
for (int i = 0; i < NUM_GENERATIONS; i++) {
16911690
if (!(gc_referrers_for(args, GEN_HEAD(gcstate, i), result))) {
16921691
Py_DECREF(result);
16931692
return NULL;
@@ -1806,11 +1805,10 @@ gc_get_stats_impl(PyObject *module)
18061805
{
18071806
int i;
18081807
struct gc_generation_stats stats[NUM_GENERATIONS], *st;
1809-
PyThreadState *tstate = _PyThreadState_GET();
18101808

18111809
/* To get consistent values despite allocations while constructing
18121810
the result list, we use a snapshot of the running stats. */
1813-
GCState *gcstate = &tstate->interp->gc;
1811+
GCState *gcstate = get_gc_state();
18141812
for (i = 0; i < NUM_GENERATIONS; i++) {
18151813
stats[i] = gcstate->generation_stats[i];
18161814
}
@@ -1901,8 +1899,7 @@ static PyObject *
19011899
gc_freeze_impl(PyObject *module)
19021900
/*[clinic end generated code: output=502159d9cdc4c139 input=b602b16ac5febbe5]*/
19031901
{
1904-
PyThreadState *tstate = _PyThreadState_GET();
1905-
GCState *gcstate = &tstate->interp->gc;
1902+
GCState *gcstate = get_gc_state();
19061903
for (int i = 0; i < NUM_GENERATIONS; ++i) {
19071904
gc_list_merge(GEN_HEAD(gcstate, i), &gcstate->permanent_generation.head);
19081905
gcstate->generations[i].count = 0;
@@ -1922,8 +1919,7 @@ static PyObject *
19221919
gc_unfreeze_impl(PyObject *module)
19231920
/*[clinic end generated code: output=1c15f2043b25e169 input=2dd52b170f4cef6c]*/
19241921
{
1925-
PyThreadState *tstate = _PyThreadState_GET();
1926-
GCState *gcstate = &tstate->interp->gc;
1922+
GCState *gcstate = get_gc_state();
19271923
gc_list_merge(&gcstate->permanent_generation.head,
19281924
GEN_HEAD(gcstate, NUM_GENERATIONS-1));
19291925
Py_RETURN_NONE;
@@ -1939,8 +1935,7 @@ static Py_ssize_t
19391935
gc_get_freeze_count_impl(PyObject *module)
19401936
/*[clinic end generated code: output=61cbd9f43aa032e1 input=45ffbc65cfe2a6ed]*/
19411937
{
1942-
PyThreadState *tstate = _PyThreadState_GET();
1943-
GCState *gcstate = &tstate->interp->gc;
1938+
GCState *gcstate = get_gc_state();
19441939
return gc_list_size(&gcstate->permanent_generation.head);
19451940
}
19461941

@@ -2006,8 +2001,7 @@ static struct PyModuleDef gcmodule = {
20062001
PyMODINIT_FUNC
20072002
PyInit_gc(void)
20082003
{
2009-
PyThreadState *tstate = _PyThreadState_GET();
2010-
GCState *gcstate = &tstate->interp->gc;
2004+
GCState *gcstate = get_gc_state();
20112005

20122006
PyObject *m = PyModule_Create(&gcmodule);
20132007

@@ -2316,8 +2310,7 @@ PyObject_GC_Del(void *op)
23162310
if (_PyObject_GC_IS_TRACKED(op)) {
23172311
gc_list_remove(g);
23182312
}
2319-
PyThreadState *tstate = _PyThreadState_GET();
2320-
GCState *gcstate = &tstate->interp->gc;
2313+
GCState *gcstate = get_gc_state();
23212314
if (gcstate->generations[0].count > 0) {
23222315
gcstate->generations[0].count--;
23232316
}

Objects/longobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ static PyObject *
4141
get_small_int(sdigit ival)
4242
{
4343
assert(IS_SMALL_INT(ival));
44-
PyThreadState *tstate = _PyThreadState_GET();
45-
PyObject *v = (PyObject*)tstate->interp->small_ints[ival + NSMALLNEGINTS];
44+
PyInterpreterState *interp = _PyInterpreterState_GET();
45+
PyObject *v = (PyObject*)interp->small_ints[ival + NSMALLNEGINTS];
4646
Py_INCREF(v);
4747
return v;
4848
}

Objects/object.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,8 +2029,8 @@ Py_ReprLeave(PyObject *obj)
20292029
void
20302030
_PyTrash_deposit_object(PyObject *op)
20312031
{
2032-
PyThreadState *tstate = _PyThreadState_GET();
2033-
struct _gc_runtime_state *gcstate = &tstate->interp->gc;
2032+
PyInterpreterState *interp = _PyInterpreterState_GET();
2033+
struct _gc_runtime_state *gcstate = &interp->gc;
20342034

20352035
_PyObject_ASSERT(op, _PyObject_IS_GC(op));
20362036
_PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
@@ -2057,8 +2057,8 @@ _PyTrash_thread_deposit_object(PyObject *op)
20572057
void
20582058
_PyTrash_destroy_chain(void)
20592059
{
2060-
PyThreadState *tstate = _PyThreadState_GET();
2061-
struct _gc_runtime_state *gcstate = &tstate->interp->gc;
2060+
PyInterpreterState *interp = _PyInterpreterState_GET();
2061+
struct _gc_runtime_state *gcstate = &interp->gc;
20622062

20632063
while (gcstate->trash_delete_later) {
20642064
PyObject *op = gcstate->trash_delete_later;

Parser/listnode.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ listnode(FILE *fp, node *n)
3030
static void
3131
list1node(FILE *fp, node *n)
3232
{
33-
PyInterpreterState *interp;
34-
3533
if (n == NULL)
3634
return;
3735
if (ISNONTERMINAL(TYPE(n))) {
@@ -40,26 +38,28 @@ list1node(FILE *fp, node *n)
4038
list1node(fp, CHILD(n, i));
4139
}
4240
else if (ISTERMINAL(TYPE(n))) {
43-
interp = _PyInterpreterState_GET();
41+
PyInterpreterState *interp = _PyInterpreterState_GET();
42+
struct _Py_parser_state *parser = &interp->parser;
4443
switch (TYPE(n)) {
4544
case INDENT:
46-
interp->parser.listnode.level++;
45+
parser->listnode.level++;
4746
break;
4847
case DEDENT:
49-
interp->parser.listnode.level--;
48+
parser->listnode.level--;
5049
break;
5150
default:
52-
if (interp->parser.listnode.atbol) {
51+
if (parser->listnode.atbol) {
5352
int i;
54-
for (i = 0; i < interp->parser.listnode.level; ++i)
53+
for (i = 0; i < parser->listnode.level; ++i) {
5554
fprintf(fp, "\t");
56-
interp->parser.listnode.atbol = 0;
55+
}
56+
parser->listnode.atbol = 0;
5757
}
5858
if (TYPE(n) == NEWLINE) {
5959
if (STR(n) != NULL)
6060
fprintf(fp, "%s", STR(n));
6161
fprintf(fp, "\n");
62-
interp->parser.listnode.atbol = 1;
62+
parser->listnode.atbol = 1;
6363
}
6464
else
6565
fprintf(fp, "%s ", STR(n));

Python/_warnings.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ _Py_IDENTIFIER(__name__);
3232
static WarningsState *
3333
warnings_get_state(void)
3434
{
35-
PyThreadState *tstate = _PyThreadState_GET();
36-
if (tstate == NULL) {
37-
_PyErr_SetString(tstate, PyExc_RuntimeError,
38-
"warnings_get_state: could not identify "
39-
"current interpreter");
35+
PyInterpreterState *interp = _PyInterpreterState_GET();
36+
if (interp == NULL) {
37+
PyErr_SetString(PyExc_RuntimeError,
38+
"warnings_get_state: could not identify "
39+
"current interpreter");
4040
return NULL;
4141
}
42-
return &tstate->interp->warnings;
42+
return &interp->warnings;
4343
}
4444

4545
/* Clear the given warnings module state. */

Python/ceval.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,8 +788,8 @@ _PyEval_FiniState(struct _ceval_state *ceval)
788788
int
789789
Py_GetRecursionLimit(void)
790790
{
791-
PyThreadState *tstate = _PyThreadState_GET();
792-
return tstate->interp->ceval.recursion_limit;
791+
PyInterpreterState *interp = _PyInterpreterState_GET();
792+
return interp->ceval.recursion_limit;
793793
}
794794

795795
void

0 commit comments

Comments
 (0)