8000 GH-105588: Add missing error checks to some `obj2ast_*` converters by brandtbucher · Pull Request #105589 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-105588: Add missing error checks to some obj2ast_* converters #105589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add failing regression tests
  • Loading branch information
brandtbucher committed Jun 9, 2023
commit 0546055b055d05b037e219ebf7099d104b3a0ec9
27 changes: 27 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dis
import enum
import os
import re
import sys
import textwrap
import types
Expand Down Expand Up @@ -1110,6 +1111,32 @@ def test_null_bytes(self):
msg="source code string cannot contain null bytes"):
ast.parse("a\0b")

def assert_none_check(self, node: type[ast.AST], attr: str, source: str) -> None:
with self.subTest(f"{node.__name__}.{attr}"):
tree = ast.parse(source)
found = 0
for child in ast.walk(tree):
if isinstance(child, node):
setattr(child, attr, None)
found += 1
self.assertEqual(found, 1)
e = re.escape(f"field '{attr}' is required for {node.__name__}")
with self.assertRaisesRegex(ValueError, f"^{e}$"):
compile(tree, "<test>", "exec")

def test_none_checks(self) -> None:
tests = [
(ast.alias, "name", "import spam as SPAM"),
(ast.arg, "arg", "def spam(SPAM): spam"),
(ast.comprehension, "target", "[spam for SPAM in spam]"),
(ast.comprehension, "iter", "[spam for spam in SPAM]"),
(ast.keyword, "value", "spam(**SPAM)"),
(ast.match_case, "pattern", "match spam:\n case SPAM: pass"),
(ast.withitem, "context_expr", "with SPAM: pass"),
]
for node, attr, source in tests:
self.assert_none_check(node, attr, source)

class ASTHelpers_Test(unittest.TestCase):
maxDiff = None

Expand Down
0