[pypy-commit] pypy translation-cleanup: Simplify import handling and support relative imports

rlamy noreply at buildbot.pypy.org
Thu Aug 30 18:38:34 CEST 2012


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: translation-cleanup
Changeset: r57012:5a34835ef055
Date: 2012-08-23 05:55 +0100
http://bitbucket.org/pypy/pypy/changeset/5a34835ef055/

Log:	Simplify import handling and support relative imports

diff --git a/pypy/objspace/flow/flowcontext.py b/pypy/objspace/flow/flowcontext.py
--- a/pypy/objspace/flow/flowcontext.py
+++ b/pypy/objspace/flow/flowcontext.py
@@ -495,16 +495,10 @@
     def IMPORT_NAME(self, nameindex, next_instr):
         space = self.space
         modulename = self.getname_u(nameindex)
-        w_fromlist = self.popvalue()
-
+        glob = space.unwrap(self.w_globals)
+        fromlist = space.unwrap(self.popvalue())
         level = self.popvalue().value
-        if level != -1:
-            raise FlowingError("Relative imports are not implemented in RPython")
-
-        w_locals = space.w_None
-        w_modulename = space.wrap(modulename)
-        w_globals = self.w_globals
-        w_obj = space.import_name(w_modulename, w_globals, w_locals, w_fromlist)
+        w_obj = space.import_name(modulename, glob, None, fromlist, level)
         self.pushvalue(w_obj)
 
     def IMPORT_FROM(self, nameindex, next_instr):
diff --git a/pypy/objspace/flow/objspace.py b/pypy/objspace/flow/objspace.py
--- a/pypy/objspace/flow/objspace.py
+++ b/pypy/objspace/flow/objspace.py
@@ -405,16 +405,9 @@
         return self.do_operation_with_implicit_exceptions('getattr',
                 w_obj, w_name)
 
-    def import_name(self, w_name, w_glob, w_loc, w_frm):
-        if not isinstance(w_loc, Constant):
-            # import * in a function gives us the locals as Variable
-            # we always forbid it as a SyntaxError
-            raise SyntaxError, "RPython: import * is not allowed in functions"
-
-        name, glob, loc, frm = (self.unwrap(w_name), self.unwrap(w_glob),
-                                self.unwrap(w_loc), self.unwrap(w_frm))
+    def import_name(self, name, glob=None, loc=None, frm=None, level=-1):
         try:
-            mod = __import__(name, glob, loc, frm)
+            mod = __import__(name, glob, loc, frm, level)
         except ImportError, e:
             raise OperationError(self.w_ImportError, self.wrap(str(e)))
         return self.wrap(mod)
diff --git a/pypy/objspace/flow/specialcase.py b/pypy/objspace/flow/specialcase.py
--- a/pypy/objspace/flow/specialcase.py
+++ b/pypy/objspace/flow/specialcase.py
@@ -11,16 +11,8 @@
     args_w, kwds_w = args.unpack()
     assert kwds_w == {}, "should not call %r with keyword arguments" % (fn,)
     assert len(args_w) > 0 and len(args_w) <= 5, 'import needs 1 to 5 arguments'
-    w_name = args_w[0]
-    w_None = space.wrap(None)
-    w_glob, w_loc, w_frm = w_None, w_None, w_None
-    if len(args_w) > 1:
-        w_glob = args_w[1]
-    if len(args_w) > 2:
-        w_loc = args_w[2]
-    if len(args_w) > 3:
-        w_frm = args_w[3]
-    return space.import_name(w_name, w_glob, w_loc, w_frm)
+    args = [space.unwrap(arg) for arg in args_w]
+    return space.import_name(*args)
 
 def sc_operator(space, fn, args):
     args_w, kwds_w = args.unpack()
diff --git a/pypy/objspace/flow/test/test_objspace.py b/pypy/objspace/flow/test/test_objspace.py
--- a/pypy/objspace/flow/test/test_objspace.py
+++ b/pypy/objspace/flow/test/test_objspace.py
@@ -706,9 +706,7 @@
             from ..test.test_objspace import FlowObjSpace
         # Check that the function works in Python
         assert f() is None
-
-        with py.test.raises(error.FlowingError):
-            self.codetest(f)
+        self.codetest(f)
 
     def test_mergeable(self):
         def myfunc(x):


More information about the pypy-commit mailing list