with self.assertRaises(SyntaxError):
ast.parse('f"{x=}"', feature_version=(3, 7))
+ def test_constant_as_name(self):
+ for constant in "True", "False", "None":
+ expr = ast.Expression(ast.Name(constant, ast.Load()))
+ ast.fix_missing_locations(expr)
+ with self.assertRaisesRegex(ValueError, f"Name node can't be used with '{constant}' constant"):
+ compile(expr, "<test>", "eval")
+
class ASTHelpers_Test(unittest.TestCase):
maxDiff = None
static int validate_stmt(stmt_ty);
static int validate_expr(expr_ty, expr_context_ty);
+static int
+validate_name(PyObject *name)
+{
+ assert(PyUnicode_Check(name));
+ static const char * const forbidden[] = {
+ "None",
+ "True",
+ "False",
+ NULL
+ };
+ for (int i = 0; forbidden[i] != NULL; i++) {
+ if (_PyUnicode_EqualToASCIIString(name, forbidden[i])) {
+ PyErr_Format(PyExc_ValueError, "Name node can't be used with '%s' constant", forbidden[i]);
+ return 0;
+ }
+ }
+ return 1;
+}
+
static int
validate_comprehension(asdl_seq *gens)
{
actual_ctx = exp->v.Starred.ctx;
break;
case Name_kind:
+ if (!validate_name(exp->v.Name.id)) {
+ return 0;
+ }
actual_ctx = exp->v.Name.ctx;
break;
case List_kind: