mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.14] gh-133516: Raise ValueError when constants True, False or None are used as an identifier after NFKC normalization (GH-133523) (#133596)
This commit is contained in:
parent
c98b5a9369
commit
54c3aa1597
3 changed files with 28 additions and 0 deletions
|
|
@ -821,6 +821,17 @@ def test_constant_as_name(self):
|
||||||
with self.assertRaisesRegex(ValueError, f"identifier field can't represent '{constant}' constant"):
|
with self.assertRaisesRegex(ValueError, f"identifier field can't represent '{constant}' constant"):
|
||||||
compile(expr, "<test>", "eval")
|
compile(expr, "<test>", "eval")
|
||||||
|
|
||||||
|
def test_constant_as_unicode_name(self):
|
||||||
|
constants = [
|
||||||
|
("True", b"Tru\xe1\xb5\x89"),
|
||||||
|
("False", b"Fal\xc5\xbfe"),
|
||||||
|
("None", b"N\xc2\xbane"),
|
||||||
|
]
|
||||||
|
for constant in constants:
|
||||||
|
with self.assertRaisesRegex(ValueError,
|
||||||
|
f"identifier field can't represent '{constant[0]}' constant"):
|
||||||
|
ast.parse(constant[1], mode="eval")
|
||||||
|
|
||||||
def test_precedence_enum(self):
|
def test_precedence_enum(self):
|
||||||
class _Precedence(enum.IntEnum):
|
class _Precedence(enum.IntEnum):
|
||||||
"""Precedence table that originated from python grammar."""
|
"""Precedence table that originated from python grammar."""
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
Raise :exc:`ValueError` when constants ``True``, ``False`` or ``None`` are
|
||||||
|
used as an identifier after NFKC normalization.
|
||||||
|
|
@ -549,6 +549,21 @@ _PyPegen_new_identifier(Parser *p, const char *n)
|
||||||
}
|
}
|
||||||
id = id2;
|
id = id2;
|
||||||
}
|
}
|
||||||
|
static const char * const forbidden[] = {
|
||||||
|
"None",
|
||||||
|
"True",
|
||||||
|
"False",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
for (int i = 0; forbidden[i] != NULL; i++) {
|
||||||
|
if (_PyUnicode_EqualToASCIIString(id, forbidden[i])) {
|
||||||
|
PyErr_Format(PyExc_ValueError,
|
||||||
|
"identifier field can't represent '%s' constant",
|
||||||
|
forbidden[i]);
|
||||||
|
Py_DECREF(id);
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
}
|
||||||
PyInterpreterState *interp = _PyInterpreterState_GET();
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
||||||
_PyUnicode_InternImmortal(interp, &id);
|
_PyUnicode_InternImmortal(interp, &id);
|
||||||
if (_PyArena_AddPyObject(p->arena, id) < 0)
|
if (_PyArena_AddPyObject(p->arena, id) < 0)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue