Skip to content

Commit 432fa46

Browse files
Move the cached parser dummy name to _PyRuntimeState as a static object.
1 parent b430399 commit 432fa46

File tree

5 files changed

+22
-24
lines changed

5 files changed

+22
-24
lines changed

Include/internal/pycore_global_objects.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_ast.h" // struct _expr
1112
#include "pycore_gc.h" // PyGC_Head
1213
#include "pycore_global_strings.h" // struct _Py_global_strings
1314
#include "pycore_hamt.h" // PyHamtNode_Bitmap
@@ -60,6 +61,8 @@ struct _Py_static_objects {
6061
_PyGC_Head_UNUSED _hamt_bitmap_node_empty_gc_not_used;
6162
PyHamtNode_Bitmap hamt_bitmap_node_empty;
6263
_PyContextTokenMissing context_token_missing;
64+
65+
struct _expr parser_dummy_name;
6366
} singletons;
6467
};
6568

Include/internal/pycore_parser.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ extern "C" {
99
#endif
1010

1111

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

1416

@@ -25,6 +27,18 @@ struct _parser_runtime_state {
2527
};
2628

2729

30+
_Py_DECLARE_STR(empty, "")
31+
32+
#define _Py_parser_dummy_name_INIT \
33+
{ \
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+
}
2842

2943
extern struct _mod* _PyParser_ASTFromString(
3044
const char *str,

Include/internal/pycore_runtime_init.h

Lines changed: 2 additions & 0 deletions
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

@@ -90,6 +91,7 @@ extern "C" {
9091
.context_token_missing = { \
9192
.ob_base = _PyObject_IMMORTAL_INIT(&_PyContextTokenMissing_Type), \
9293
}, \
94+
.parser_dummy_name = _Py_parser_dummy_name_INIT, \
9395
}, \
9496
}, \
9597
._main_interpreter = _PyInterpreterState_INIT, \

Parser/action_helpers.c

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,13 @@
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
6+
#include "pycore_global_objects.h" // _Py_SINGLETON()
117

128
void *
139
_PyPegen_dummy_name(Parser *p, ...)
1410
{
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;
11+
return &_Py_SINGLETON(parser_dummy_name);
2912
}
3013

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

Tools/c-analyzer/cpython/ignored.tsv

Lines changed: 0 additions & 4 deletions
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)