Commits

Ronan Lamy  committed 5a34835

Simplify import handling and support relative imports

  • Participants
  • Parent commits 1f98e87
  • Branches translation-cleanup

Comments (0)

Files changed (4)

File pypy/objspace/flow/flowcontext.py

     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):

File pypy/objspace/flow/objspace.py

         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)

File pypy/objspace/flow/specialcase.py

     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()

File pypy/objspace/flow/test/test_objspace.py

             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):