mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
[3.12] gh-109341: Fix crash on compiling invalid AST including TypeAlias (GH-109349) (#109381)
gh-109341: Fix crash on compiling invalid AST including TypeAlias (GH-109349)
(cherry picked from commit 987b4bc087
)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
36d6ba08d0
commit
5c7e8c3b72
3 changed files with 26 additions and 0 deletions
|
@ -478,6 +478,26 @@ class TestSpecifics(unittest.TestCase):
|
||||||
ast.body = [_ast.BoolOp()]
|
ast.body = [_ast.BoolOp()]
|
||||||
self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
|
self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
|
||||||
|
|
||||||
|
def test_compile_invalid_typealias(self):
|
||||||
|
# gh-109341
|
||||||
|
m = ast.Module(
|
||||||
|
body=[
|
||||||
|
ast.TypeAlias(
|
||||||
|
name=ast.Subscript(
|
||||||
|
value=ast.Name(id="foo", ctx=ast.Load()),
|
||||||
|
slice=ast.Constant(value="x"),
|
||||||
|
ctx=ast.Store(),
|
||||||
|
),
|
||||||
|
type_params=[],
|
||||||
|
value=ast.Name(id="Callable", ctx=ast.Load()),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
type_ignores=[],
|
||||||
|
)
|
||||||
|
|
||||||
|
with self.assertRaisesRegex(TypeError, "TypeAlias with non-Name name"):
|
||||||
|
compile(ast.fix_missing_locations(m), "<file>", "exec")
|
||||||
|
|
||||||
def test_dict_evaluation_order(self):
|
def test_dict_evaluation_order(self):
|
||||||
i = 0
|
i = 0
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix crash when compiling an invalid AST involving a :class:`ast.TypeAlias`.
|
|
@ -768,6 +768,11 @@ validate_stmt(struct validator *state, stmt_ty stmt)
|
||||||
validate_expr(state, stmt->v.AnnAssign.annotation, Load);
|
validate_expr(state, stmt->v.AnnAssign.annotation, Load);
|
||||||
break;
|
break;
|
||||||
case TypeAlias_kind:
|
case TypeAlias_kind:
|
||||||
|
if (stmt->v.TypeAlias.name->kind != Name_kind) {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"TypeAlias with non-Name name");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
ret = validate_expr(state, stmt->v.TypeAlias.name, Store) &&
|
ret = validate_expr(state, stmt->v.TypeAlias.name, Store) &&
|
||||||
validate_type_params(state, stmt->v.TypeAlias.type_params) &&
|
validate_type_params(state, stmt->v.TypeAlias.type_params) &&
|
||||||
validate_expr(state, stmt->v.TypeAlias.value, Load);
|
validate_expr(state, stmt->v.TypeAlias.value, Load);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue