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

bgola at codespeak.net bgola at codespeak.net
Wed Aug 6 16:53:53 CEST 2008


Author: bgola
Date: Wed Aug  6 16:53:52 2008
New Revision: 57029

Modified:
   pypy/branch/2.5-features/pypy/interpreter/astcompiler/ast.txt
   pypy/branch/2.5-features/pypy/interpreter/astcompiler/pycodegen.py
   pypy/branch/2.5-features/pypy/interpreter/pyopcode.py
   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:
absolute import working (__future__.absolute_import)

Modified: pypy/branch/2.5-features/pypy/interpreter/astcompiler/ast.txt
==============================================================================
--- pypy/branch/2.5-features/pypy/interpreter/astcompiler/ast.txt	(original)
+++ pypy/branch/2.5-features/pypy/interpreter/astcompiler/ast.txt	Wed Aug  6 16:53:52 2008
@@ -31,7 +31,7 @@
 While: test, body, else_&
 If: tests!, else_&
 Exec: expr, locals&, globals&
-From: modname*str, names*
+From: modname*str, names*, level*int
 Import: names*
 Raise: expr1&, expr2&, expr3&
 TryFinally: body, final

Modified: pypy/branch/2.5-features/pypy/interpreter/astcompiler/pycodegen.py
==============================================================================
--- pypy/branch/2.5-features/pypy/interpreter/astcompiler/pycodegen.py	(original)
+++ pypy/branch/2.5-features/pypy/interpreter/astcompiler/pycodegen.py	Wed Aug  6 16:53:52 2008
@@ -670,7 +670,7 @@
         stack = []
         i = 0
         for for_ in node.quals:
-            assert isinstance(for_, ast.GenExprFor)            
+            assert isinstance(for_, ast.GenExprFor)
             start, anchor = self._visitGenExprFor(for_)
             self.genexpr_cont_stack.append( None )
             for if_ in for_.ifs:
@@ -853,8 +853,12 @@
 
     def visitImport(self, node):
         self.set_lineno(node)
+        if self.graph.checkFlag(CO_FUTURE_ABSIMPORT):
+            level = 0
+        else:
+            level = -1
         for name, alias in node.names:
-            self.emitop_obj('LOAD_CONST', self.space.wrap(-1)) # 2.5 flag
+            self.emitop_obj('LOAD_CONST', self.space.wrap(level)) # 2.5 flag
             self.emitop_obj('LOAD_CONST', self.space.w_None)
             self.emitop('IMPORT_NAME', name)
             mod = name.split(".")[0]
@@ -866,8 +870,11 @@
 
     def visitFrom(self, node):
         self.set_lineno(node)
+        level = node.level
+        if level == 0 and not self.graph.checkFlag(CO_FUTURE_ABSIMPORT):
+            level = -1
         fromlist = [ self.space.wrap(name) for name,alias in node.names ]
-        self.emitop_obj('LOAD_CONST', self.space.wrap(node.level)) # 2.5 flag
+        self.emitop_obj('LOAD_CONST', self.space.wrap(level)) # 2.5 flag
         self.emitop_obj('LOAD_CONST', self.space.newtuple(fromlist))
         self.emitop('IMPORT_NAME', node.modname)
         for name, alias in node.names:

Modified: pypy/branch/2.5-features/pypy/interpreter/pyopcode.py
==============================================================================
--- pypy/branch/2.5-features/pypy/interpreter/pyopcode.py	(original)
+++ pypy/branch/2.5-features/pypy/interpreter/pyopcode.py	Wed Aug  6 16:53:52 2008
@@ -727,17 +727,9 @@
         modulename = f.space.str_w(w_modulename)
         w_fromlist = f.popvalue()
 
-        # CPython 2.5 adds an obscure extra flag consumed by this opcode
+        # CPython 2.5 adds an extra argument consumed by this opcode
         if f.pycode.magic >= 0xa0df294:
             w_flag = f.popvalue()
-            try:
-                if space.int_w(w_flag) == -1:
-                    w_flag = None     # don't provide the extra flag if == -1
-            except OperationError, e:
-                # let SystemExit and KeyboardInterrupt go through
-                if e.async(space):
-                    raise
-                # ignore other exceptions
         else:
             w_flag = None
 
@@ -750,12 +742,8 @@
             w_locals = space.w_None
         w_modulename = space.wrap(modulename)
         w_globals = f.w_globals
-        if w_flag is None:
-            w_obj = space.call_function(w_import, w_modulename, w_globals,
-                                        w_locals, w_fromlist)
-        else:
-            w_obj = space.call_function(w_import, w_modulename, w_globals,
-                                        w_locals, w_fromlist, w_flag)
+        w_obj = space.call_function(w_import, w_modulename, w_globals,
+                                    w_locals, w_fromlist, w_flag)
         f.pushvalue(w_obj)
 
     def IMPORT_STAR(f, *ignored):

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 16:53:52 2008
@@ -842,10 +842,9 @@
     """
     atoms = get_atoms(builder, nb)
     index = 1 # skip from
-    level = 0
     while atoms[index].name == builder.parser.tokens['DOT']:
-        level += 1
         index += 1
+    level = index - 1
     incr, from_name = parse_dotted_names(atoms[index:], builder)
     index += (incr + 1) # skip 'import'
     token = atoms[index]
@@ -882,8 +881,6 @@
             names.append((name, as_name))
             if index < l: # case ','
                 index += 1
-    if level == 0:
-        level = -1
     builder.push(ast.From(from_name, names, level, atoms[0].lineno))
 
 

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 16:53:52 2008
@@ -158,7 +158,9 @@
             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):
         ctxt_w_name = try_getitem(space, w_globals, w('__name__'))
@@ -174,21 +176,25 @@
 
     rel_modulename = None
     if ctxt_name is not None:
-
-        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 ctxt_name_prefix_parts:
-                rel_modulename = '.'.join(ctxt_name_prefix_parts+[modulename])
-        else: # context is a package module
-            rel_modulename = ctxt_name+'.'+modulename
+        if level == 0:
+            baselevel = 0
+            rel_modulename = modulename
+        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 ctxt_name_prefix_parts:
+                    rel_modulename = '.'.join(ctxt_name_prefix_parts+[modulename])
+            else: # context is a package module
+                rel_modulename = ctxt_name+'.'+modulename
+            baselevel = len(ctxt_name_prefix_parts)
         if rel_modulename is not None:
             w_mod = check_sys_modules(space, w(rel_modulename))
             if (w_mod is None or
                 not space.is_w(w_mod, space.w_None)):
                 
                 w_mod = absolute_import(space, rel_modulename,
-                                        len(ctxt_name_prefix_parts),
+                                        baselevel,
                                         w_fromlist, tentative=1)
                 if w_mod is not None:
                     return w_mod

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 16:53:52 2008
@@ -40,6 +40,8 @@
              relative_a = "import a",
              abs_b      = "import b",
              abs_x_y    = "import x.y",
+             string     = "inpackage = 1",
+             absolute   = "from __future__ import absolute_import\nimport string",
              )
     setuppkg("pkg.pkg1", a='')
     setuppkg("pkg.pkg2", a='', b='')
@@ -269,6 +271,13 @@
         import sys
         assert glob['sys'] is sys
 
+    def test_future_absolute_import(self):
+        def imp():
+            from pkg import absolute
+            absolute.string.inpackage
+        raises(AttributeError, imp)
+
+
 def _getlong(data):
     x = marshal.dumps(data)
     return x[-4:]



More information about the Pypy-commit mailing list