Skip to content

Commit bbeeb46

Browse files
authored
Merge branch 'main' into fix/warnings-helper-lazy-imports
2 parents d50c1fa + 8b082aa commit bbeeb46

4 files changed

Lines changed: 15 additions & 7 deletions

File tree

Include/internal/pycore_compile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ int _PyCompile_EnterScope(struct _PyCompiler *c, identifier name, int scope_type
137137
void *key, int lineno, PyObject *private,
138138
_PyCompile_CodeUnitMetadata *umd);
139139
void _PyCompile_ExitScope(struct _PyCompiler *c);
140+
int _PyCompile_SetQualname(struct _PyCompiler *c);
140141
Py_ssize_t _PyCompile_AddConst(struct _PyCompiler *c, PyObject *o);
141142
_PyInstructionSequence *_PyCompile_InstrSequence(struct _PyCompiler *c);
142143
int _PyCompile_StartAnnotationSetup(struct _PyCompiler *c);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix cleanup on error in ``compiler_set_qualname``. Previously it was called in
2+
``_PyCompile_EnterScope``, after the scope had been entered, and this was not
3+
reversed in case of an error.

Python/codegen.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ codegen_enter_scope(compiler *c, identifier name, int scope_type,
687687
{
688688
RETURN_IF_ERROR(
689689
_PyCompile_EnterScope(c, name, scope_type, key, lineno, private, umd));
690+
RETURN_IF_ERROR_IN_SCOPE(c, _PyCompile_SetQualname(c));
690691
location loc = LOCATION(lineno, lineno, 0, 0);
691692
if (scope_type == COMPILE_SCOPE_MODULE) {
692693
loc.lineno = 0;
@@ -1634,7 +1635,7 @@ codegen_class_body(compiler *c, stmt_ty s, int firstlineno)
16341635
ADDOP_N_IN_SCOPE(c, loc, STORE_DEREF, &_Py_ID(__classdict__), cellvars);
16351636
}
16361637
if (SYMTABLE_ENTRY(c)->ste_has_conditional_annotations) {
1637-
ADDOP_I(c, loc, BUILD_SET, 0);
1638+
ADDOP_I_IN_SCOPE(c, loc, BUILD_SET, 0);
16381639
ADDOP_N_IN_SCOPE(c, loc, STORE_DEREF, &_Py_ID(__conditional_annotations__), cellvars);
16391640
}
16401641
/* compile the body proper */

Python/compile.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,17 @@ _PyCompile_MaybeAddStaticAttributeToClass(compiler *c, expr_ty e)
233233
return SUCCESS;
234234
}
235235

236-
static int
237-
compiler_set_qualname(compiler *c)
236+
int
237+
_PyCompile_SetQualname(compiler *c)
238238
{
239239
Py_ssize_t stack_size;
240240
struct compiler_unit *u = c->u;
241241
PyObject *name, *base;
242242

243+
if (u->u_scope_type == COMPILE_SCOPE_MODULE) {
244+
return SUCCESS;
245+
}
246+
243247
base = NULL;
244248
stack_size = PyList_GET_SIZE(c->c_stack);
245249
assert(stack_size >= 1);
@@ -724,9 +728,6 @@ _PyCompile_EnterScope(compiler *c, identifier name, int scope_type,
724728
u->u_private = Py_XNewRef(private);
725729

726730
c->u = u;
727-
if (scope_type != COMPILE_SCOPE_MODULE) {
728-
RETURN_IF_ERROR(compiler_set_qualname(c));
729-
}
730731
return SUCCESS;
731732
}
732733

@@ -1735,7 +1736,9 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
17351736
finally:
17361737
Py_XDECREF(consts_list);
17371738
Py_XDECREF(metadata);
1738-
_PyCompile_ExitScope(c);
1739+
if (c->u != NULL) {
1740+
_PyCompile_ExitScope(c);
1741+
}
17391742
compiler_free(c);
17401743
_PyArena_Free(arena);
17411744
return res;

0 commit comments

Comments
 (0)