diff options
Diffstat (limited to 'tests/run/test_patma.py')
-rw-r--r-- | tests/run/test_patma.py | 67 |
1 files changed, 34 insertions, 33 deletions
diff --git a/tests/run/test_patma.py b/tests/run/test_patma.py index 6956099e2..240dbf0d3 100644 --- a/tests/run/test_patma.py +++ b/tests/run/test_patma.py @@ -4,40 +4,34 @@ # new code import cython -from Cython.Compiler.Main import compile as cython_compile, CompileError -from Cython.Build.Inline import cython_inline -import contextlib -from tempfile import NamedTemporaryFile - -@contextlib.contextmanager -def hidden_stderr(): - try: - from StringIO import StringIO - except ImportError: - from io import StringIO - - old_stderr = sys.stderr - try: - sys.stderr = StringIO() - yield - finally: - sys.stderr = old_stderr +from Cython.Compiler.TreeFragment import TreeFragment, StringParseContext +from Cython.Compiler.Errors import local_errors, CompileError +from Cython.Compiler.ParseTreeTransforms import PostParse def _compile(code): - with NamedTemporaryFile(suffix='.py') as f: - f.write(code.encode('utf8')) - f.flush() + context = StringParseContext("test") + # all the errors we care about are in the parsing or postparse stage + try: + with local_errors() as errors: + result = TreeFragment(code, pipeline=[PostParse(context)]) + result = result.substitute() + if errors: + raise errors[0] # compile error, which should get caught + else: + return result + except CompileError as e: + raise SyntaxError(e.message_only) - with hidden_stderr(): - result = cython_compile(f.name, language_level=3) - return result if cython.compiled: def compile(code, name, what): assert what == 'exec' - result = _compile(code) - if not result.c_file: - raise SyntaxError('unexpected EOF') # compile is only used for testing errors + _compile(code) + + +def disable(func): + pass + ############## SLIGHTLY MODIFIED ORIGINAL CODE import array @@ -69,9 +63,9 @@ class TestTracing(unittest.TestCase): # Deeply nested patterns can cause exponential backtracking when parsing. # See CPython gh-93671 for more information. # - # DW Cython note - this doesn't break the parser but may cause a + # DW: Cython note - this doesn't break the parser but may cause a # RecursionError later in the code-generation. I don't believe that's - # easily avoidable + # easily avoidable with the way Cython visitors currently work levels = 100 @@ -84,10 +78,9 @@ class TestTracing(unittest.TestCase): for pattern in patterns: with self.subTest(pattern): code = inspect.cleandoc(""" - if 0: # disabled - FIXME remove once pattern matching is fully implemented! - match None: - case {}: - pass + match None: + case {}: + pass """.format(pattern)) compile(code, "<string>", "exec") @@ -3019,6 +3012,7 @@ class TestSyntaxErrors(unittest.TestCase): """) + @disable # validation will be added when class patterns are added def test_attribute_name_repeated_in_class_pattern(self): self.assert_syntax_error(""" match ...: @@ -3117,6 +3111,7 @@ class TestSyntaxErrors(unittest.TestCase): pass """) + @disable # will be implemented as part of sequence patterns def test_multiple_starred_names_in_sequence_pattern_0(self): self.assert_syntax_error(""" match ...: @@ -3124,6 +3119,7 @@ class TestSyntaxErrors(unittest.TestCase): pass """) + @disable # will be implemented as part of sequence patterns def test_multiple_starred_names_in_sequence_pattern_1(self): self.assert_syntax_error(""" match ...: @@ -3258,6 +3254,7 @@ class TestSyntaxErrors(unittest.TestCase): pass """) + @disable # validation will be added when class patterns are added def test_mapping_pattern_duplicate_key(self): self.assert_syntax_error(""" match ...: @@ -3265,6 +3262,7 @@ class TestSyntaxErrors(unittest.TestCase): pass """) + @disable # validation will be added when class patterns are added def test_mapping_pattern_duplicate_key_edge_case0(self): self.assert_syntax_error(""" match ...: @@ -3272,6 +3270,7 @@ class TestSyntaxErrors(unittest.TestCase): pass """) + @disable # validation will be added when class patterns are added def test_mapping_pattern_duplicate_key_edge_case1(self): self.assert_syntax_error(""" match ...: @@ -3279,6 +3278,7 @@ class TestSyntaxErrors(unittest.TestCase): pass """) + @disable # validation will be added when class patterns are added def test_mapping_pattern_duplicate_key_edge_case2(self): self.assert_syntax_error(""" match ...: @@ -3286,6 +3286,7 @@ class TestSyntaxErrors(unittest.TestCase): pass """) + @disable # validation will be added when class patterns are added def test_mapping_pattern_duplicate_key_edge_case3(self): self.assert_syntax_error(""" match ...: |