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

Commita4056c8

Browse files
authored
GH-105588: Add missing error checks to some obj2ast_* converters (GH-105589)
1 parent3af2dc7 commita4056c8

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

‎Lib/test/test_ast.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
importdis
44
importenum
55
importos
6+
importre
67
importsys
78
importtextwrap
89
importtypes
@@ -1110,6 +1111,32 @@ def test_null_bytes(self):
11101111
msg="source code string cannot contain null bytes"):
11111112
ast.parse("a\0b")
11121113

1114+
defassert_none_check(self,node:type[ast.AST],attr:str,source:str)->None:
1115+
withself.subTest(f"{node.__name__}.{attr}"):
1116+
tree=ast.parse(source)
1117+
found=0
1118+
forchildinast.walk(tree):
1119+
ifisinstance(child,node):
1120+
setattr(child,attr,None)
1121+
found+=1
1122+
self.assertEqual(found,1)
1123+
e=re.escape(f"field '{attr}' is required for{node.__name__}")
1124+
withself.assertRaisesRegex(ValueError,f"^{e}$"):
1125+
compile(tree,"<test>","exec")
1126+
1127+
deftest_none_checks(self)->None:
1128+
tests= [
1129+
(ast.alias,"name","import spam as SPAM"),
1130+
(ast.arg,"arg","def spam(SPAM): spam"),
1131+
(ast.comprehension,"target","[spam for SPAM in spam]"),
1132+
(ast.comprehension,"iter","[spam for spam in SPAM]"),
1133+
(ast.keyword,"value","spam(**SPAM)"),
1134+
(ast.match_case,"pattern","match spam:\n case SPAM: spam"),
1135+
(ast.withitem,"context_expr","with SPAM: spam"),
1136+
]
1137+
fornode,attr,sourceintests:
1138+
self.assert_none_check(node,attr,source)
1139+
11131140
classASTHelpers_Test(unittest.TestCase):
11141141
maxDiff=None
11151142

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an issue that could result in crashes when compiling malformed
2+
:mod:`ast` nodes.

‎Parser/asdl_c.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ def visitProduct(self, prod, name):
601601
args= [f.nameforfinprod.fields]
602602
args.extend([a.nameforainprod.attributes])
603603
self.emit("*out = %s(%s);"% (ast_func_name(name),self.buildArgs(args)),1)
604+
self.emit("if (*out == NULL) goto failed;",1)
604605
self.emit("return 0;",1)
605606
self.emit("failed:",0)
606607
self.emit("Py_XDECREF(tmp);",1)

‎Python/Python-ast.c‎

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp