Skip to content

Commit b4ae25b

Browse files
committed
Alternative approach
1 parent e6f1adf commit b4ae25b

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

Include/internal/pycore_symtable.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ typedef struct _symtable_entry {
8989
PyObject_HEAD
9090
PyObject *ste_id; /* int: key in ste_table->st_blocks */
9191
PyObject *ste_symbols; /* dict: variable names to flags */
92-
PyObject *ste_name; /* string: name of current block */
93-
PyObject *ste_function_name; /* string or NULL: for annotation blocks: name of the corresponding functions */
92+
PyObject *ste_name; /* string: name of current block (for annotation blocks, may be the name of the corresponding function instead) */
9493
PyObject *ste_varnames; /* list of function parameters */
9594
PyObject *ste_children; /* list of child blocks */
9695
PyObject *ste_directives;/* locations of global and nonlocal statements */

Python/compile.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ compiler_set_qualname(compiler *c)
231231
struct compiler_unit *u = c->u;
232232
PyObject *name, *base;
233233

234+
PyObject *u_name, *function_name = NULL;
235+
if (u->u_scope_type == COMPILE_SCOPE_ANNOTATIONS) {
236+
u_name = &_Py_ID(__annotate__);
237+
_Py_DECLARE_STR(empty, "");
238+
if (u->u_ste->ste_name != &_Py_STR(empty)) {
239+
function_name = u->u_ste->ste_name;
240+
}
241+
}
242+
else {
243+
u_name = u->u_metadata.u_name;
244+
}
245+
234246
base = NULL;
235247
stack_size = PyList_GET_SIZE(c->c_stack);
236248
assert(stack_size >= 1);
@@ -248,7 +260,7 @@ compiler_set_qualname(compiler *c)
248260
if (stack_size == 2) {
249261
// If we're immediately within the module, we can skip
250262
// the rest and just set the qualname to be the same as name.
251-
u->u_metadata.u_qualname = Py_NewRef(u->u_metadata.u_name);
263+
u->u_metadata.u_qualname = Py_NewRef(u_name);
252264
return SUCCESS;
253265
}
254266
capsule = PyList_GET_ITEM(c->c_stack, stack_size - 2);
@@ -260,7 +272,7 @@ compiler_set_qualname(compiler *c)
260272
|| u->u_scope_type == COMPILE_SCOPE_ASYNC_FUNCTION
261273
|| u->u_scope_type == COMPILE_SCOPE_CLASS) {
262274
assert(u->u_metadata.u_name);
263-
mangled = _Py_Mangle(parent->u_private, u->u_metadata.u_name);
275+
mangled = _Py_Mangle(parent->u_private, u_name);
264276
if (!mangled) {
265277
return ERROR;
266278
}
@@ -289,19 +301,17 @@ compiler_set_qualname(compiler *c)
289301
base = Py_NewRef(parent->u_metadata.u_qualname);
290302
}
291303
}
292-
if (u->u_ste->ste_function_name != NULL) {
304+
if (function_name != NULL) {
293305
PyObject *tmp = base;
294-
base = PyUnicode_FromFormat("%U.%U",
295-
base,
296-
u->u_ste->ste_function_name);
306+
base = PyUnicode_FromFormat("%U.%U", base, function_name);
297307
Py_DECREF(tmp);
298308
if (base == NULL) {
299309
return ERROR;
300310
}
301311
}
302312
}
303-
else if (u->u_ste->ste_function_name != NULL) {
304-
base = Py_NewRef(u->u_ste->ste_function_name);
313+
else if (function_name != NULL) {
314+
base = Py_NewRef(function_name);
305315
}
306316

307317
if (base != NULL) {
@@ -310,13 +320,13 @@ compiler_set_qualname(compiler *c)
310320
if (name == NULL) {
311321
return ERROR;
312322
}
313-
PyUnicode_Append(&name, u->u_metadata.u_name);
323+
PyUnicode_Append(&name, u_name);
314324
if (name == NULL) {
315325
return ERROR;
316326
}
317327
}
318328
else {
319-
name = Py_NewRef(u->u_metadata.u_name);
329+
name = Py_NewRef(u_name);
320330
}
321331
u->u_metadata.u_qualname = name;
322332

@@ -608,7 +618,12 @@ _PyCompile_EnterScope(compiler *c, identifier name, int scope_type,
608618
compiler_unit_free(u);
609619
return ERROR;
610620
}
611-
u->u_metadata.u_name = Py_NewRef(name);
621+
if (u->u_ste->ste_type == AnnotationBlock) {
622+
u->u_metadata.u_name = Py_NewRef(&_Py_ID(__annotate__));
623+
}
624+
else {
625+
u->u_metadata.u_name = Py_NewRef(name);
626+
}
612627
u->u_metadata.u_varnames = list2dict(u->u_ste->ste_varnames);
613628
if (!u->u_metadata.u_varnames) {
614629
compiler_unit_free(u);

Python/symtable.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block,
108108
ste->ste_id = k; /* ste owns reference to k */
109109

110110
ste->ste_name = Py_NewRef(name);
111-
ste->ste_function_name = NULL;
112111

113112
ste->ste_symbols = NULL;
114113
ste->ste_varnames = NULL;
@@ -186,7 +185,6 @@ ste_dealloc(PyObject *op)
186185
ste->ste_table = NULL;
187186
Py_XDECREF(ste->ste_id);
188187
Py_XDECREF(ste->ste_name);
189-
Py_XDECREF(ste->ste_function_name);
190188
Py_XDECREF(ste->ste_symbols);
191189
Py_XDECREF(ste->ste_varnames);
192190
Py_XDECREF(ste->ste_children);
@@ -198,9 +196,17 @@ ste_dealloc(PyObject *op)
198196

199197
#define OFF(x) offsetof(PySTEntryObject, x)
200198

199+
static PyObject *
200+
ste_get_name(PySTEntryObject *ste, void *context)
201+
{
202+
if (ste->ste_type == AnnotationBlock) {
203+
return Py_NewRef(&_Py_ID(__annotate__));
204+
}
205+
return Py_NewRef(ste->ste_name);
206+
}
207+
201208
static PyMemberDef ste_memberlist[] = {
202209
{"id", _Py_T_OBJECT, OFF(ste_id), Py_READONLY},
203-
{"name", _Py_T_OBJECT, OFF(ste_name), Py_READONLY},
204210
{"symbols", _Py_T_OBJECT, OFF(ste_symbols), Py_READONLY},
205211
{"varnames", _Py_T_OBJECT, OFF(ste_varnames), Py_READONLY},
206212
{"children", _Py_T_OBJECT, OFF(ste_children), Py_READONLY},
@@ -210,6 +216,14 @@ static PyMemberDef ste_memberlist[] = {
210216
{NULL}
211217
};
212218

219+
static PyGetSetDef ste_getsetlist[] = {
220+
{"name",
221+
(getter)ste_get_name, NULL,
222+
NULL,
223+
NULL},
224+
{NULL}
225+
};
226+
213227
PyTypeObject PySTEntry_Type = {
214228
PyVarObject_HEAD_INIT(&PyType_Type, 0)
215229
"symtable entry",
@@ -240,7 +254,7 @@ PyTypeObject PySTEntry_Type = {
240254
0, /* tp_iternext */
241255
0, /* tp_methods */
242256
ste_memberlist, /* tp_members */
243-
0, /* tp_getset */
257+
ste_getsetlist, /* tp_getset */
244258
0, /* tp_base */
245259
0, /* tp_dict */
246260
0, /* tp_descr_get */
@@ -2773,7 +2787,8 @@ symtable_visit_annotation(struct symtable *st, expr_ty annotation, void *key)
27732787
struct _symtable_entry *parent_ste = st->st_cur;
27742788
if (parent_ste->ste_annotation_block == NULL) {
27752789
_Py_block_ty current_type = parent_ste->ste_type;
2776-
if (!symtable_enter_block(st, &_Py_ID(__annotate__), AnnotationBlock,
2790+
_Py_DECLARE_STR(empty, "");
2791+
if (!symtable_enter_block(st, &_Py_STR(empty), AnnotationBlock,
27772792
key, LOCATION(annotation))) {
27782793
return 0;
27792794
}
@@ -2828,11 +2843,10 @@ symtable_visit_annotations(struct symtable *st, stmt_ty o, arguments_ty a, expr_
28282843
{
28292844
int is_in_class = st->st_cur->ste_can_see_class_scope;
28302845
_Py_block_ty current_type = st->st_cur->ste_type;
2831-
if (!symtable_enter_block(st, &_Py_ID(__annotate__), AnnotationBlock,
2846+
if (!symtable_enter_block(st, function_ste->ste_name, AnnotationBlock,
28322847
(void *)a, LOCATION(o))) {
28332848
return 0;
28342849
}
2835-
Py_XSETREF(st->st_cur->ste_function_name, Py_NewRef(function_ste->ste_name));
28362850
if (is_in_class || current_type == ClassBlock) {
28372851
st->st_cur->ste_can_see_class_scope = 1;
28382852
if (!symtable_add_def(st, &_Py_ID(__classdict__), USE, LOCATION(o))) {

0 commit comments

Comments
 (0)