add py2_import directive to control import level
authorHaoyu Bai <baihaoyu@gmail.com>
Fri, 1 Apr 2011 17:02:00 +0000 (01:02 +0800)
committerRobert Bradshaw <robertwb@math.washington.edu>
Sat, 16 Apr 2011 07:36:29 +0000 (00:36 -0700)
Cython/Compiler/ExprNodes.py
Cython/Compiler/Options.py
Cython/Compiler/Parsing.py
tests/run/relativeimport_T542.pyx

index 1a1c11e24606bc1e7b69bb642cb6dda7188ea5af..db4e69932debf764f7266d37c896c9db757f54ab 100755 (executable)
@@ -1749,12 +1749,19 @@ class ImportNode(ExprNode):
     #                        0: absolute import;
     #                       >0: the number of parent directories to search
     #                           relative to the current module.
+    #                     None: decide the level according to language level and
+    #                           directives
 
     type = py_object_type
 
     subexprs = ['module_name', 'name_list']
 
     def analyse_types(self, env):
+        if self.level is None:
+            if env.directives['language_level'] < 3 or env.directives['py2_import']:
+                self.level = -1
+            else:
+                self.level = 0
         self.module_name.analyse_types(env)
         self.module_name = self.module_name.coerce_to_pyobject(env)
         if self.name_list:
index 2e3663aebbf84ad44cbdfcab646b8e2f36d6b48c..e4aea76c8256c3bc4c0da15c02fb23dfca1ef17c 100644 (file)
@@ -81,6 +81,7 @@ directive_defaults = {
     'autotestdict.all': False,
     'language_level': 2,
     'fast_getattr': False, # Undocumented until we come up with a better way to handle this everywhere.
+    'py2_import': False, # For backward compatibility of Cython's source code
 
     'warn': None,
     'warn.undeclared': False,
index 910e62a950e5b0a7b0e57ea2a63a47f7a0e6ca3b..6aab2f84438ce3607b7e7487dc7a775838a18777 100644 (file)
@@ -1189,11 +1189,6 @@ def p_raise_statement(s):
         return Nodes.ReraiseStatNode(pos)
 
 def p_import_statement(s):
-    # will do absolute import in Py3 and try both relative and absolute in Py2.
-    if s.context.language_level >= 3:
-        level = 0
-    else:
-        level = -1
     # s.sy in ('import', 'cimport')
     pos = s.position()
     kind = s.sy
@@ -1221,7 +1216,7 @@ def p_import_statement(s):
                 rhs = ExprNodes.ImportNode(pos,
                     module_name = ExprNodes.IdentifierStringNode(
                         pos, value = dotted_name),
-                    level = level,
+                    level = None,
                     name_list = name_list))
         stats.append(stat)
     return Nodes.StatListNode(pos, stats = stats)
@@ -1236,18 +1231,16 @@ def p_from_import_statement(s, first_statement = 0):
         while s.sy == '.':
             level += 1
             s.next()
+        if s.sy == 'cimport':
+            s.error("Relative cimport is not supported yet")
     else:
-        # will do absolute import in Py3 and try both relative and absolute in Py2.
-        if s.context.language_level >= 3:
-            level = 0
-        else:
-            level = -1
-
-    if level > 0 and s.sy == 'cimport':
-        s.error("Relative cimport is not supported yet")
-    if level > 0 and s.sy == 'import':
+        level = None
+    if level is not None and s.sy == 'import':
         # we are dealing with "from .. import foo, bar"
         dotted_name_pos, dotted_name = s.position(), ''
+    elif level is not None and s.sy == 'cimport':
+        # "from .. cimport"
+        s.error("Relative cimport is not supported yet")
     else:
         (dotted_name_pos, _, dotted_name, _) = \
             p_dotted_name(s, as_allowed = 0)
index 2994cc40f4061382ed554339bbf2dcc9fafdc9b7..c292cc60612b2c966bf6a514be75e491b0f3c3fc 100644 (file)
@@ -1,5 +1,10 @@
 # cython: language_level=3
-__name__='distutils.baregg' # fool Python we are in distutils
+import sys
+# fool Python we are in distutils
+if sys.version_info >= (3,):
+    __package__='distutils'
+else:
+    __package__=b'distutils'
 from distutils import cmd, core, version
 
 from .core import *