[pypy-commit] pypy py3.5: Implement PyImport_ImportModuleLevelObject

rlamy pypy.commits at gmail.com
Wed Jan 18 13:57:01 EST 2017


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: py3.5
Changeset: r89665:5e0d28baa9d9
Date: 2017-01-18 18:53 +0000
http://bitbucket.org/pypy/pypy/changeset/5e0d28baa9d9/

Log:	Implement PyImport_ImportModuleLevelObject

diff --git a/pypy/module/cpyext/import_.py b/pypy/module/cpyext/import_.py
--- a/pypy/module/cpyext/import_.py
+++ b/pypy/module/cpyext/import_.py
@@ -1,8 +1,8 @@
-from pypy.interpreter import module
 from pypy.module.cpyext.api import (
-    generic_cpy_call, cpython_api, PyObject, CONST_STRING, CANNOT_FAIL)
+    cpython_api, PyObject, CONST_STRING, CANNOT_FAIL,
+    cts, api_decl)
 from rpython.rtyper.lltypesystem import lltype, rffi
-from pypy.interpreter.error import OperationError
+from pypy.interpreter.error import OperationError, oefmt
 from pypy.interpreter.module import Module
 from pypy.interpreter.pycode import PyCode
 from pypy.module.imp import importing
@@ -50,6 +50,28 @@
         space.w_RuntimeWarning)
     return PyImport_Import(space, space.wrap(rffi.charp2str(name)))
 
+
+ at api_decl(
+    '''PyObject * PyImport_ImportModuleLevelObject(
+        PyObject *name, PyObject *given_globals, PyObject *locals,
+        PyObject *given_fromlist, int level)''', cts)
+def PyImport_ImportModuleLevelObject(space, w_name, w_glob, w_loc, w_fromlist, level):
+    if w_glob is None:
+        w_glob = space.newdict()
+    else:
+        if level > 0 and not space.isinstance_w(w_glob, space.w_dict):
+            raise oefmt(space.w_TypeError, "globals must be a dict")
+    if w_fromlist is None:
+        w_fromlist = space.newlist([])
+    if w_name is None:
+        raise oefmt(space.w_ValueError, "Empty module name")
+    w_import = space.builtin.get('__import__')
+    if level < 0:
+        raise oefmt(space.w_ValueError, "level must be >= 0")
+    return space.call_function(
+        w_import, w_name, w_glob, w_loc, w_fromlist, space.newint(level))
+
+
 @cpython_api([PyObject], PyObject)
 def PyImport_ReloadModule(space, w_mod):
     w_import = space.builtin.get('__import__')
diff --git a/pypy/module/cpyext/test/test_import.py b/pypy/module/cpyext/test/test_import.py
--- a/pypy/module/cpyext/test/test_import.py
+++ b/pypy/module/cpyext/test/test_import.py
@@ -40,6 +40,12 @@
         stat = PyImport_ReloadModule(space, stat)
         assert space.getattr(stat, space.wrap("S_IMODE"))
 
+    def test_ImportModuleLevelObject(self, space):
+        w_mod = PyImport_ImportModuleLevelObject(
+            space, space.wrap('stat'), None, None, None, 0)
+        assert w_mod
+        assert space.getattr(w_mod, space.wrap("S_IMODE"))
+
     def test_lock(self, space):
         # "does not crash"
         _PyImport_AcquireLock(space, )


More information about the pypy-commit mailing list