Skip to content

Commit 0415cf8

Browse files
gh-81057: Move the Cached Parser Dummy Name to _PyRuntimeState (#100277)
1 parent 5ee7eb9 commit 0415cf8

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

Include/internal/pycore_parser.h

+16-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ extern "C" {
99
#endif
1010

1111

12+
#include "pycore_ast.h" // struct _expr
13+
#include "pycore_global_strings.h" // _Py_DECLARE_STR()
1214
#include "pycore_pyarena.h" // PyArena
1315

1416

@@ -22,9 +24,22 @@ struct _parser_runtime_state {
2224
#else
2325
int _not_used;
2426
#endif
27+
struct _expr dummy_name;
2528
};
2629

27-
30+
_Py_DECLARE_STR(empty, "")
31+
#define _parser_runtime_state_INIT \
32+
{ \
33+
.dummy_name = { \
34+
.kind = Name_kind, \
35+
.v.Name.id = &_Py_STR(empty), \
36+
.v.Name.ctx = Load, \
37+
.lineno = 1, \
38+
.col_offset = 0, \
39+
.end_lineno = 1, \
40+
.end_col_offset = 0, \
41+
}, \
42+
}
2843

2944
extern struct _mod* _PyParser_ASTFromString(
3045
const char *str,

Include/internal/pycore_runtime_init.h

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern "C" {
99
#endif
1010

1111
#include "pycore_object.h"
12+
#include "pycore_parser.h"
1213
#include "pycore_pymem_init.h"
1314
#include "pycore_obmalloc_init.h"
1415

@@ -32,6 +33,7 @@ extern "C" {
3233
until _PyInterpreterState_Enable() is called. */ \
3334
.next_id = -1, \
3435
}, \
36+
.parser = _parser_runtime_state_INIT, \
3537
.imports = { \
3638
.lock = { \
3739
.mutex = NULL, \

Parser/action_helpers.c

+2-20
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,12 @@
22

33
#include "pegen.h"
44
#include "string_parser.h"
5-
6-
static PyObject *
7-
_create_dummy_identifier(Parser *p)
8-
{
9-
return _PyPegen_new_identifier(p, "");
10-
}
5+
#include "pycore_runtime.h" // _PyRuntime
116

127
void *
138
_PyPegen_dummy_name(Parser *p, ...)
149
{
15-
// XXX This leaks memory from the initial arena.
16-
// Use a statically allocated variable instead of a pointer?
17-
static void *cache = NULL;
18-
19-
if (cache != NULL) {
20-
return cache;
21-
}
22-
23-
PyObject *id = _create_dummy_identifier(p);
24-
if (!id) {
25-
return NULL;
26-
}
27-
cache = _PyAST_Name(id, Load, 1, 0, 1, 0, p->arena);
28-
return cache;
10+
return &_PyRuntime.parser.dummy_name;
2911
}
3012

3113
/* Creates a single-element asdl_seq* that contains a */

Tools/c-analyzer/cpython/ignored.tsv

-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ Python/getversion.c - version -
5050
Python/bootstrap_hash.c - _Py_HashSecret_Initialized -
5151
Python/pyhash.c - _Py_HashSecret -
5252

53-
## internal state - set lazily (*after* first init)
54-
# XXX Move to _PyRuntimeState (i.e. tie to init/fini cycle)?
55-
Parser/action_helpers.c _PyPegen_dummy_name cache -
56-
5753

5854
##################################
5955
## state tied to Py_Main()

0 commit comments

Comments
 (0)