Commit 66e47e8d authored by da-woods's avatar da-woods Committed by GitHub

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.
parent 3ff46855
...@@ -630,6 +630,9 @@ class InterpretCompilerDirectives(CythonTransform): ...@@ -630,6 +630,9 @@ class InterpretCompilerDirectives(CythonTransform):
- Command-line arguments overriding these - Command-line arguments overriding these
- @cython.directivename decorators - @cython.directivename decorators
- with cython.directivename: statements - 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 This transform is responsible for interpreting these various sources
and store the directive in two ways: and store the directive in two ways:
...@@ -846,6 +849,16 @@ class InterpretCompilerDirectives(CythonTransform): ...@@ -846,6 +849,16 @@ class InterpretCompilerDirectives(CythonTransform):
directive = self.directive_names.get(node.name) directive = self.directive_names.get(node.name)
if directive is not None: if directive is not None:
node.cython_attribute = directive 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 return node
def visit_NewExprNode(self, node): def visit_NewExprNode(self, node):
...@@ -3031,9 +3044,7 @@ class TransformBuiltinMethods(EnvTransform): ...@@ -3031,9 +3044,7 @@ class TransformBuiltinMethods(EnvTransform):
def visit_cython_attribute(self, node): def visit_cython_attribute(self, node):
attribute = node.as_cython_attribute() attribute = node.as_cython_attribute()
if attribute: if attribute:
if attribute == u'compiled': if attribute == u'__version__':
node = ExprNodes.BoolNode(node.pos, value=True)
elif attribute == u'__version__':
from .. import __version__ as version from .. import __version__ as version
node = ExprNodes.StringNode(node.pos, value=EncodedString(version)) node = ExprNodes.StringNode(node.pos, value=EncodedString(version))
elif attribute == u'NULL': elif attribute == u'NULL':
......
from libc.math cimport sin, cos, sqrt, tan, log
# 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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment