Skip to content

Commit

Permalink
Merge branch 'main' into api_managed_dict
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored Sep 13, 2023
2 parents 00af801 + 987b4bc commit f50f2d0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,33 @@ def f():
self.assertIn("_A__mangled_mod", A.f.__code__.co_varnames)
self.assertIn("__package__", A.f.__code__.co_varnames)

def test_compile_invalid_namedexpr(self):
# gh-109351
m = ast.Module(
body=[
ast.Expr(
value=ast.ListComp(
elt=ast.NamedExpr(
target=ast.Constant(value=1),
value=ast.Constant(value=3),
),
generators=[
ast.comprehension(
target=ast.Name(id="x", ctx=ast.Store()),
iter=ast.Name(id="y", ctx=ast.Load()),
ifs=[],
is_async=0,
)
],
)
)
],
type_ignores=[],
)

with self.assertRaisesRegex(TypeError, "NamedExpr target must be a Name"):
compile(ast.fix_missing_locations(m), "<file>", "exec")

def test_compile_ast(self):
fname = __file__
if fname.lower().endswith('pyc'):
Expand Down Expand Up @@ -478,6 +505,26 @@ def test_compile_ast(self):
ast.body = [_ast.BoolOp()]
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):
i = 0

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crash when compiling an invalid AST involving a :class:`ast.TypeAlias`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash when compiling an invalid AST involving a named (walrus)
expression.
10 changes: 10 additions & 0 deletions Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,11 @@ validate_expr(struct validator *state, expr_ty exp, expr_context_ty ctx)
ret = validate_exprs(state, exp->v.Tuple.elts, ctx, 0);
break;
case NamedExpr_kind:
if (exp->v.NamedExpr.target->kind != Name_kind) {
PyErr_SetString(PyExc_TypeError,
"NamedExpr target must be a Name");
return 0;
}
ret = validate_expr(state, exp->v.NamedExpr.value, Load);
break;
/* This last case doesn't have any checking. */
Expand Down Expand Up @@ -768,6 +773,11 @@ validate_stmt(struct validator *state, stmt_ty stmt)
validate_expr(state, stmt->v.AnnAssign.annotation, Load);
break;
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) &&
validate_type_params(state, stmt->v.TypeAlias.type_params) &&
validate_expr(state, stmt->v.TypeAlias.value, Load);
Expand Down

0 comments on commit f50f2d0

Please sign in to comment.