Skip to content

Commit eeec1f6

Browse files
committed
gh-156459: Fix cleanup on error in compiler_set_qualname
1 parent 6671d27 commit eeec1f6

3 files changed

Lines changed: 8 additions & 5 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);

Python/codegen.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ codegen_enter_scope(compiler *c, identifier name, int scope_type,
683683
{
684684
RETURN_IF_ERROR(
685685
_PyCompile_EnterScope(c, name, scope_type, key, lineno, private, umd));
686+
RETURN_IF_ERROR_IN_SCOPE(c, _PyCompile_SetQualname(c));
686687
location loc = LOCATION(lineno, lineno, 0, 0);
687688
if (scope_type == COMPILE_SCOPE_MODULE) {
688689
loc.lineno = 0;

Python/compile.c

Lines changed: 6 additions & 5 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

0 commit comments

Comments
 (0)