Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "Alternative approach"
This reverts commit b4ae25b.
  • Loading branch information
JelleZijlstra committed Aug 16, 2025
commit 3a50dbc377ff10cbfc291e7591c3b135cb2d68ed
3 changes: 2 additions & 1 deletion Include/internal/pycore_symtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ typedef struct _symtable_entry {
PyObject_HEAD
PyObject *ste_id; /* int: key in ste_table->st_blocks */
PyObject *ste_symbols; /* dict: variable names to flags */
PyObject *ste_name; /* string: name of current block (for annotation blocks, may be the name of the corresponding function instead) */
PyObject *ste_name; /* string: name of current block */
PyObject *ste_function_name; /* string or NULL: for annotation blocks: name of the corresponding functions */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can’t use ste_name for this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible but I don't know if it's better. I just pushed a version that does this.

PyObject *ste_varnames; /* list of function parameters */
PyObject *ste_children; /* list of child blocks */
PyObject *ste_directives;/* locations of global and nonlocal statements */
Expand Down
37 changes: 11 additions & 26 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,6 @@ compiler_set_qualname(compiler *c)
struct compiler_unit *u = c->u;
PyObject *name, *base;

PyObject *u_name, *function_name = NULL;
if (u->u_scope_type == COMPILE_SCOPE_ANNOTATIONS) {
u_name = &_Py_ID(__annotate__);
_Py_DECLARE_STR(empty, "");
if (u->u_ste->ste_name != &_Py_STR(empty)) {
function_name = u->u_ste->ste_name;
}
}
else {
u_name = u->u_metadata.u_name;
}

base = NULL;
stack_size = PyList_GET_SIZE(c->c_stack);
assert(stack_size >= 1);
Expand All @@ -260,7 +248,7 @@ compiler_set_qualname(compiler *c)
if (stack_size == 2) {
// If we're immediately within the module, we can skip
// the rest and just set the qualname to be the same as name.
u->u_metadata.u_qualname = Py_NewRef(u_name);
u->u_metadata.u_qualname = Py_NewRef(u->u_metadata.u_name);
return SUCCESS;
}
capsule = PyList_GET_ITEM(c->c_stack, stack_size - 2);
Expand All @@ -272,7 +260,7 @@ compiler_set_qualname(compiler *c)
|| u->u_scope_type == COMPILE_SCOPE_ASYNC_FUNCTION
|| u->u_scope_type == COMPILE_SCOPE_CLASS) {
assert(u->u_metadata.u_name);
mangled = _Py_Mangle(parent->u_private, u_name);
mangled = _Py_Mangle(parent->u_private, u->u_metadata.u_name);
if (!mangled) {
return ERROR;
}
Expand Down Expand Up @@ -301,17 +289,19 @@ compiler_set_qualname(compiler *c)
base = Py_NewRef(parent->u_metadata.u_qualname);
}
}
if (function_name != NULL) {
if (u->u_ste->ste_function_name != NULL) {
PyObject *tmp = base;
base = PyUnicode_FromFormat("%U.%U", base, function_name);
base = PyUnicode_FromFormat("%U.%U",
base,
u->u_ste->ste_function_name);
Py_DECREF(tmp);
if (base == NULL) {
return ERROR;
}
}
}
else if (function_name != NULL) {
base = Py_NewRef(function_name);
else if (u->u_ste->ste_function_name != NULL) {
base = Py_NewRef(u->u_ste->ste_function_name);
}

if (base != NULL) {
Expand All @@ -320,13 +310,13 @@ compiler_set_qualname(compiler *c)
if (name == NULL) {
return ERROR;
}
PyUnicode_Append(&name, u_name);
PyUnicode_Append(&name, u->u_metadata.u_name);
if (name == NULL) {
return ERROR;
}
}
else {
name = Py_NewRef(u_name);
name = Py_NewRef(u->u_metadata.u_name);
}
u->u_metadata.u_qualname = name;

Expand Down Expand Up @@ -618,12 +608,7 @@ _PyCompile_EnterScope(compiler *c, identifier name, int scope_type,
compiler_unit_free(u);
return ERROR;
}
if (u->u_ste->ste_type == AnnotationBlock) {
u->u_metadata.u_name = Py_NewRef(&_Py_ID(__annotate__));
}
else {
u->u_metadata.u_name = Py_NewRef(name);
}
u->u_metadata.u_name = Py_NewRef(name);
u->u_metadata.u_varnames = list2dict(u->u_ste->ste_varnames);
if (!u->u_metadata.u_varnames) {
compiler_unit_free(u);
Expand Down
28 changes: 7 additions & 21 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block,
ste->ste_id = k; /* ste owns reference to k */

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

ste->ste_symbols = NULL;
ste->ste_varnames = NULL;
Expand Down Expand Up @@ -185,6 +186,7 @@ ste_dealloc(PyObject *op)
ste->ste_table = NULL;
Py_XDECREF(ste->ste_id);
Py_XDECREF(ste->ste_name);
Py_XDECREF(ste->ste_function_name);
Py_XDECREF(ste->ste_symbols);
Py_XDECREF(ste->ste_varnames);
Py_XDECREF(ste->ste_children);
Expand All @@ -196,17 +198,9 @@ ste_dealloc(PyObject *op)

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

static PyObject *
ste_get_name(PySTEntryObject *ste, void *context)
{
if (ste->ste_type == AnnotationBlock) {
return Py_NewRef(&_Py_ID(__annotate__));
}
return Py_NewRef(ste->ste_name);
}

static PyMemberDef ste_memberlist[] = {
{"id", _Py_T_OBJECT, OFF(ste_id), Py_READONLY},
{"name", _Py_T_OBJECT, OFF(ste_name), Py_READONLY},
{"symbols", _Py_T_OBJECT, OFF(ste_symbols), Py_READONLY},
{"varnames", _Py_T_OBJECT, OFF(ste_varnames), Py_READONLY},
{"children", _Py_T_OBJECT, OFF(ste_children), Py_READONLY},
Expand All @@ -216,14 +210,6 @@ static PyMemberDef ste_memberlist[] = {
{NULL}
};

static PyGetSetDef ste_getsetlist[] = {
{"name",
(getter)ste_get_name, NULL,
NULL,
NULL},
{NULL}
};

PyTypeObject PySTEntry_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"symtable entry",
Expand Down Expand Up @@ -254,7 +240,7 @@ PyTypeObject PySTEntry_Type = {
0, /* tp_iternext */
0, /* tp_methods */
ste_memberlist, /* tp_members */
ste_getsetlist, /* tp_getset */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
Expand Down Expand Up @@ -2787,8 +2773,7 @@ symtable_visit_annotation(struct symtable *st, expr_ty annotation, void *key)
struct _symtable_entry *parent_ste = st->st_cur;
if (parent_ste->ste_annotation_block == NULL) {
_Py_block_ty current_type = parent_ste->ste_type;
_Py_DECLARE_STR(empty, "");
if (!symtable_enter_block(st, &_Py_STR(empty), AnnotationBlock,
if (!symtable_enter_block(st, &_Py_ID(__annotate__), AnnotationBlock,
key, LOCATION(annotation))) {
return 0;
}
Expand Down Expand Up @@ -2843,10 +2828,11 @@ symtable_visit_annotations(struct symtable *st, stmt_ty o, arguments_ty a, expr_
{
int is_in_class = st->st_cur->ste_can_see_class_scope;
_Py_block_ty current_type = st->st_cur->ste_type;
if (!symtable_enter_block(st, function_ste->ste_name, AnnotationBlock,
if (!symtable_enter_block(st, &_Py_ID(__annotate__), AnnotationBlock,
(void *)a, LOCATION(o))) {
return 0;
}
Py_XSETREF(st->st_cur->ste_function_name, Py_NewRef(function_ste->ste_name));
if (is_in_class || current_type == ClassBlock) {
st->st_cur->ste_can_see_class_scope = 1;
if (!symtable_add_def(st, &_Py_ID(__classdict__), USE, LOCATION(o))) {
Expand Down
Loading