From eeec1f68485dcc84095d04b5e14f76ef99526cdf Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 27 Aug 2026 13:18:58 +0100 Subject: [PATCH 1/3] gh-156459: Fix cleanup on error in compiler_set_qualname --- Include/internal/pycore_compile.h | 1 + Python/codegen.c | 1 + Python/compile.c | 11 ++++++----- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Include/internal/pycore_compile.h b/Include/internal/pycore_compile.h index 911cc1f10f1513..7e248429af8eb8 100644 --- a/Include/internal/pycore_compile.h +++ b/Include/internal/pycore_compile.h @@ -137,6 +137,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/Python/codegen.c b/Python/codegen.c index bedf3b17c52ce4..96555f4c037234 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -683,6 +683,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 fefb2b04b78db8..7bfad525b16915 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -233,13 +233,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); @@ -724,9 +728,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; } From 77255246fe743212a757a729ef74e7c96218c25f Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 27 Aug 2026 13:26:57 +0100 Subject: [PATCH 2/3] add news --- .../2026-08-27-13-26-50.gh-issue-156459.Z8Zqik.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-08-27-13-26-50.gh-issue-156459.Z8Zqik.rst 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..286e0e2ac38d17 --- /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 enterred, and this was not +reversed in case of an error. From 965a9ece78cbeccee1ad3ccab6743dfef49bf73a Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 27 Aug 2026 13:40:23 +0100 Subject: [PATCH 3/3] lint --- .../2026-08-27-13-26-50.gh-issue-156459.Z8Zqik.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index 286e0e2ac38d17..7bf1ab39796098 100644 --- 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 @@ -1,3 +1,3 @@ -Fix cleanup on error in `compiler_set_qualname`. Previously it was called in -`_PyCompile_EnterScope`, after the scope had been enterred, and this was not +Fix cleanup on error in ``compiler_set_qualname``. Previously it was called in +``_PyCompile_EnterScope``, after the scope had been enterred, and this was not reversed in case of an error.