Skip to content

Commit

Permalink
Better fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Sep 12, 2023
1 parent ecd7b6c commit c8d43ca
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def test_compile_invalid_namedexpr(self):
type_ignores=[],
)

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

def test_compile_ast(self):
Expand Down
5 changes: 5 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_ValueError,
"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
5 changes: 1 addition & 4 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1869,10 +1869,7 @@ static int
symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
{
assert(st->st_stack);
if (e->kind != Name_kind) {
PyErr_SetString(PyExc_SyntaxError, ":= target must be a Name");
VISIT_QUIT(st, 0);
}
assert(e->kind == Name_kind);

PyObject *target_name = e->v.Name.id;
Py_ssize_t i, size;
Expand Down

0 comments on commit c8d43ca

Please sign in to comment.