Skip to content

Commit 12092af

Browse files
[3.14] gh-145187: Fix crash on invalid type parameter bound expression in conditional block (GH-145188) (#145196)
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 5e61a16) Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
1 parent bbce6ba commit 12092af

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
@@ -1200,11 +1200,11 @@ codegen_type_param_bound_or_default(compiler *c, expr_ty e,
12001200
ADDOP_LOAD_CONST_NEW(c, LOC(e), defaults);
12011201
RETURN_IF_ERROR(codegen_setup_annotations_scope(c, LOC(e), key, name));
12021202
if (allow_starred && e->kind == Starred_kind) {
1203-
VISIT(c, expr, e->v.Starred.value);
1204-
ADDOP_I(c, LOC(e), UNPACK_SEQUENCE, (Py_ssize_t)1);
1203+
VISIT_IN_SCOPE(c, expr, e->v.Starred.value);
1204+
ADDOP_I_IN_SCOPE(c, LOC(e), UNPACK_SEQUENCE, (Py_ssize_t)1);
12051205
}
12061206
else {
1207-
VISIT(c, expr, e);
1207+
VISIT_IN_SCOPE(c, expr, e);
12081208
}
12091209
ADDOP_IN_SCOPE(c, LOC(e), RETURN_VALUE);
12101210
PyCodeObject *co = _PyCompile_OptimizeAndAssemble(c, 1);

0 commit comments

Comments
 (0)