Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,12 @@ def test_constant_as_name(self):
with self.assertRaisesRegex(ValueError, f"identifier field can't represent '{constant}' constant"):
compile(expr, "<test>", "eval")

def test_constant_as_unicode_name(self):
for constant in b"Tru\xe1\xb5\x89", b"Fal\xc5\xbfe", b"N\xc2\xbane":
with self.assertRaises(ValueError,
msg="identifier must not be None, True or False after NFKC normalization"):
ast.parse(constant, mode="eval")

def test_precedence_enum(self):
class _Precedence(enum.IntEnum):
"""Precedence table that originated from python grammar."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Raise :exc:`ValueError` when constants ``True``, ``False`` or ``None`` are
used as an identifier after NFKC normalization
8 changes: 8 additions & 0 deletions Parser/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,14 @@ _PyPegen_new_identifier(Parser *p, const char *n)
}
id = id2;
}
if (_PyUnicode_EqualToASCIIString(id, "None") ||
_PyUnicode_EqualToASCIIString(id, "True") ||
_PyUnicode_EqualToASCIIString(id, "False"))
{
PyErr_SetString(PyExc_ValueError, "identifier must not be None, True or False after NFKC normalization");
Py_DECREF(id);
goto error;
}
PyInterpreterState *interp = _PyInterpreterState_GET();
_PyUnicode_InternImmortal(interp, &id);
if (_PyArena_AddPyObject(p->arena, id) < 0)
Expand Down
Loading