Skip to content

gh-133516: Raise ValueError when constants True, False or None are used as an identifier after NFKC normalization #133523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
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.assertRaisesRegex(ValueError,
"identifier must not be None, True or False after Unicode normalization \\(NKFC\\)"):
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.
10 changes: 10 additions & 0 deletions Parser/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,16 @@ _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 Unicode normalization (NKFC)");
Py_DECREF(id);
goto error;
}
PyInterpreterState *interp = _PyInterpreterState_GET();
_PyUnicode_InternImmortal(interp, &id);
if (_PyArena_AddPyObject(p->arena, id) < 0)
Expand Down
Loading