summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorda-woods <dw-git@d-woods.co.uk>2022-07-31 19:59:56 +0100
committerda-woods <dw-git@d-woods.co.uk>2022-07-31 19:59:56 +0100
commit23972a4aeaab166a173797c08f371ada8231614d (patch)
tree15eb43d0a52fbd9acab1ba73ec09c15e98b83075
parent34b72dc6fc511c900d0e27c415550fd625c4d724 (diff)
downloadcython-23972a4aeaab166a173797c08f371ada8231614d.tar.gz
Moved "_compile" to shared test util
-rw-r--r--Cython/TestUtils.py24
-rw-r--r--tests/run/test_patma.py20
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):