Skip to content

bpo-39465: Use _PyInterpreterState_GET() #20788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ extern "C" {
#include "pycore_gc.h" /* struct _gc_runtime_state */
#include "pycore_warnings.h" /* struct _warnings_runtime_state */

/* ceval state */
struct _Py_parser_state {
struct {
int level;
int atbol;
} listnode;
};

struct _pending_calls {
PyThread_type_lock lock;
Expand Down Expand Up @@ -209,12 +214,7 @@ struct _is {

PyObject *audit_hooks;

struct {
struct {
int level;
int atbol;
} listnode;
} parser;
struct _Py_parser_state parser;

#if _PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS > 0
/* Small integers are preallocated in this array so that they
Expand Down
57 changes: 25 additions & 32 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ gc_decref(PyGC_Head *g)

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


static GCState *
get_gc_state(void)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
return &interp->gc;
}


void
_PyGC_InitState(GCState *gcstate)
{
Expand Down Expand Up @@ -1466,8 +1475,7 @@ static PyObject *
gc_enable_impl(PyObject *module)
/*[clinic end generated code: output=45a427e9dce9155c input=81ac4940ca579707]*/
{
PyThreadState *tstate = _PyThreadState_GET();
GCState *gcstate = &tstate->interp->gc;
GCState *gcstate = get_gc_state();
gcstate->enabled = 1;
Py_RETURN_NONE;
}
Expand All @@ -1482,8 +1490,7 @@ static PyObject *
gc_disable_impl(PyObject *module)
/*[clinic end generated code: output=97d1030f7aa9d279 input=8c2e5a14e800d83b]*/
{
PyThreadState *tstate = _PyThreadState_GET();
GCState *gcstate = &tstate->interp->gc;
GCState *gcstate = get_gc_state();
gcstate->enabled = 0;
Py_RETURN_NONE;
}
Expand All @@ -1498,8 +1505,7 @@ static int
gc_isenabled_impl(PyObject *module)
/*[clinic end generated code: output=1874298331c49130 input=30005e0422373b31]*/
{
PyThreadState *tstate = _PyThreadState_GET();
GCState *gcstate = &tstate->interp->gc;
GCState *gcstate = get_gc_state();
return gcstate->enabled;
}

Expand Down Expand Up @@ -1564,8 +1570,7 @@ static PyObject *
gc_set_debug_impl(PyObject *module, int flags)
/*[clinic end generated code: output=7c8366575486b228 input=5e5ce15e84fbed15]*/
{
PyThreadState *tstate = _PyThreadState_GET();
GCState *gcstate = &tstate->interp->gc;
GCState *gcstate = get_gc_state();
gcstate->debug = flags;
Py_RETURN_NONE;
}
Expand All @@ -1580,8 +1585,7 @@ static int
gc_get_debug_impl(PyObject *module)
/*[clinic end generated code: output=91242f3506cd1e50 input=91a101e1c3b98366]*/
{
PyThreadState *tstate = _PyThreadState_GET();
GCState *gcstate = &tstate->interp->gc;
GCState *gcstate = get_gc_state();
return gcstate->debug;
}

Expand All @@ -1594,8 +1598,7 @@ PyDoc_STRVAR(gc_set_thresh__doc__,
static PyObject *
gc_set_threshold(PyObject *self, PyObject *args)
{
PyThreadState *tstate = _PyThreadState_GET();
GCState *gcstate = &tstate->interp->gc;
GCState *gcstate = get_gc_state();
if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
&gcstate->generations[0].threshold,
&gcstate->generations[1].threshold,
Expand All @@ -1618,8 +1621,7 @@ static PyObject *
gc_get_threshold_impl(PyObject *module)
/*[clinic end generated code: output=7902bc9f41ecbbd8 input=286d79918034d6e6]*/
{
PyThreadState *tstate = _PyThreadState_GET();
GCState *gcstate = &tstate->interp->gc;
GCState *gcstate = get_gc_state();
return Py_BuildValue("(iii)",
gcstate->generations[0].threshold,
gcstate->generations[1].threshold,
Expand All @@ -1636,8 +1638,7 @@ static PyObject *
gc_get_count_impl(PyObject *module)
/*[clinic end generated code: output=354012e67b16398f input=a392794a08251751]*/
{
PyThreadState *tstate = _PyThreadState_GET();
GCState *gcstate = &tstate->interp->gc;
GCState *gcstate = get_gc_state();
return Py_BuildValue("(iii)",
gcstate->generations[0].count,
gcstate->generations[1].count,
Expand Down Expand Up @@ -1680,15 +1681,13 @@ Return the list of objects that directly refer to any of objs.");
static PyObject *
gc_get_referrers(PyObject *self, PyObject *args)
{
PyThreadState *tstate = _PyThreadState_GET();
int i;
PyObject *result = PyList_New(0);
if (!result) {
return NULL;
}

GCState *gcstate = &tstate->interp->gc;
for (i = 0; i < NUM_GENERATIONS; i++) {
GCState *gcstate = get_gc_state();
for (int i = 0; i < NUM_GENERATIONS; i++) {
if (!(gc_referrers_for(args, GEN_HEAD(gcstate, i), result))) {
Py_DECREF(result);
return NULL;
Expand Down Expand Up @@ -1807,11 +1806,10 @@ gc_get_stats_impl(PyObject *module)
{
int i;
struct gc_generation_stats stats[NUM_GENERATIONS], *st;
PyThreadState *tstate = _PyThreadState_GET();

/* To get consistent values despite allocations while constructing
the result list, we use a snapshot of the running stats. */
GCState *gcstate = &tstate->interp->gc;
GCState *gcstate = get_gc_state();
for (i = 0; i < NUM_GENERATIONS; i++) {
stats[i] = gcstate->generation_stats[i];
}
Expand Down Expand Up @@ -1902,8 +1900,7 @@ static PyObject *
gc_freeze_impl(PyObject *module)
/*[clinic end generated code: output=502159d9cdc4c139 input=b602b16ac5febbe5]*/
{
PyThreadState *tstate = _PyThreadState_GET();
GCState *gcstate = &tstate->interp->gc;
GCState *gcstate = get_gc_state();
for (int i = 0; i < NUM_GENERATIONS; ++i) {
gc_list_merge(GEN_HEAD(gcstate, i), &gcstate->permanent_generation.head);
gcstate->generations[i].count = 0;
Expand All @@ -1923,8 +1920,7 @@ static PyObject *
gc_unfreeze_impl(PyObject *module)
/*[clinic end generated code: output=1c15f2043b25e169 input=2dd52b170f4cef6c]*/
{
PyThreadState *tstate = _PyThreadState_GET();
GCState *gcstate = &tstate->interp->gc;
GCState *gcstate = get_gc_state();
gc_list_merge(&gcstate->permanent_generation.head,
GEN_HEAD(gcstate, NUM_GENERATIONS-1));
Py_RETURN_NONE;
Expand All @@ -1940,8 +1936,7 @@ static Py_ssize_t
gc_get_freeze_count_impl(PyObject *module)
/*[clinic end generated code: output=61cbd9f43aa032e1 input=45ffbc65cfe2a6ed]*/
{
PyThreadState *tstate = _PyThreadState_GET();
GCState *gcstate = &tstate->interp->gc;
GCState *gcstate = get_gc_state();
return gc_list_size(&gcstate->permanent_generation.head);
}

Expand Down Expand Up @@ -2007,8 +2002,7 @@ static struct PyModuleDef gcmodule = {
PyMODINIT_FUNC
PyInit_gc(void)
{
PyThreadState *tstate = _PyThreadState_GET();
GCState *gcstate = &tstate->interp->gc;
GCState *gcstate = get_gc_state();

PyObject *m = PyModule_Create(&gcmodule);

Expand Down Expand Up @@ -2317,8 +2311,7 @@ PyObject_GC_Del(void *op)
if (_PyObject_GC_IS_TRACKED(op)) {
gc_list_remove(g);
}
PyThreadState *tstate = _PyThreadState_GET();
GCState *gcstate = &tstate->interp->gc;
GCState *gcstate = get_gc_state();
if (gcstate->generations[0].count > 0) {
gcstate->generations[0].count--;
}
Expand Down
4 changes: 2 additions & 2 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ static PyObject *
get_small_int(sdigit ival)
{
assert(IS_SMALL_INT(ival));
PyThreadState *tstate = _PyThreadState_GET();
PyObject *v = (PyObject*)tstate->interp->small_ints[ival + NSMALLNEGINTS];
PyInterpreterState *interp = _PyInterpreterState_GET();
PyObject *v = (PyObject*)interp->small_ints[ival + NSMALLNEGINTS];
Py_INCREF(v);
return v;
}
Expand Down
8 changes: 4 additions & 4 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2029,8 +2029,8 @@ Py_ReprLeave(PyObject *obj)
void
_PyTrash_deposit_object(PyObject *op)
{
PyThreadState *tstate = _PyThreadState_GET();
struct _gc_runtime_state *gcstate = &tstate->interp->gc;
PyInterpreterState *interp = _PyInterpreterState_GET();
struct _gc_runtime_state *gcstate = &interp->gc;

_PyObject_ASSERT(op, _PyObject_IS_GC(op));
_PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Expand All @@ -2057,8 +2057,8 @@ _PyTrash_thread_deposit_object(PyObject *op)
void
_PyTrash_destroy_chain(void)
{
PyThreadState *tstate = _PyThreadState_GET();
struct _gc_runtime_state *gcstate = &tstate->interp->gc;
PyInterpreterState *interp = _PyInterpreterState_GET();
struct _gc_runtime_state *gcstate = &interp->gc;

while (gcstate->trash_delete_later) {
PyObject *op = gcstate->trash_delete_later;
Expand Down
18 changes: 9 additions & 9 deletions Parser/listnode.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ listnode(FILE *fp, node *n)
static void
list1node(FILE *fp, node *n)
{
PyInterpreterState *interp;

if (n == NULL)
return;
if (ISNONTERMINAL(TYPE(n))) {
Expand All @@ -40,26 +38,28 @@ list1node(FILE *fp, node *n)
list1node(fp, CHILD(n, i));
}
else if (ISTERMINAL(TYPE(n))) {
interp = _PyInterpreterState_GET();
PyInterpreterState *interp = _PyInterpreterState_GET();
struct _Py_parser_state *parser = &interp->parser;
switch (TYPE(n)) {
case INDENT:
interp->parser.listnode.level++;
parser->listnode.level++;
break;
case DEDENT:
interp->parser.listnode.level--;
parser->listnode.level--;
break;
default:
if (interp->parser.listnode.atbol) {
if (parser->listnode.atbol) {
int i;
for (i = 0; i < interp->parser.listnode.level; ++i)
for (i = 0; i < parser->listnode.level; ++i) {
fprintf(fp, "\t");
interp->parser.listnode.atbol = 0;
}
parser->listnode.atbol = 0;
}
if (TYPE(n) == NEWLINE) {
if (STR(n) != NULL)
fprintf(fp, "%s", STR(n));
fprintf(fp, "\n");
interp->parser.listnode.atbol = 1;
parser->listnode.atbol = 1;
}
else
fprintf(fp, "%s ", STR(n));
Expand Down
12 changes: 6 additions & 6 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ _Py_IDENTIFIER(__name__);
static WarningsState *
warnings_get_state(void)
{
PyThreadState *tstate = _PyThreadState_GET();
if (tstate == NULL) {
_PyErr_SetString(tstate, PyExc_RuntimeError,
"warnings_get_state: could not identify "
"current interpreter");
PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"warnings_get_state: could not identify "
"current interpreter");
return NULL;
}
return &tstate->interp->warnings;
return &interp->warnings;
}

/* Clear the given warnings module state. */
Expand Down
4 changes: 2 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,8 @@ _PyEval_FiniState(struct _ceval_state *ceval)
int
Py_GetRecursionLimit(void)
{
PyThreadState *tstate = _PyThreadState_GET();
return tstate->interp->ceval.recursion_limit;
PyInterpreterState *interp = _PyInterpreterState_GET();
return interp->ceval.recursion_limit;
}

void
Expand Down