From 5a990b64be27452eec35a357f51ba652c51bcaba Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Thu, 27 Aug 2026 21:11:05 +0100 Subject: [PATCH] gh-156466: fix cleanup on error in codegen_setup_annotations_scope (#156473) (cherry picked from commit 24e5a55ccb0c5db68136d29f4c16c0bc21407db2) --- Python/codegen.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Python/codegen.c b/Python/codegen.c index 529c1733598e381..b4baa11f9b56e61 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -703,16 +703,8 @@ codegen_enter_scope(compiler *c, identifier name, int scope_type, } static int -codegen_setup_annotations_scope(compiler *c, location loc, - void *key, PyObject *name) +codegen_emit_annotations_prologue(compiler *c, location loc) { - _PyCompile_CodeUnitMetadata umd = { - .u_posonlyargcount = 1, - }; - RETURN_IF_ERROR( - codegen_enter_scope(c, name, COMPILE_SCOPE_ANNOTATIONS, - key, loc.lineno, NULL, &umd)); - // if .format > VALUE_WITH_FAKE_GLOBALS: raise NotImplementedError PyObject *value_with_fake_globals = PyLong_FromLong(_Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS); if (value_with_fake_globals == NULL) { @@ -732,6 +724,21 @@ codegen_setup_annotations_scope(compiler *c, location loc, return SUCCESS; } +static int +codegen_setup_annotations_scope(compiler *c, location loc, + void *key, PyObject *name) +{ + _PyCompile_CodeUnitMetadata umd = { + .u_posonlyargcount = 1, + }; + RETURN_IF_ERROR( + codegen_enter_scope(c, name, COMPILE_SCOPE_ANNOTATIONS, + key, loc.lineno, NULL, &umd)); + + RETURN_IF_ERROR_IN_SCOPE(c, codegen_emit_annotations_prologue(c, loc)); + return SUCCESS; +} + static int codegen_leave_annotations_scope(compiler *c, location loc) {