[pypy-svn] r57035 - in pypy/branch/2.5-features/pypy: interpreter/pyparser module/__builtin__ module/__builtin__/test

bgola at codespeak.net bgola at codespeak.net
Wed Aug 6 18:38:40 CEST 2008


Author: bgola
Date: Wed Aug  6 18:38:39 2008
New Revision: 57035

Modified:
   pypy/branch/2.5-features/pypy/interpreter/pyparser/astbuilder.py
   pypy/branch/2.5-features/pypy/module/__builtin__/importing.py
   pypy/branch/2.5-features/pypy/module/__builtin__/test/test_import.py
Log:
PEP 328 (absolute/relative imports)

Modified: pypy/branch/2.5-features/pypy/interpreter/pyparser/astbuilder.py
==============================================================================
--- pypy/branch/2.5-features/pypy/interpreter/pyparser/astbuilder.py	(original)
+++ pypy/branch/2.5-features/pypy/interpreter/pyparser/astbuilder.py	Wed Aug  6 18:38:39 2008
@@ -845,7 +845,12 @@
     while atoms[index].name == builder.parser.tokens['DOT']:
         index += 1
     level = index - 1
-    incr, from_name = parse_dotted_names(atoms[index:], builder)
+    if atoms[index].value == 'import': 
+        # from . import x
+        from_name =  ""
+        incr = 0
+    else:
+        incr, from_name = parse_dotted_names(atoms[index:], builder)
     index += (incr + 1) # skip 'import'
     token = atoms[index]
     assert isinstance(token, TokenObject) # XXX

Modified: pypy/branch/2.5-features/pypy/module/__builtin__/importing.py
==============================================================================
--- pypy/branch/2.5-features/pypy/module/__builtin__/importing.py	(original)
+++ pypy/branch/2.5-features/pypy/module/__builtin__/importing.py	Wed Aug  6 18:38:39 2008
@@ -153,13 +153,13 @@
 
 def importhook(space, modulename, w_globals=None,
                w_locals=None, w_fromlist=None, w_level=-1):
-    if not modulename:
+    level = space.int_w(w_level)
+    if not modulename and level < 0: 
         raise OperationError(
             space.w_ValueError,
             space.wrap("Empty module name"))
     w = space.wrap
 
-    level = space.int_w(w_level)
 
     ctxt_name = None
     if w_globals is not None and not space.is_w(w_globals, space.w_None):
@@ -182,7 +182,12 @@
         else:
             ctxt_name_prefix_parts = ctxt_name.split('.')
             if ctxt_w_path is None: # context is a plain module
-                ctxt_name_prefix_parts = ctxt_name_prefix_parts[:-1]
+                if level < 0:
+                    ctxt_name_prefix_parts = ctxt_name_prefix_parts[:-1]
+                else:
+                    cnpp = ctxt_name_prefix_parts
+                    ctxt_name_prefix_parts = [ ctxt_name_prefix_parts[i] 
+                                               for i in range(len(cnpp)-level) ]
                 if ctxt_name_prefix_parts:
                     rel_modulename = '.'.join(ctxt_name_prefix_parts+[modulename])
             else: # context is a package module
@@ -200,7 +205,9 @@
                     return w_mod
             else:
                 rel_modulename = None
-
+    if level > 0:
+        msg = "Attempted relative import in non-package"
+        raise OperationError(space.w_ValueError, w(msg))
     w_mod = absolute_import(space, modulename, 0, w_fromlist, tentative=0)
     if rel_modulename is not None:
         space.setitem(space.sys.get('modules'), w(rel_modulename),space.w_None)

Modified: pypy/branch/2.5-features/pypy/module/__builtin__/test/test_import.py
==============================================================================
--- pypy/branch/2.5-features/pypy/module/__builtin__/test/test_import.py	(original)
+++ pypy/branch/2.5-features/pypy/module/__builtin__/test/test_import.py	Wed Aug  6 18:38:39 2008
@@ -42,8 +42,14 @@
              abs_x_y    = "import x.y",
              string     = "inpackage = 1",
              absolute   = "from __future__ import absolute_import\nimport string",
+             relative_b = "from __future__ import absolute_import\nfrom . import string",
+             relative_c = "from __future__ import absolute_import\nfrom .string import inpackage",
+             )
+    setuppkg("pkg.pkg1", 
+             a          = '',
+             relative_d = "from __future__ import absolute_import\nfrom ..string import inpackage",
+             relative_e = "from __future__ import absolute_import\nfrom .. import string",
              )
-    setuppkg("pkg.pkg1", a='')
     setuppkg("pkg.pkg2", a='', b='')
     setuppkg("pkg_r", inpkg = "import x.y")
     setuppkg("pkg_r.x")
@@ -277,6 +283,27 @@
             absolute.string.inpackage
         raises(AttributeError, imp)
 
+    def test_future_relative_import_without_from_name(self):
+        from pkg import relative_b
+        assert relative_b.string.inpackage == 1
+
+    def test_future_relative_import_level_1(self):
+        from pkg import relative_c
+        assert relative_c.inpackage == 1
+    
+    def test_future_relative_import_level_2(self):
+        from pkg.pkg1 import relative_d
+        assert relative_d.inpackage == 1
+
+    def test_future_relative_import_level_2_without_from_name(self):
+        from pkg.pkg1 import relative_e
+        assert relative_e.string.inpackage == 1
+
+    def test_future_relative_import_error_when_in_non_package(self):
+        def imp():
+            from .string import inpackage
+        raises(ValueError, imp)
+
 
 def _getlong(data):
     x = marshal.dumps(data)



More information about the Pypy-commit mailing list