Skip to content

Commit d60dcb1

Browse files
vsajipJake Taylor
authored and
Jake Taylor
committed
bpo-36876: Moved Parser/listnode.c statics to interpreter state. (pythonGH-16328)
1 parent db08598 commit d60dcb1

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

Include/internal/pycore_pystate.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ struct _is {
125125
struct _warnings_runtime_state warnings;
126126

127127
PyObject *audit_hooks;
128+
/*
129+
* See bpo-36876: miscellaneous ad hoc statics have been moved here.
130+
*/
131+
struct {
132+
struct {
133+
int level;
134+
int atbol;
135+
} listnode;
136+
} parser;
128137
};
129138

130139
PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(PY_INT64_T);

Parser/listnode.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* List a node on a file */
33

44
#include "Python.h"
5+
#include "pycore_pystate.h"
56
#include "token.h"
67
#include "node.h"
78

@@ -15,19 +16,21 @@ PyNode_ListTree(node *n)
1516
listnode(stdout, n);
1617
}
1718

18-
static int level, atbol;
19-
2019
static void
2120
listnode(FILE *fp, node *n)
2221
{
23-
level = 0;
24-
atbol = 1;
22+
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
23+
24+
interp->parser.listnode.level = 0;
25+
interp->parser.listnode.atbol = 1;
2526
list1node(fp, n);
2627
}
2728

2829
static void
2930
list1node(FILE *fp, node *n)
3031
{
32+
PyInterpreterState *interp;
33+
3134
if (n == NULL)
3235
return;
3336
if (ISNONTERMINAL(TYPE(n))) {
@@ -36,25 +39,26 @@ list1node(FILE *fp, node *n)
3639
list1node(fp, CHILD(n, i));
3740
}
3841
else if (ISTERMINAL(TYPE(n))) {
42+
interp = _PyInterpreterState_GET_UNSAFE();
3943
switch (TYPE(n)) {
4044
case INDENT:
41-
++level;
45+
interp->parser.listnode.level++;
4246
break;
4347
case DEDENT:
44-
--level;
48+
interp->parser.listnode.level--;
4549
break;
4650
default:
47-
if (atbol) {
51+
if (interp->parser.listnode.atbol) {
4852
int i;
49-
for (i = 0; i < level; ++i)
53+
for (i = 0; i < interp->parser.listnode.level; ++i)
5054
fprintf(fp, "\t");
51-
atbol = 0;
55+
interp->parser.listnode.atbol = 0;
5256
}
5357
if (TYPE(n) == NEWLINE) {
5458
if (STR(n) != NULL)
5559
fprintf(fp, "%s", STR(n));
5660
fprintf(fp, "\n");
57-
atbol = 1;
61+
interp->parser.listnode.atbol = 1;
5862
}
5963
else
6064
fprintf(fp, "%s ", STR(n));

0 commit comments

Comments
 (0)