Skip to content

Commit 81b6c61

Browse files
committed
Keep the error message the same as in the validate_name
1 parent c71edcf commit 81b6c61

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -822,10 +822,15 @@ def test_constant_as_name(self):
822822
compile(expr, "<test>", "eval")
823823

824824
def test_constant_as_unicode_name(self):
825-
for constant in b"Tru\xe1\xb5\x89", b"Fal\xc5\xbfe", b"N\xc2\xbane":
825+
constants = [
826+
("True", b"Tru\xe1\xb5\x89"),
827+
("False", b"Fal\xc5\xbfe"),
828+
("None", b"N\xc2\xbane"),
829+
]
830+
for constant in constants:
826831
with self.assertRaisesRegex(ValueError,
827-
"identifier must not be None, True or False after Unicode normalization \\(NKFC\\)"):
828-
ast.parse(constant, mode="eval")
832+
f"identifier field can't represent '{constant[0]}' constant"):
833+
ast.parse(constant[1], mode="eval")
829834

830835
def test_precedence_enum(self):
831836
class _Precedence(enum.IntEnum):

Parser/pegen.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,20 @@ _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,
557-
"identifier must not be None, True or False "
558-
"after Unicode normalization (NKFC)");
559-
Py_DECREF(id);
560-
goto error;
552+
static const char * const forbidden[] = {
553+
"None",
554+
"True",
555+
"False",
556+
NULL
557+
};
558+
for (int i = 0; forbidden[i] != NULL; i++) {
559+
if (_PyUnicode_EqualToASCIIString(id, forbidden[i])) {
560+
PyErr_Format(PyExc_ValueError,
561+
"identifier field can't represent '%s' constant",
562+
forbidden[i]);
563+
Py_DECREF(id);
564+
goto error;
565+
}
561566
}
562567
PyInterpreterState *interp = _PyInterpreterState_GET();
563568
_PyUnicode_InternImmortal(interp, &id);

0 commit comments

Comments
 (0)