Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdaadf7c

Browse files
committed
[3.13]gh-133516: RaiseValueError when constantsTrue,False orNone are used as an identifier after NFKC normalization (GH-133523)
(cherry picked from commitd9b0b07)
1 parenta0dd4f0 commitdaadf7c

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

‎Lib/test/test_ast/test_ast.py

Lines changed: 11 additions & 0 deletions
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+
deftest_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+
forconstantinconstants:
970+
withself.assertRaisesRegex(ValueError,
971+
f"identifier field can't represent '{constant[0]}' constant"):
972+
ast.parse(constant[1],mode="eval")
973+
963974
deftest_precedence_enum(self):
964975
class_Precedence(enum.IntEnum):
965976
"""Precedence table that originated from python grammar."""
Lines changed: 2 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
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+
staticconstchar*constforbidden[]= {
549+
"None",
550+
"True",
551+
"False",
552+
NULL
553+
};
554+
for (inti=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+
gotoerror;
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)

[8]ページ先頭

©2009-2025 Movatter.jp