https://github.com/python/cpython/commit/a53b1324fcb8144123d786ed2a06ef1442333c49
commit: a53b1324fcb8144123d786ed2a06ef1442333c49
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: hugovk <[email protected]>
date: 2026-10-23T20:35:42+03:00
summary:

[3.14] gh-156459: Fix cleanup on error in compiler_set_qualname (GH-156462) 
(#156575)

Co-authored-by: Irit Katriel <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-08-27-13-26-50.gh-issue-156459.Z8Zqik.rst
M Include/internal/pycore_compile.h
M Python/codegen.c
M Python/compile.c

diff --git a/Include/internal/pycore_compile.h 
b/Include/internal/pycore_compile.h
index 193d5d180a3c8c..f78a3d320278db 100644
--- a/Include/internal/pycore_compile.h
+++ b/Include/internal/pycore_compile.h
@@ -133,6 +133,7 @@ int _PyCompile_EnterScope(struct _PyCompiler *c, identifier 
name, int scope_type
                           void *key, int lineno, PyObject *private,
                           _PyCompile_CodeUnitMetadata *umd);
 void _PyCompile_ExitScope(struct _PyCompiler *c);
+int _PyCompile_SetQualname(struct _PyCompiler *c);
 Py_ssize_t _PyCompile_AddConst(struct _PyCompiler *c, PyObject *o);
 _PyInstructionSequence *_PyCompile_InstrSequence(struct _PyCompiler *c);
 int _PyCompile_StartAnnotationSetup(struct _PyCompiler *c);
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-27-13-26-50.gh-issue-156459.Z8Zqik.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-27-13-26-50.gh-issue-156459.Z8Zqik.rst
new file mode 100644
index 00000000000000..2de2e0e72273c6
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-27-13-26-50.gh-issue-156459.Z8Zqik.rst
@@ -0,0 +1,3 @@
+Fix cleanup on error in ``compiler_set_qualname``. Previously it was called in
+``_PyCompile_EnterScope``, after the scope had been entered, and this was not
+reversed in case of an error.
diff --git a/Python/codegen.c b/Python/codegen.c
index 0aa9995b4f087f..5e391f5595a0b2 100644
--- a/Python/codegen.c
+++ b/Python/codegen.c
@@ -656,6 +656,7 @@ codegen_enter_scope(compiler *c, identifier name, int 
scope_type,
 {
     RETURN_IF_ERROR(
         _PyCompile_EnterScope(c, name, scope_type, key, lineno, private, umd));
+    RETURN_IF_ERROR_IN_SCOPE(c, _PyCompile_SetQualname(c));
     location loc = LOCATION(lineno, lineno, 0, 0);
     if (scope_type == COMPILE_SCOPE_MODULE) {
         loc.lineno = 0;
diff --git a/Python/compile.c b/Python/compile.c
index aa0d1b0ed31374..df436eaf4bbd70 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -226,13 +226,17 @@ _PyCompile_MaybeAddStaticAttributeToClass(compiler *c, 
expr_ty e)
     return SUCCESS;
 }
 
-static int
-compiler_set_qualname(compiler *c)
+int
+_PyCompile_SetQualname(compiler *c)
 {
     Py_ssize_t stack_size;
     struct compiler_unit *u = c->u;
     PyObject *name, *base;
 
+    if (u->u_scope_type == COMPILE_SCOPE_MODULE) {
+        return SUCCESS;
+    }
+
     base = NULL;
     stack_size = PyList_GET_SIZE(c->c_stack);
     assert(stack_size >= 1);
@@ -704,9 +708,6 @@ _PyCompile_EnterScope(compiler *c, identifier name, int 
scope_type,
     u->u_private = Py_XNewRef(private);
 
     c->u = u;
-    if (scope_type != COMPILE_SCOPE_MODULE) {
-        RETURN_IF_ERROR(compiler_set_qualname(c));
-    }
     return SUCCESS;
 }
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to