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

bgola at codespeak.net bgola at codespeak.net
Mon Aug 4 20:08:36 CEST 2008


Author: bgola
Date: Mon Aug  4 20:08:35 2008
New Revision: 56980

Modified:
   pypy/branch/2.5-features/pypy/interpreter/astcompiler/ast.py
   pypy/branch/2.5-features/pypy/interpreter/astcompiler/consts.py
   pypy/branch/2.5-features/pypy/interpreter/astcompiler/future.py
   pypy/branch/2.5-features/pypy/interpreter/astcompiler/pycodegen.py
   pypy/branch/2.5-features/pypy/interpreter/pyparser/astbuilder.py
   pypy/branch/2.5-features/pypy/interpreter/pyparser/future.py
   pypy/branch/2.5-features/pypy/interpreter/pyparser/test/test_futureautomaton.py
   pypy/branch/2.5-features/pypy/module/__builtin__/importing.py
Log:
absolute import support. astbuilder and astcompiler already done, missing the new import behavior (when __future__.asbolute_import) in importing.py

Modified: pypy/branch/2.5-features/pypy/interpreter/astcompiler/ast.py
==============================================================================
--- pypy/branch/2.5-features/pypy/interpreter/astcompiler/ast.py	(original)
+++ pypy/branch/2.5-features/pypy/interpreter/astcompiler/ast.py	Mon Aug  4 20:08:35 2008
@@ -2477,10 +2477,11 @@
 For.typedef.acceptable_as_base_class = False
 
 class From(Node):
-    def __init__(self, modname, names, lineno=-1):
+    def __init__(self, modname, names, level, lineno=-1):
         Node.__init__(self, lineno)
         self.modname = modname
         self.names = names
+        self.level = level
 
     def getChildren(self):
         "NOT_RPYTHON"

Modified: pypy/branch/2.5-features/pypy/interpreter/astcompiler/consts.py
==============================================================================
--- pypy/branch/2.5-features/pypy/interpreter/astcompiler/consts.py	(original)
+++ pypy/branch/2.5-features/pypy/interpreter/astcompiler/consts.py	Mon Aug  4 20:08:35 2008
@@ -19,4 +19,5 @@
 CO_GENERATOR = 0x0020
 CO_GENERATOR_ALLOWED = 0x1000
 CO_FUTURE_DIVISION = 0x2000
+CO_FUTURE_ABSIMPORT = 0x4000
 CO_FUTURE_WITH_STATEMENT = 0x8000

Modified: pypy/branch/2.5-features/pypy/interpreter/astcompiler/future.py
==============================================================================
--- pypy/branch/2.5-features/pypy/interpreter/astcompiler/future.py	(original)
+++ pypy/branch/2.5-features/pypy/interpreter/astcompiler/future.py	Mon Aug  4 20:08:35 2008
@@ -15,7 +15,7 @@
 
 class FutureParser(ast.ASTVisitor):
 
-    features = ("nested_scopes", "generators", "division", "with_statement")
+    features = ("nested_scopes", "generators", "division", "with_statement", "absolute_import")
 
     def __init__(self):
         self.found = {} # set

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	Mon Aug  4 20:08:35 2008
@@ -10,7 +10,7 @@
     SC_FREE, SC_CELL, SC_DEFAULT, OP_APPLY, OP_ASSIGN, OP_DELETE, OP_NONE
 from pypy.interpreter.astcompiler.consts import CO_VARARGS, CO_VARKEYWORDS, \
     CO_NEWLOCALS, CO_NESTED, CO_GENERATOR, CO_GENERATOR_ALLOWED, \
-    CO_FUTURE_DIVISION, CO_FUTURE_WITH_STATEMENT
+    CO_FUTURE_DIVISION, CO_FUTURE_WITH_STATEMENT, CO_FUTURE_ABSIMPORT
 from pypy.interpreter.pyparser.error import SyntaxError
 from pypy.interpreter.astcompiler.opt import is_constant_false
 from pypy.interpreter.astcompiler.opt import is_constant_true
@@ -150,6 +150,8 @@
                 self.graph.setFlag(CO_GENERATOR_ALLOWED)
             elif feature == "with_statement":
                 self.graph.setFlag(CO_FUTURE_WITH_STATEMENT)
+            elif feature == "absolute_import":
+                self.graph.setFlag(CO_FUTURE_ABSIMPORT)
 
     def emit(self, inst ):
         return self.graph.emit( inst )
@@ -865,7 +867,7 @@
     def visitFrom(self, node):
         self.set_lineno(node)
         fromlist = [ self.space.wrap(name) 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(node.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/pyparser/astbuilder.py
==============================================================================
--- pypy/branch/2.5-features/pypy/interpreter/pyparser/astbuilder.py	(original)
+++ pypy/branch/2.5-features/pypy/interpreter/pyparser/astbuilder.py	Mon Aug  4 20:08:35 2008
@@ -841,8 +841,11 @@
     import_as_name: NAME [NAME NAME]
     """
     atoms = get_atoms(builder, nb)
-
-    index = 1
+    index = 1 # skip from
+    level = 0
+    while atoms[index].name == builder.parser.tokens['DOT']:
+        level += 1
+        index += 1
     incr, from_name = parse_dotted_names(atoms[index:], builder)
     index += (incr + 1) # skip 'import'
     token = atoms[index]
@@ -879,7 +882,9 @@
             names.append((name, as_name))
             if index < l: # case ','
                 index += 1
-    builder.push(ast.From(from_name, names, atoms[0].lineno))
+    if level == 0:
+        level = -1
+    builder.push(ast.From(from_name, names, level, atoms[0].lineno))
 
 
 def build_yield_stmt(builder, nb):

Modified: pypy/branch/2.5-features/pypy/interpreter/pyparser/future.py
==============================================================================
--- pypy/branch/2.5-features/pypy/interpreter/pyparser/future.py	(original)
+++ pypy/branch/2.5-features/pypy/interpreter/pyparser/future.py	Mon Aug  4 20:08:35 2008
@@ -25,7 +25,7 @@
 """
 
 from pypy.interpreter.astcompiler.consts import CO_GENERATOR_ALLOWED, \
-    CO_FUTURE_DIVISION, CO_FUTURE_WITH_STATEMENT
+    CO_FUTURE_DIVISION, CO_FUTURE_WITH_STATEMENT, CO_FUTURE_ABSIMPORT
             
 def getFutures(futureFlags, source):
     futures = FutureAutomaton(futureFlags, source)
@@ -53,7 +53,7 @@
         * other future statements.
 
     The features recognized by Python 2.5 are "generators",
-    "division", "nested_scopes" and "with_statement".
+    "division", "nested_scopes" and "with_statement", "absolute_import".
     "generators", "division" and "nested_scopes" are redundant
     in 2.5 because they are always enabled.
 

Modified: pypy/branch/2.5-features/pypy/interpreter/pyparser/test/test_futureautomaton.py
==============================================================================
--- pypy/branch/2.5-features/pypy/interpreter/pyparser/test/test_futureautomaton.py	(original)
+++ pypy/branch/2.5-features/pypy/interpreter/pyparser/test/test_futureautomaton.py	Mon Aug  4 20:08:35 2008
@@ -141,4 +141,10 @@
     assert f.pos == len(s)
     assert f.flags == 0
 
-    
+def test_from_import_abs_import():
+    s = 'from  __future__ import absolute_import\n'
+    f = run(s)
+    assert f.pos == len(s)
+    assert f.flags == fut.CO_FUTURE_ABSIMPORT
+
+

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	Mon Aug  4 20:08:35 2008
@@ -152,13 +152,13 @@
     return None
 
 def importhook(space, modulename, w_globals=None,
-               w_locals=None, w_fromlist=None):
+               w_locals=None, w_fromlist=None, w_level=-1):
     if not modulename:
         raise OperationError(
             space.w_ValueError,
             space.wrap("Empty module name"))
     w = space.wrap
-
+    
     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__'))
@@ -200,7 +200,7 @@
         space.setitem(space.sys.get('modules'), w(rel_modulename),space.w_None)
     return w_mod
 #
-importhook.unwrap_spec = [ObjSpace,str,W_Root,W_Root,W_Root]
+importhook.unwrap_spec = [ObjSpace,str,W_Root,W_Root,W_Root,W_Root]
 
 def absolute_import(space, modulename, baselevel, w_fromlist, tentative):
     lock = getimportlock(space)



More information about the Pypy-commit mailing list