]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-133516: Raise `ValueError` when constants `True`, `False` or `None` are...
authorTERESH1 <svyatoslavtereshin@yandex.ru>
Thu, 8 May 2025 11:18:26 +0000 (14:18 +0300)
committerGitHub <noreply@github.com>
Thu, 8 May 2025 11:18:26 +0000 (12:18 +0100)
Lib/test/test_ast/test_ast.py
Misc/NEWS.d/next/Core and Builtins/2025-05-06-15-01-41.gh-issue-133516.RqWVf2.rst [new file with mode: 0644]
Parser/pegen.c

index 7ac5a816bf02296b42920c037d1ec64d76bbb79b..db2ea9f546eb1a8be32fb84a3a1ae713a1504688 100644 (file)
@@ -960,6 +960,17 @@ class AST_Tests(unittest.TestCase):
             ):
                 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 (file)
index 0000000..b93ba11
--- /dev/null
@@ -0,0 +1,2 @@
+Raise :exc:`ValueError` when constants ``True``, ``False`` or ``None`` are
+used as an identifier after NFKC normalization.
index e0cfc16961987d17496aaadc1ca6b46133918af3..d167397919f56e906cdf79dc47c0537c2b084bf3 100644 (file)
@@ -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)