Skip to content

Commit 12360aa

Browse files
bpo-46541: Discover the global strings. (gh-31346)
Instead of manually enumerating the global strings in generate_global_objects.py, we extrapolate the list from usage of _Py_ID() and _Py_STR() in the source files. This is partly inspired by gh-31261. https://bugs.python.org/issue46541
1 parent 278fdd3 commit 12360aa

9 files changed

+101
-272
lines changed

Include/internal/pycore_global_strings.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,19 @@ extern "C" {
2828
/* The following is auto-generated by Tools/scripts/generate_global_objects.py. */
2929
struct _Py_global_strings {
3030
struct {
31-
STRUCT_FOR_STR(empty, "")
32-
STRUCT_FOR_STR(dot, ".")
33-
STRUCT_FOR_STR(comma_sep, ", ")
34-
STRUCT_FOR_STR(percent, "%")
35-
STRUCT_FOR_STR(dbl_percent, "%%")
36-
37-
// "anonymous" labels
3831
STRUCT_FOR_STR(anon_dictcomp, "<dictcomp>")
3932
STRUCT_FOR_STR(anon_genexpr, "<genexpr>")
4033
STRUCT_FOR_STR(anon_lambda, "<lambda>")
4134
STRUCT_FOR_STR(anon_listcomp, "<listcomp>")
4235
STRUCT_FOR_STR(anon_module, "<module>")
4336
STRUCT_FOR_STR(anon_setcomp, "<setcomp>")
4437
STRUCT_FOR_STR(anon_string, "<string>")
38+
STRUCT_FOR_STR(comma_sep, ", ")
39+
STRUCT_FOR_STR(dbl_percent, "%%")
40+
STRUCT_FOR_STR(dot, ".")
4541
STRUCT_FOR_STR(dot_locals, ".<locals>")
42+
STRUCT_FOR_STR(empty, "")
43+
STRUCT_FOR_STR(percent, "%")
4644
} literals;
4745

4846
struct {
@@ -330,6 +328,7 @@ struct _Py_global_strings {
330328
#define _Py_STR(NAME) \
331329
(_Py_SINGLETON(strings.literals._ ## NAME._ascii.ob_base))
332330

331+
#define _Py_DECLARE_STR(name, str)
333332

334333
#ifdef __cplusplus
335334
}

Include/internal/pycore_runtime_init.h

+5-6
Original file line numberDiff line numberDiff line change
@@ -644,20 +644,19 @@ extern "C" {
644644
\
645645
.strings = { \
646646
.literals = { \
647-
INIT_STR(empty, ""), \
648-
INIT_STR(dot, "."), \
649-
INIT_STR(comma_sep, ", "), \
650-
INIT_STR(percent, "%"), \
651-
INIT_STR(dbl_percent, "%%"), \
652-
\
653647
INIT_STR(anon_dictcomp, "<dictcomp>"), \
654648
INIT_STR(anon_genexpr, "<genexpr>"), \
655649
INIT_STR(anon_lambda, "<lambda>"), \
656650
INIT_STR(anon_listcomp, "<listcomp>"), \
657651
INIT_STR(anon_module, "<module>"), \
658652
INIT_STR(anon_setcomp, "<setcomp>"), \
659653
INIT_STR(anon_string, "<string>"), \
654+
INIT_STR(comma_sep, ", "), \
655+
INIT_STR(dbl_percent, "%%"), \
656+
INIT_STR(dot, "."), \
660657
INIT_STR(dot_locals, ".<locals>"), \
658+
INIT_STR(empty, ""), \
659+
INIT_STR(percent, "%"), \
661660
}, \
662661
.identifiers = { \
663662
INIT_ID(Py_Repr), \

Objects/typeobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -4546,6 +4546,7 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
45464546
Py_DECREF(sorted_methods);
45474547
return NULL;
45484548
}
4549+
_Py_DECLARE_STR(comma_sep, ", ");
45494550
joined = PyUnicode_Join(&_Py_STR(comma_sep), sorted_methods);
45504551
method_count = PyObject_Length(sorted_methods);
45514552
Py_DECREF(sorted_methods);

Objects/weakrefobject.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,12 @@ proxy_checkref(PyWeakReference *proxy)
458458
return res; \
459459
}
460460

461-
#define WRAP_METHOD(method, special) \
461+
#define WRAP_METHOD(method, SPECIAL) \
462462
static PyObject * \
463463
method(PyObject *proxy, PyObject *Py_UNUSED(ignored)) { \
464464
UNWRAP(proxy); \
465465
Py_INCREF(proxy); \
466-
PyObject* res = PyObject_CallMethodNoArgs(proxy, &_Py_ID(special)); \
466+
PyObject* res = PyObject_CallMethodNoArgs(proxy, &_Py_ID(SPECIAL)); \
467467
Py_DECREF(proxy); \
468468
return res; \
469469
}

Python/_warnings.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ check_matched(PyInterpreterState *interp, PyObject *obj, PyObject *arg)
186186
return rc;
187187
}
188188

189-
#define GET_WARNINGS_ATTR(interp, attr, try_import) \
190-
get_warnings_attr(interp, &_Py_ID(attr), try_import)
189+
#define GET_WARNINGS_ATTR(interp, ATTR, try_import) \
190+
get_warnings_attr(interp, &_Py_ID(ATTR), try_import)
191191

192192
/*
193193
Returns a new reference.

Python/ast_opt.c

+2
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ parse_literal(PyObject *fmt, Py_ssize_t *ppos, PyArena *arena)
268268
PyObject *str = PyUnicode_Substring(fmt, start, pos);
269269
/* str = str.replace('%%', '%') */
270270
if (str && has_percents) {
271+
_Py_DECLARE_STR(percent, "%");
272+
_Py_DECLARE_STR(dbl_percent, "%%");
271273
Py_SETREF(str, PyUnicode_Replace(str, &_Py_STR(dbl_percent),
272274
&_Py_STR(percent), -1));
273275
}

Python/compile.c

+7
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ compiler_set_qualname(struct compiler *c)
667667
|| parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
668668
|| parent->u_scope_type == COMPILER_SCOPE_LAMBDA)
669669
{
670+
_Py_DECLARE_STR(dot_locals, ".<locals>");
670671
base = PyUnicode_Concat(parent->u_qualname,
671672
&_Py_STR(dot_locals));
672673
if (base == NULL)
@@ -2022,6 +2023,7 @@ compiler_mod(struct compiler *c, mod_ty mod)
20222023
{
20232024
PyCodeObject *co;
20242025
int addNone = 1;
2026+
_Py_DECLARE_STR(anon_module, "<module>");
20252027
if (!compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE,
20262028
mod, 1)) {
20272029
return NULL;
@@ -2876,6 +2878,7 @@ compiler_lambda(struct compiler *c, expr_ty e)
28762878
return 0;
28772879
}
28782880

2881+
_Py_DECLARE_STR(anon_lambda, "<lambda>");
28792882
if (!compiler_enter_scope(c, &_Py_STR(anon_lambda), COMPILER_SCOPE_LAMBDA,
28802883
(void *)e, e->lineno)) {
28812884
return 0;
@@ -5347,6 +5350,7 @@ static int
53475350
compiler_genexp(struct compiler *c, expr_ty e)
53485351
{
53495352
assert(e->kind == GeneratorExp_kind);
5353+
_Py_DECLARE_STR(anon_genexpr, "<genexpr>");
53505354
return compiler_comprehension(c, e, COMP_GENEXP, &_Py_STR(anon_genexpr),
53515355
e->v.GeneratorExp.generators,
53525356
e->v.GeneratorExp.elt, NULL);
@@ -5356,6 +5360,7 @@ static int
53565360
compiler_listcomp(struct compiler *c, expr_ty e)
53575361
{
53585362
assert(e->kind == ListComp_kind);
5363+
_Py_DECLARE_STR(anon_listcomp, "<listcomp>");
53595364
return compiler_comprehension(c, e, COMP_LISTCOMP, &_Py_STR(anon_listcomp),
53605365
e->v.ListComp.generators,
53615366
e->v.ListComp.elt, NULL);
@@ -5365,6 +5370,7 @@ static int
53655370
compiler_setcomp(struct compiler *c, expr_ty e)
53665371
{
53675372
assert(e->kind == SetComp_kind);
5373+
_Py_DECLARE_STR(anon_setcomp, "<setcomp>");
53685374
return compiler_comprehension(c, e, COMP_SETCOMP, &_Py_STR(anon_setcomp),
53695375
e->v.SetComp.generators,
53705376
e->v.SetComp.elt, NULL);
@@ -5375,6 +5381,7 @@ static int
53755381
compiler_dictcomp(struct compiler *c, expr_ty e)
53765382
{
53775383
assert(e->kind == DictComp_kind);
5384+
_Py_DECLARE_STR(anon_dictcomp, "<dictcomp>");
53785385
return compiler_comprehension(c, e, COMP_DICTCOMP, &_Py_STR(anon_dictcomp),
53795386
e->v.DictComp.generators,
53805387
e->v.DictComp.key, e->v.DictComp.value);

Python/pythonrun.c

+2
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
515515
goto finally;
516516
if (v == Py_None) {
517517
Py_DECREF(v);
518+
_Py_DECLARE_STR(anon_string, "<string>");
518519
*filename = &_Py_STR(anon_string);
519520
Py_INCREF(*filename);
520521
}
@@ -1562,6 +1563,7 @@ PyRun_StringFlags(const char *str, int start, PyObject *globals,
15621563
if (arena == NULL)
15631564
return NULL;
15641565

1566+
_Py_DECLARE_STR(anon_string, "<string>");
15651567
mod = _PyParser_ASTFromString(
15661568
str, &_Py_STR(anon_string), start, flags, arena);
15671569

0 commit comments

Comments
 (0)