From 3c82f2edd253c20a1d31de4ce6dd7a9cb6b31ef6 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Sun, 22 Sep 2019 05:38:51 +0100 Subject: [PATCH 1/2] Moved Parser/listnode.c statics to interpreter state. --- Include/internal/pycore_pystate.h | 9 +++++++++ Parser/listnode.c | 24 ++++++++++++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 8e5a022b4a09aa..52666ab7c40242 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -132,6 +132,15 @@ struct _is { struct _warnings_runtime_state warnings; PyObject *audit_hooks; +/* + * See bpo-36876: miscellaneous ad hoc statics have been moved here. + */ + struct { + struct { + int level; + int atbol; + } listnode_data; + } parser_data; }; PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(PY_INT64_T); diff --git a/Parser/listnode.c b/Parser/listnode.c index 8f1a1163b63d5c..5998396694645b 100644 --- a/Parser/listnode.c +++ b/Parser/listnode.c @@ -2,6 +2,7 @@ /* List a node on a file */ #include "Python.h" +#include "pycore_pystate.h" #include "token.h" #include "node.h" @@ -15,19 +16,21 @@ PyNode_ListTree(node *n) listnode(stdout, n); } -static int level, atbol; - static void listnode(FILE *fp, node *n) { - level = 0; - atbol = 1; + PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); + + interp->parser_data.listnode_data.level = 0; + interp->parser_data.listnode_data.atbol = 1; list1node(fp, n); } static void list1node(FILE *fp, node *n) { + PyInterpreterState *interp; + if (n == NULL) return; if (ISNONTERMINAL(TYPE(n))) { @@ -36,25 +39,26 @@ list1node(FILE *fp, node *n) list1node(fp, CHILD(n, i)); } else if (ISTERMINAL(TYPE(n))) { + interp = _PyInterpreterState_GET_UNSAFE(); switch (TYPE(n)) { case INDENT: - ++level; + interp->parser_data.listnode_data.level++; break; case DEDENT: - --level; + interp->parser_data.listnode_data.level--; break; default: - if (atbol) { + if (interp->parser_data.listnode_data.atbol) { int i; - for (i = 0; i < level; ++i) + for (i = 0; i < interp->parser_data.listnode_data.level; ++i) fprintf(fp, "\t"); - atbol = 0; + interp->parser_data.listnode_data.atbol = 0; } if (TYPE(n) == NEWLINE) { if (STR(n) != NULL) fprintf(fp, "%s", STR(n)); fprintf(fp, "\n"); - atbol = 1; + interp->parser_data.listnode_data.atbol = 1; } else fprintf(fp, "%s ", STR(n)); From 4c88f82c7a7e58e1d5a20683b954a6b60b785dd9 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Thu, 7 Nov 2019 08:56:20 +0000 Subject: [PATCH 2/2] Addressed review comments. --- Include/internal/pycore_pystate.h | 4 ++-- Parser/listnode.c | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 52666ab7c40242..7108727027b509 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -139,8 +139,8 @@ struct _is { struct { int level; int atbol; - } listnode_data; - } parser_data; + } listnode; + } parser; }; PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(PY_INT64_T); diff --git a/Parser/listnode.c b/Parser/listnode.c index 5998396694645b..d431ae537e3b41 100644 --- a/Parser/listnode.c +++ b/Parser/listnode.c @@ -21,8 +21,8 @@ listnode(FILE *fp, node *n) { PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); - interp->parser_data.listnode_data.level = 0; - interp->parser_data.listnode_data.atbol = 1; + interp->parser.listnode.level = 0; + interp->parser.listnode.atbol = 1; list1node(fp, n); } @@ -42,23 +42,23 @@ list1node(FILE *fp, node *n) interp = _PyInterpreterState_GET_UNSAFE(); switch (TYPE(n)) { case INDENT: - interp->parser_data.listnode_data.level++; + interp->parser.listnode.level++; break; case DEDENT: - interp->parser_data.listnode_data.level--; + interp->parser.listnode.level--; break; default: - if (interp->parser_data.listnode_data.atbol) { + if (interp->parser.listnode.atbol) { int i; - for (i = 0; i < interp->parser_data.listnode_data.level; ++i) + for (i = 0; i < interp->parser.listnode.level; ++i) fprintf(fp, "\t"); - interp->parser_data.listnode_data.atbol = 0; + interp->parser.listnode.atbol = 0; } if (TYPE(n) == NEWLINE) { if (STR(n) != NULL) fprintf(fp, "%s", STR(n)); fprintf(fp, "\n"); - interp->parser_data.listnode_data.atbol = 1; + interp->parser.listnode.atbol = 1; } else fprintf(fp, "%s ", STR(n));