From 432fa4689f81df022c386d3ad640463eda403885 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 15 Dec 2022 12:23:54 -0700 Subject: [PATCH 1/2] Move the cached parser dummy name to _PyRuntimeState as a static object. --- Include/internal/pycore_global_objects.h | 3 +++ Include/internal/pycore_parser.h | 14 ++++++++++++++ Include/internal/pycore_runtime_init.h | 2 ++ Parser/action_helpers.c | 23 +++-------------------- Tools/c-analyzer/cpython/ignored.tsv | 4 ---- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/Include/internal/pycore_global_objects.h b/Include/internal/pycore_global_objects.h index d0461fa7e82e8b..218b1e49d7defe 100644 --- a/Include/internal/pycore_global_objects.h +++ b/Include/internal/pycore_global_objects.h @@ -8,6 +8,7 @@ extern "C" { # error "this header requires Py_BUILD_CORE define" #endif +#include "pycore_ast.h" // struct _expr #include "pycore_gc.h" // PyGC_Head #include "pycore_global_strings.h" // struct _Py_global_strings #include "pycore_hamt.h" // PyHamtNode_Bitmap @@ -60,6 +61,8 @@ struct _Py_static_objects { _PyGC_Head_UNUSED _hamt_bitmap_node_empty_gc_not_used; PyHamtNode_Bitmap hamt_bitmap_node_empty; _PyContextTokenMissing context_token_missing; + + struct _expr parser_dummy_name; } singletons; }; diff --git a/Include/internal/pycore_parser.h b/Include/internal/pycore_parser.h index 2d2b56bd824cb4..1624f38d28bb0f 100644 --- a/Include/internal/pycore_parser.h +++ b/Include/internal/pycore_parser.h @@ -9,6 +9,8 @@ extern "C" { #endif +#include "pycore_ast.h" // Name_kind +#include "pycore_global_strings.h" // _Py_DECLARE_STR() #include "pycore_pyarena.h" // PyArena @@ -25,6 +27,18 @@ struct _parser_runtime_state { }; +_Py_DECLARE_STR(empty, "") + +#define _Py_parser_dummy_name_INIT \ + { \ + .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 6342a28f4df911..52a5a80f8920e2 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" @@ -90,6 +91,7 @@ extern "C" { .context_token_missing = { \ .ob_base = _PyObject_IMMORTAL_INIT(&_PyContextTokenMissing_Type), \ }, \ + .parser_dummy_name = _Py_parser_dummy_name_INIT, \ }, \ }, \ ._main_interpreter = _PyInterpreterState_INIT, \ diff --git a/Parser/action_helpers.c b/Parser/action_helpers.c index f12dad095acaa8..16572703876751 100644 --- a/Parser/action_helpers.c +++ b/Parser/action_helpers.c @@ -2,30 +2,13 @@ #include "pegen.h" #include "string_parser.h" - -static PyObject * -_create_dummy_identifier(Parser *p) -{ - return _PyPegen_new_identifier(p, ""); -} +#include "pycore_runtime.h" // _PyRuntime +#include "pycore_global_objects.h" // _Py_SINGLETON() 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 &_Py_SINGLETON(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 c71fc0d958216c..02531692a88447 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() From 2d0876e7d671a0a1d414e80df5a16239946b2936 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 15 Dec 2022 12:41:13 -0700 Subject: [PATCH 2/2] Move it to _PyRuntime.parser. --- Include/internal/pycore_global_objects.h | 3 --- Include/internal/pycore_parser.h | 23 ++++++++++++----------- Include/internal/pycore_runtime_init.h | 2 +- Parser/action_helpers.c | 3 +-- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/Include/internal/pycore_global_objects.h b/Include/internal/pycore_global_objects.h index 218b1e49d7defe..d0461fa7e82e8b 100644 --- a/Include/internal/pycore_global_objects.h +++ b/Include/internal/pycore_global_objects.h @@ -8,7 +8,6 @@ extern "C" { # error "this header requires Py_BUILD_CORE define" #endif -#include "pycore_ast.h" // struct _expr #include "pycore_gc.h" // PyGC_Head #include "pycore_global_strings.h" // struct _Py_global_strings #include "pycore_hamt.h" // PyHamtNode_Bitmap @@ -61,8 +60,6 @@ struct _Py_static_objects { _PyGC_Head_UNUSED _hamt_bitmap_node_empty_gc_not_used; PyHamtNode_Bitmap hamt_bitmap_node_empty; _PyContextTokenMissing context_token_missing; - - struct _expr parser_dummy_name; } singletons; }; diff --git a/Include/internal/pycore_parser.h b/Include/internal/pycore_parser.h index 1624f38d28bb0f..dd51b92801aebf 100644 --- a/Include/internal/pycore_parser.h +++ b/Include/internal/pycore_parser.h @@ -9,7 +9,7 @@ extern "C" { #endif -#include "pycore_ast.h" // Name_kind +#include "pycore_ast.h" // struct _expr #include "pycore_global_strings.h" // _Py_DECLARE_STR() #include "pycore_pyarena.h" // PyArena @@ -24,20 +24,21 @@ struct _parser_runtime_state { #else int _not_used; #endif + struct _expr dummy_name; }; - _Py_DECLARE_STR(empty, "") - -#define _Py_parser_dummy_name_INIT \ +#define _parser_runtime_state_INIT \ { \ - .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, \ + .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( diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h index 52a5a80f8920e2..cb3fce3732c79b 100644 --- a/Include/internal/pycore_runtime_init.h +++ b/Include/internal/pycore_runtime_init.h @@ -33,6 +33,7 @@ extern "C" { until _PyInterpreterState_Enable() is called. */ \ .next_id = -1, \ }, \ + .parser = _parser_runtime_state_INIT, \ .imports = { \ .lock = { \ .mutex = NULL, \ @@ -91,7 +92,6 @@ extern "C" { .context_token_missing = { \ .ob_base = _PyObject_IMMORTAL_INIT(&_PyContextTokenMissing_Type), \ }, \ - .parser_dummy_name = _Py_parser_dummy_name_INIT, \ }, \ }, \ ._main_interpreter = _PyInterpreterState_INIT, \ diff --git a/Parser/action_helpers.c b/Parser/action_helpers.c index 16572703876751..46390966892d16 100644 --- a/Parser/action_helpers.c +++ b/Parser/action_helpers.c @@ -3,12 +3,11 @@ #include "pegen.h" #include "string_parser.h" #include "pycore_runtime.h" // _PyRuntime -#include "pycore_global_objects.h" // _Py_SINGLETON() void * _PyPegen_dummy_name(Parser *p, ...) { - return &_Py_SINGLETON(parser_dummy_name); + return &_PyRuntime.parser.dummy_name; } /* Creates a single-element asdl_seq* that contains a */