diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-06-15 23:12:01 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-15 23:12:01 (GMT) |
commit | 0add51672d8c8db4b18838dd592991a6cd9b55ca (patch) | |
tree | 2bbe6348d66a63de5adcf8f25067e1162f728155 /Lib/test | |
parent | e9cf5a324e10b693d464692ab19422c40d5e179f (diff) | |
download | cpython-0add51672d8c8db4b18838dd592991a6cd9b55ca.zip cpython-0add51672d8c8db4b18838dd592991a6cd9b55ca.tar.gz cpython-0add51672d8c8db4b18838dd592991a6cd9b55ca.tar.bz2 |
[3.12] GH-105588: Add missing error checks to some obj2ast_* converters (GH-105838)
GH-105588: Add missing error checks to some obj2ast_* converters (GH-105589)
(cherry picked from commit a4056c8f9c2d9970d39e3cb6bffb255cd4b8a42c)
Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_ast.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index ffd082e..a03fa4c 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -3,6 +3,7 @@ import builtins import dis import enum import os +import re import sys import textwrap import types @@ -1110,6 +1111,32 @@ class AST_Tests(unittest.TestCase): 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: spam"), + (ast.withitem, "context_expr", "with SPAM: spam"), + ] + for node, attr, source in tests: + self.assert_none_check(node, attr, source) + class ASTHelpers_Test(unittest.TestCase): maxDiff = None |