https://github.com/python/cpython/commit/a719e5fbb63346c751fc9dfbc62af32215f6c0be
commit: a719e5fbb63346c751fc9dfbc62af32215f6c0be
branch: 3.13
author: TERESH1 <svyatoslavteres...@yandex.ru>
committer: pablogsal <pablog...@gmail.com>
date: 2025-05-08T12:18:26+01:00
summary:

[3.13] gh-133516: Raise `ValueError` when constants `True`, `False` or `None` 
are used as an identifier after NFKC normalization (GH-133523) (#133615)

files:
A Misc/NEWS.d/next/Core and 
Builtins/2025-05-06-15-01-41.gh-issue-133516.RqWVf2.rst
M Lib/test/test_ast/test_ast.py
M Parser/pegen.c

diff --git a/Lib/test/test_ast/test_ast.py b/Lib/test/test_ast/test_ast.py
index 7ac5a816bf0229..db2ea9f546eb1a 100644
--- a/Lib/test/test_ast/test_ast.py
+++ b/Lib/test/test_ast/test_ast.py
@@ -960,6 +960,17 @@ def test_constant_as_name(self):
             ):
                 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):
         class _Precedence(enum.IntEnum):
             """Precedence table that originated from python grammar."""
diff --git a/Misc/NEWS.d/next/Core and 
Builtins/2025-05-06-15-01-41.gh-issue-133516.RqWVf2.rst b/Misc/NEWS.d/next/Core 
and Builtins/2025-05-06-15-01-41.gh-issue-133516.RqWVf2.rst
new file mode 100644
index 00000000000000..b93ba11f93252b
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2025-05-06-15-01-41.gh-issue-133516.RqWVf2.rst 
@@ -0,0 +1,2 @@
+Raise :exc:`ValueError` when constants ``True``, ``False`` or ``None`` are
+used as an identifier after NFKC normalization.
diff --git a/Parser/pegen.c b/Parser/pegen.c
index e0cfc16961987d..d167397919f56e 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -545,6 +545,21 @@ _PyPegen_new_identifier(Parser *p, const char *n)
         }
         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();
     _PyUnicode_InternImmortal(interp, &id);
     if (_PyArena_AddPyObject(p->arena, id) < 0)

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to