Skip to content

Commit 3a50dbc

Browse files
committed
Revert "Alternative approach"
This reverts commit b4ae25b.
1 parent b4ae25b commit 3a50dbc

File tree

3 files changed

+20
-48
lines changed

3 files changed

+20
-48
lines changed

Include/internal/pycore_symtable.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ 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 (for annotation blocks, may be the name of the corresponding function instead) */
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 */
9394
PyObject *ste_varnames; /* list of function parameters */
9495
PyObject *ste_children; /* list of child blocks */
9596
PyObject *ste_directives;/* locations of global and nonlocal statements */

Python/compile.c

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,6 @@ 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-
246234
base = NULL;
247235
stack_size = PyList_GET_SIZE(c->c_stack);
248236
assert(stack_size >= 1);
@@ -260,7 +248,7 @@ compiler_set_qualname(compiler *c)
260248
if (stack_size == 2) {
261249
// If we're immediately within the module, we can skip
262250
// the rest and just set the qualname to be the same as name.
263-
u->u_metadata.u_qualname = Py_NewRef(u_name);
251+
u->u_metadata.u_qualname = Py_NewRef(u->u_metadata.u_name);
264252
return SUCCESS;
265253
}
266254
capsule = PyList_GET_ITEM(c->c_stack, stack_size - 2);
@@ -272,7 +260,7 @@ compiler_set_qualname(compiler *c)
272260
|| u->u_scope_type == COMPILE_SCOPE_ASYNC_FUNCTION
273261
|| u->u_scope_type == COMPILE_SCOPE_CLASS) {
274262
assert(u->u_metadata.u_name);
275-
mangled = _Py_Mangle(parent->u_private, u_name);
263+
mangled = _Py_Mangle(parent->u_private, u->u_metadata.u_name);
276264
if (!mangled) {
277265
return ERROR;
278266
}
@@ -301,17 +289,19 @@ compiler_set_qualname(compiler *c)
301289
base = Py_NewRef(parent->u_metadata.u_qualname);
302290
}
303291
}
304-
if (function_name != NULL) {
292+
if (u->u_ste->ste_function_name != NULL) {
305293
PyObject *tmp = base;
306-
base = PyUnicode_FromFormat("%U.%U", base, function_name);
294+
base = PyUnicode_FromFormat("%U.%U",
295+
base,
296+
u->u_ste->ste_function_name);
307297
Py_DECREF(tmp);
308298
if (base == NULL) {
309299
return ERROR;
310300
}
311301
}
312302
}
313-
else if (function_name != NULL) {
314-
base = Py_NewRef(function_name);
303+
else if (u->u_ste->ste_function_name != NULL) {
304+
base = Py_NewRef(u->u_ste->ste_function_name);
315305
}
316306

317307
if (base != NULL) {
@@ -320,13 +310,13 @@ compiler_set_qualname(compiler *c)
320310
if (name == NULL) {
321311
return ERROR;
322312
}
323-
PyUnicode_Append(&name, u_name);
313+
PyUnicode_Append(&name, u->u_metadata.u_name);
324314
if (name == NULL) {
325315
return ERROR;
326316
}
327317
}
328318
else {
329-
name = Py_NewRef(u_name);
319+
name = Py_NewRef(u->u_metadata.u_name);
330320
}
331321
u->u_metadata.u_qualname = name;
332322

@@ -618,12 +608,7 @@ _PyCompile_EnterScope(compiler *c, identifier name, int scope_type,
618608
compiler_unit_free(u);
619609
return ERROR;
620610
}
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-
}
611+
u->u_metadata.u_name = Py_NewRef(name);
627612
u->u_metadata.u_varnames = list2dict(u->u_ste->ste_varnames);
628613
if (!u->u_metadata.u_varnames) {
629614
compiler_unit_free(u);

Python/symtable.c

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ 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;
111112

112113
ste->ste_symbols = NULL;
113114
ste->ste_varnames = NULL;
@@ -185,6 +186,7 @@ ste_dealloc(PyObject *op)
185186
ste->ste_table = NULL;
186187
Py_XDECREF(ste->ste_id);
187188
Py_XDECREF(ste->ste_name);
189+
Py_XDECREF(ste->ste_function_name);
188190
Py_XDECREF(ste->ste_symbols);
189191
Py_XDECREF(ste->ste_varnames);
190192
Py_XDECREF(ste->ste_children);
@@ -196,17 +198,9 @@ ste_dealloc(PyObject *op)
196198

197199
#define OFF(x) offsetof(PySTEntryObject, x)
198200

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-
208201
static PyMemberDef ste_memberlist[] = {
209202
{"id", _Py_T_OBJECT, OFF(ste_id), Py_READONLY},
203+
{"name", _Py_T_OBJECT, OFF(ste_name), Py_READONLY},
210204
{"symbols", _Py_T_OBJECT, OFF(ste_symbols), Py_READONLY},
211205
{"varnames", _Py_T_OBJECT, OFF(ste_varnames), Py_READONLY},
212206
{"children", _Py_T_OBJECT, OFF(ste_children), Py_READONLY},
@@ -216,14 +210,6 @@ static PyMemberDef ste_memberlist[] = {
216210
{NULL}
217211
};
218212

219-
static PyGetSetDef ste_getsetlist[] = {
220-
{"name",
221-
(getter)ste_get_name, NULL,
222-
NULL,
223-
NULL},
224-
{NULL}
225-
};
226-
227213
PyTypeObject PySTEntry_Type = {
228214
PyVarObject_HEAD_INIT(&PyType_Type, 0)
229215
"symtable entry",
@@ -254,7 +240,7 @@ PyTypeObject PySTEntry_Type = {
254240
0, /* tp_iternext */
255241
0, /* tp_methods */
256242
ste_memberlist, /* tp_members */
257-
ste_getsetlist, /* tp_getset */
243+
0, /* tp_getset */
258244
0, /* tp_base */
259245
0, /* tp_dict */
260246
0, /* tp_descr_get */
@@ -2787,8 +2773,7 @@ symtable_visit_annotation(struct symtable *st, expr_ty annotation, void *key)
27872773
struct _symtable_entry *parent_ste = st->st_cur;
27882774
if (parent_ste->ste_annotation_block == NULL) {
27892775
_Py_block_ty current_type = parent_ste->ste_type;
2790-
_Py_DECLARE_STR(empty, "");
2791-
if (!symtable_enter_block(st, &_Py_STR(empty), AnnotationBlock,
2776+
if (!symtable_enter_block(st, &_Py_ID(__annotate__), AnnotationBlock,
27922777
key, LOCATION(annotation))) {
27932778
return 0;
27942779
}
@@ -2843,10 +2828,11 @@ symtable_visit_annotations(struct symtable *st, stmt_ty o, arguments_ty a, expr_
28432828
{
28442829
int is_in_class = st->st_cur->ste_can_see_class_scope;
28452830
_Py_block_ty current_type = st->st_cur->ste_type;
2846-
if (!symtable_enter_block(st, function_ste->ste_name, AnnotationBlock,
2831+
if (!symtable_enter_block(st, &_Py_ID(__annotate__), AnnotationBlock,
28472832
(void *)a, LOCATION(o))) {
28482833
return 0;
28492834
}
2835+
Py_XSETREF(st->st_cur->ste_function_name, Py_NewRef(function_ste->ste_name));
28502836
if (is_in_class || current_type == ClassBlock) {
28512837
st->st_cur->ste_can_see_class_scope = 1;
28522838
if (!symtable_add_def(st, &_Py_ID(__classdict__), USE, LOCATION(o))) {

0 commit comments

Comments
 (0)