diff options
| author | da-woods <dw-git@d-woods.co.uk> | 2020-04-13 21:05:26 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-13 22:05:26 +0200 |
| commit | 66e47e8daeb38877b7d3a137fd574f3326e067b9 (patch) | |
| tree | b402caa633c9839cd4d6dab08e6b981e3ce28e41 | |
| parent | 3ff468555338e203397c52b43a1f64627ad3443e (diff) | |
| download | cython-66e47e8daeb38877b7d3a137fd574f3326e067b9.tar.gz | |
Drop unused code-paths associated with "if cython.compiled" early (GH-3507)
This allows things to work like:
# in pxd file
from libc.math cimport sin
# in py file
if not cython.compiled:
from math import sin # previously failed with cython compile
# error because it was assigning to a cdef name
This seems worthwhile because it makes it easier to write code
that re-assigns cdef names and so works in both modes.
| -rw-r--r-- | Cython/Compiler/ParseTreeTransforms.py | 17 | ||||
| -rw-r--r-- | tests/compile/cython_compiled_folding.pxd | 1 | ||||
| -rw-r--r-- | tests/compile/cython_compiled_folding.py | 39 |
3 files changed, 54 insertions, 3 deletions
diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py index 608fe079b..0add92ca6 100644 --- a/Cython/Compiler/ParseTreeTransforms.py +++ b/Cython/Compiler/ParseTreeTransforms.py @@ -630,6 +630,9 @@ class InterpretCompilerDirectives(CythonTransform): - Command-line arguments overriding these - @cython.directivename decorators - with cython.directivename: statements + - replaces "cython.compiled" with BoolNode(value=True) + allowing unreachable blocks to be removed at a fairly early stage + before cython typing rules are forced on applied This transform is responsible for interpreting these various sources and store the directive in two ways: @@ -846,6 +849,16 @@ class InterpretCompilerDirectives(CythonTransform): directive = self.directive_names.get(node.name) if directive is not None: node.cython_attribute = directive + if node.as_cython_attribute() == "compiled": + return ExprNodes.BoolNode(node.pos, value=True) # replace early so unused branches can be dropped + # before they have a chance to cause compile-errors + return node + + def visit_AttributeNode(self, node): + self.visitchildren(node) + if node.as_cython_attribute() == "compiled": + return ExprNodes.BoolNode(node.pos, value=True) # replace early so unused branches can be dropped + # before they have a chance to cause compile-errors return node def visit_NewExprNode(self, node): @@ -3031,9 +3044,7 @@ class TransformBuiltinMethods(EnvTransform): def visit_cython_attribute(self, node): attribute = node.as_cython_attribute() if attribute: - if attribute == u'compiled': - node = ExprNodes.BoolNode(node.pos, value=True) - elif attribute == u'__version__': + if attribute == u'__version__': from .. import __version__ as version node = ExprNodes.StringNode(node.pos, value=EncodedString(version)) elif attribute == u'NULL': diff --git a/tests/compile/cython_compiled_folding.pxd b/tests/compile/cython_compiled_folding.pxd new file mode 100644 index 000000000..4c85d4311 --- /dev/null +++ b/tests/compile/cython_compiled_folding.pxd @@ -0,0 +1 @@ +from libc.math cimport sin, cos, sqrt, tan, log diff --git a/tests/compile/cython_compiled_folding.py b/tests/compile/cython_compiled_folding.py new file mode 100644 index 000000000..047b61689 --- /dev/null +++ b/tests/compile/cython_compiled_folding.py @@ -0,0 +1,39 @@ +# mode: compile + +# libc sin, cos and sqrt cimported in the pxd file + +import cython +from cython import compiled + +if not cython.compiled: + from math import sin + +if cython.compiled: + pass +else: + from math import cos + +if "aa" == "bb": + pass +elif cython.compiled: + pass +elif True: + from math import sqrt + +if "aa" == "bb": + pass +elif compiled: + pass +else: + from math import tan + +# log10 isn't defined in the pxd file +from math import log10 + +@cython.test_fail_if_path_exists("//FromImportStatNode//ImportNode") +@cython.test_assert_path_exists("//AddNode") +def import_log(x, y): + if compiled: + return x+y + else: + from math import log |
