Skip to content

Commit

Permalink
Move the cached parser dummy name to _PyRuntimeState as a static object.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed Dec 15, 2022
1 parent b430399 commit 432fa46
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
3 changes: 3 additions & 0 deletions Include/internal/pycore_global_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
};

Expand Down
14 changes: 14 additions & 0 deletions Include/internal/pycore_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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, \
Expand Down
23 changes: 3 additions & 20 deletions Parser/action_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
4 changes: 0 additions & 4 deletions Tools/c-analyzer/cpython/ignored.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 432fa46

Please sign in to comment.