]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-145187: Fix crash on invalid type parameter bound expression in conditional...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 24 Feb 2026 21:13:08 +0000 (22:13 +0100)
committerGitHub <noreply@github.com>
Tue, 24 Feb 2026 21:13:08 +0000 (21:13 +0000)
gh-145187: Fix crash on invalid type parameter bound expression in conditional block (GH-145188)

Fix parsing crash found by oss-fuzz
(cherry picked from commit 5e61a16c1058e5de66b71dfdc9720d40e9f515d9)

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Lib/test/test_type_params.py
Misc/NEWS.d/next/Core_and_Builtins/2026-02-24-18-30-56.gh-issue-145187.YjPu1Z.rst [new file with mode: 0644]
Python/codegen.c

index 0f393def8272713b82a963665a912dbdb1ab7afb..84c1b9541367363685f25d6131dddb491ef853eb 100644 (file)
@@ -152,6 +152,13 @@ class TypeParamsInvalidTest(unittest.TestCase):
         with self.assertRaisesRegex(TypeError, r"\(MRO\) for bases object, Generic"):
             class My[X](object): ...
 
+    def test_compile_error_in_type_param_bound(self):
+        # This should not crash, see gh-145187
+        check_syntax_error(
+            self,
+            "if True:\n class h[l:{7for*()in 0}]:2"
+        )
+
 
 class TypeParamsNonlocalTest(unittest.TestCase):
     def test_nonlocal_disallowed_01(self):
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-24-18-30-56.gh-issue-145187.YjPu1Z.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-24-18-30-56.gh-issue-145187.YjPu1Z.rst
new file mode 100644 (file)
index 0000000..08c6b44
--- /dev/null
@@ -0,0 +1,2 @@
+Fix compiler assertion fail when a type parameter bound contains an invalid
+expression in a conditional block.
index bacd3460f2fa9ce83790873d043765f2d443e131..085ebc391a165f99945e6409695dd4d446686e5f 100644 (file)
@@ -1200,11 +1200,11 @@ codegen_type_param_bound_or_default(compiler *c, expr_ty e,
     ADDOP_LOAD_CONST_NEW(c, LOC(e), defaults);
     RETURN_IF_ERROR(codegen_setup_annotations_scope(c, LOC(e), key, name));
     if (allow_starred && e->kind == Starred_kind) {
-        VISIT(c, expr, e->v.Starred.value);
-        ADDOP_I(c, LOC(e), UNPACK_SEQUENCE, (Py_ssize_t)1);
+        VISIT_IN_SCOPE(c, expr, e->v.Starred.value);
+        ADDOP_I_IN_SCOPE(c, LOC(e), UNPACK_SEQUENCE, (Py_ssize_t)1);
     }
     else {
-        VISIT(c, expr, e);
+        VISIT_IN_SCOPE(c, expr, e);
     }
     ADDOP_IN_SCOPE(c, LOC(e), RETURN_VALUE);
     PyCodeObject *co = _PyCompile_OptimizeAndAssemble(c, 1);