diff options
-rw-r--r-- | Cython/TestUtils.py | 24 | ||||
-rw-r--r-- | tests/run/test_patma.py | 20 |
2 files changed, 25 insertions, 19 deletions
diff --git a/Cython/TestUtils.py b/Cython/TestUtils.py index bb2070d39..528a828c7 100644 --- a/Cython/TestUtils.py +++ b/Cython/TestUtils.py @@ -10,9 +10,10 @@ from io import open from .Compiler import Errors from .CodeWriter import CodeWriter -from .Compiler.TreeFragment import TreeFragment, strip_common_indent +from .Compiler.TreeFragment import TreeFragment, strip_common_indent, StringParseContext from .Compiler.Visitor import TreeVisitor, VisitorTransform from .Compiler import TreePath +from .Compiler.ParseTreeTransforms import PostParse class NodeTypeWriter(TreeVisitor): @@ -272,3 +273,24 @@ def write_newer_file(file_path, newer_than, content, dedent=False, encoding=None while other_time is None or other_time >= os.path.getmtime(file_path): write_file(file_path, content, dedent=dedent, encoding=encoding) + + +def py_parse_code(code): + """ + Compiles code far enough to get errors from the parser and post-parse stage. + + Is useful for checking for syntax errors, however it doesn't generate runable + code. + """ + context = StringParseContext("test") + # all the errors we care about are in the parsing or postparse stage + try: + with Errors.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 Errors.CompileError as e: + raise SyntaxError(e.message_only) diff --git a/tests/run/test_patma.py b/tests/run/test_patma.py index 61ac6957f..e55827f35 100644 --- a/tests/run/test_patma.py +++ b/tests/run/test_patma.py @@ -4,29 +4,13 @@ # new code import cython -from Cython.Compiler.TreeFragment import TreeFragment, StringParseContext -from Cython.Compiler.Errors import local_errors, CompileError -from Cython.Compiler.ParseTreeTransforms import PostParse - -def _compile(code): - 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) +from Cython.TestUtils import py_parse_code if cython.compiled: def compile(code, name, what): assert what == 'exec' - _compile(code) + py_parse_code(code) def disable(func): |