Skip to content

Commit 17aae76

Browse files
committed
Fix gh-133516
1 parent 3dfed23 commit 17aae76

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,12 @@ def test_constant_as_name(self):
821821
with self.assertRaisesRegex(ValueError, f"identifier field can't represent '{constant}' constant"):
822822
compile(expr, "<test>", "eval")
823823

824+
def test_constant_as_unicode_name(self):
825+
for constant in b"Tru\xe1\xb5\x89", b"Fal\xc5\xbfe", b"N\xc2\xbane":
826+
with self.assertRaises(ValueError,
827+
msg="identifier must not be None, True or False after NFKC normalization"):
828+
ast.parse(constant, mode="eval")
829+
824830
def test_precedence_enum(self):
825831
class _Precedence(enum.IntEnum):
826832
"""Precedence table that originated from python grammar."""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Raise :exc:`ValueError` when constants ``True``, ``False`` or ``None`` are
2+
used as an identifier after NFKC normalization

Parser/pegen.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,14 @@ _PyPegen_new_identifier(Parser *p, const char *n)
549549
}
550550
id = id2;
551551
}
552+
if (_PyUnicode_EqualToASCIIString(id, "None") ||
553+
_PyUnicode_EqualToASCIIString(id, "True") ||
554+
_PyUnicode_EqualToASCIIString(id, "False"))
555+
{
556+
PyErr_SetString(PyExc_ValueError, "identifier must not be None, True or False after NFKC normalization");
557+
Py_DECREF(id);
558+
goto error;
559+
}
552560
PyInterpreterState *interp = _PyInterpreterState_GET();
553561
_PyUnicode_InternImmortal(interp, &id);
554562
if (_PyArena_AddPyObject(p->arena, id) < 0)

0 commit comments

Comments
 (0)