Skip to content

Commit 5e61a16

Browse files
gh-145187: Fix crash on invalid type parameter bound expression in conditional block (GH-145188)
Fix parsing crash found by oss-fuzz
1 parent 56c4f10 commit 5e61a16

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

Lib/test/test_type_params.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ def test_incorrect_mro_explicit_object(self):
152152
with self.assertRaisesRegex(TypeError, r"\(MRO\) for bases object, Generic"):
153153
class My[X](object): ...
154154

155+
def test_compile_error_in_type_param_bound(self):
156+
# This should not crash, see gh-145187
157+
check_syntax_error(
158+
self,
159+
"if True:\n class h[l:{7for*()in 0}]:2"
160+
)
161+
155162

156163
class TypeParamsNonlocalTest(unittest.TestCase):
157164
def test_nonlocal_disallowed_01(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix compiler assertion fail when a type parameter bound contains an invalid
2+
expression in a conditional block.

Python/codegen.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,11 +1244,11 @@ codegen_type_param_bound_or_default(compiler *c, expr_ty e,
12441244
ADDOP_LOAD_CONST_NEW(c, LOC(e), defaults);
12451245
RETURN_IF_ERROR(codegen_setup_annotations_scope(c, LOC(e), key, name));
12461246
if (allow_starred && e->kind == Starred_kind) {
1247-
VISIT(c, expr, e->v.Starred.value);
1248-
ADDOP_I(c, LOC(e), UNPACK_SEQUENCE, (Py_ssize_t)1);
1247+
VISIT_IN_SCOPE(c, expr, e->v.Starred.value);
1248+
ADDOP_I_IN_SCOPE(c, LOC(e), UNPACK_SEQUENCE, (Py_ssize_t)1);
12491249
}
12501250
else {
1251-
VISIT(c, expr, e);
1251+
VISIT_IN_SCOPE(c, expr, e);
12521252
}
12531253
ADDOP_IN_SCOPE(c, LOC(e), RETURN_VALUE);
12541254
PyCodeObject *co = _PyCompile_OptimizeAndAssemble(c, 1);

0 commit comments

Comments
 (0)