Skip to content

Commit daadf7c

Browse files
committed
[3.13] gh-133516: Raise ValueError when constants True, False or None are used as an identifier after NFKC normalization (GH-133523)
(cherry picked from commit d9b0b07)
1 parent a0dd4f0 commit daadf7c

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Lib/test/test_ast/test_ast.py

+11
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,17 @@ def test_constant_as_name(self):
960960
):
961961
compile(expr, "<test>", "eval")
962962

963+
def test_constant_as_unicode_name(self):
964+
constants = [
965+
("True", b"Tru\xe1\xb5\x89"),
966+
("False", b"Fal\xc5\xbfe"),
967+
("None", b"N\xc2\xbane"),
968+
]
969+
for constant in constants:
970+
with self.assertRaisesRegex(ValueError,
971+
f"identifier field can't represent '{constant[0]}' constant"):
972+
ast.parse(constant[1], mode="eval")
973+
963974
def test_precedence_enum(self):
964975
class _Precedence(enum.IntEnum):
965976
"""Precedence table that originated from python grammar."""
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

+15
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,21 @@ _PyPegen_new_identifier(Parser *p, const char *n)
545545
}
546546
id = id2;
547547
}
548+
static const char * const forbidden[] = {
549+
"None",
550+
"True",
551+
"False",
552+
NULL
553+
};
554+
for (int i = 0; forbidden[i] != NULL; i++) {
555+
if (_PyUnicode_EqualToASCIIString(id, forbidden[i])) {
556+
PyErr_Format(PyExc_ValueError,
557+
"identifier field can't represent '%s' constant",
558+
forbidden[i]);
559+
Py_DECREF(id);
560+
goto error;
561+
}
562+
}
548563
PyInterpreterState *interp = _PyInterpreterState_GET();
549564
_PyUnicode_InternImmortal(interp, &id);
550565
if (_PyArena_AddPyObject(p->arena, id) < 0)

0 commit comments

Comments
 (0)