[Python-checkins] gh-81057: Move the Cached Parser Dummy Name to _PyRuntimeState (#100277)

pablogsal webhook-mailer at python.org
Fri Dec 16 08:48:09 EST 2022


https://github.com/python/cpython/commit/0415cf895f96ae3f896f1f25f0c030a820845e13
commit: 0415cf895f96ae3f896f1f25f0c030a820845e13
branch: main
author: Eric Snow <ericsnowcurrently at gmail.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2022-12-16T13:48:03Z
summary:

gh-81057: Move the Cached Parser Dummy Name to _PyRuntimeState (#100277)

files:
M Include/internal/pycore_parser.h
M Include/internal/pycore_runtime_init.h
M Parser/action_helpers.c
M Tools/c-analyzer/cpython/ignored.tsv

diff --git a/Include/internal/pycore_parser.h b/Include/internal/pycore_parser.h
index 2d2b56bd824c..dd51b92801ae 100644
--- a/Include/internal/pycore_parser.h
+++ b/Include/internal/pycore_parser.h
@@ -9,6 +9,8 @@ extern "C" {
 #endif
 
 
+#include "pycore_ast.h"             // struct _expr
+#include "pycore_global_strings.h"  // _Py_DECLARE_STR()
 #include "pycore_pyarena.h"         // PyArena
 
 
@@ -22,9 +24,22 @@ struct _parser_runtime_state {
 #else
     int _not_used;
 #endif
+    struct _expr dummy_name;
 };
 
-
+_Py_DECLARE_STR(empty, "")
+#define _parser_runtime_state_INIT \
+    { \
+        .dummy_name = { \
+            .kind = Name_kind, \
+            .v.Name.id = &_Py_STR(empty), \
+            .v.Name.ctx = Load, \
+            .lineno = 1, \
+            .col_offset = 0, \
+            .end_lineno = 1, \
+            .end_col_offset = 0, \
+        }, \
+    }
 
 extern struct _mod* _PyParser_ASTFromString(
     const char *str,
diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h
index 6342a28f4df9..cb3fce3732c7 100644
--- a/Include/internal/pycore_runtime_init.h
+++ b/Include/internal/pycore_runtime_init.h
@@ -9,6 +9,7 @@ extern "C" {
 #endif
 
 #include "pycore_object.h"
+#include "pycore_parser.h"
 #include "pycore_pymem_init.h"
 #include "pycore_obmalloc_init.h"
 
@@ -32,6 +33,7 @@ extern "C" {
               until _PyInterpreterState_Enable() is called. */ \
             .next_id = -1, \
         }, \
+        .parser = _parser_runtime_state_INIT, \
         .imports = { \
             .lock = { \
                 .mutex = NULL, \
diff --git a/Parser/action_helpers.c b/Parser/action_helpers.c
index f12dad095aca..46390966892d 100644
--- a/Parser/action_helpers.c
+++ b/Parser/action_helpers.c
@@ -2,30 +2,12 @@
 
 #include "pegen.h"
 #include "string_parser.h"
-
-static PyObject *
-_create_dummy_identifier(Parser *p)
-{
-    return _PyPegen_new_identifier(p, "");
-}
+#include "pycore_runtime.h"         // _PyRuntime
 
 void *
 _PyPegen_dummy_name(Parser *p, ...)
 {
-    // XXX This leaks memory from the initial arena.
-    // Use a statically allocated variable instead of a pointer?
-    static void *cache = NULL;
-
-    if (cache != NULL) {
-        return cache;
-    }
-
-    PyObject *id = _create_dummy_identifier(p);
-    if (!id) {
-        return NULL;
-    }
-    cache = _PyAST_Name(id, Load, 1, 0, 1, 0, p->arena);
-    return cache;
+    return &_PyRuntime.parser.dummy_name;
 }
 
 /* Creates a single-element asdl_seq* that contains a */
diff --git a/Tools/c-analyzer/cpython/ignored.tsv b/Tools/c-analyzer/cpython/ignored.tsv
index c71fc0d95821..02531692a884 100644
--- a/Tools/c-analyzer/cpython/ignored.tsv
+++ b/Tools/c-analyzer/cpython/ignored.tsv
@@ -50,10 +50,6 @@ Python/getversion.c	-	version	-
 Python/bootstrap_hash.c	-	_Py_HashSecret_Initialized	-
 Python/pyhash.c	-	_Py_HashSecret	-
 
-## internal state - set lazily (*after* first init)
-# XXX Move to _PyRuntimeState (i.e. tie to init/fini cycle)?
-Parser/action_helpers.c	_PyPegen_dummy_name	cache	-
-
 
 ##################################
 ## state tied to Py_Main()



More information about the Python-checkins mailing list