Author: Ronan Lamy <[email protected]>
Branch: py3.5
Changeset: r89667:00eea104b29e
Date: 2017-01-18 19:38 +0000
http://bitbucket.org/pypy/pypy/changeset/00eea104b29e/

Log:    Add C implementation (from CPython) for PyImport_ImportModuleLevel
        and PyImport_ImportModuleEx

diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -583,6 +583,8 @@
 
     'PyObject_AsReadBuffer', 'PyObject_AsWriteBuffer', 
'PyObject_CheckReadBuffer',
 
+    'PyImport_ImportModuleLevel',
+
     'PyOS_getsig', 'PyOS_setsig',
     'PyThread_get_thread_ident', 'PyThread_allocate_lock', 
'PyThread_free_lock',
     'PyThread_acquire_lock', 'PyThread_release_lock',
@@ -1296,6 +1298,7 @@
                          source_dir / "pymem.c",
                          source_dir / "bytesobject.c",
                          source_dir / "complexobject.c",
+                         source_dir / "import.c",
                          ]
 
 def build_eci(code, use_micronumpy=False, translating=False):
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
@@ -52,7 +52,7 @@
 
 
 @api_decl(
-    '''PyObject * PyImport_ImportModuleLevelObject(
+    '''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):
diff --git a/pypy/module/cpyext/include/import.h 
b/pypy/module/cpyext/include/import.h
--- a/pypy/module/cpyext/include/import.h
+++ b/pypy/module/cpyext/include/import.h
@@ -1,1 +1,26 @@
-/* empty */
+
+/* Module definition and import interface */
+
+#ifndef Py_IMPORT_H
+#define Py_IMPORT_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+PyAPI_FUNC(PyObject *) PyImport_ImportModuleLevel(
+    const char *name,           /* UTF-8 encoded string */
+    PyObject *globals,
+    PyObject *locals,
+    PyObject *fromlist,
+    int level
+    );
+
+#define PyImport_ImportModuleEx(n, g, l, f) \
+    PyImport_ImportModuleLevel(n, g, l, f, 0)
+
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* !Py_IMPORT_H */
diff --git a/pypy/module/cpyext/src/import.c b/pypy/module/cpyext/src/import.c
new file mode 100644
--- /dev/null
+++ b/pypy/module/cpyext/src/import.c
@@ -0,0 +1,15 @@
+#include <Python.h>
+
+PyObject *
+PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject 
*locals,
+                           PyObject *fromlist, int level)
+{
+    PyObject *nameobj, *mod;
+    nameobj = PyUnicode_FromString(name);
+    if (nameobj == NULL)
+        return NULL;
+    mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
+                                           fromlist, level);
+    Py_DECREF(nameobj);
+    return mod;
+}
diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -686,38 +686,6 @@
     NULL."""
     raise NotImplementedError
 
-@cpython_api([rffi.CCHARP, PyObject, PyObject, PyObject], PyObject)
-def PyImport_ImportModuleEx(space, name, globals, locals, fromlist):
-    """
-
-
-
-    Import a module.  This is best described by referring to the built-in 
Python
-    function __import__(), as the standard __import__() function calls
-    this function directly.
-
-    The return value is a new reference to the imported module or top-level
-    package, or NULL with an exception set on failure.  Like for
-    __import__(), the return value when a submodule of a package was
-    requested is normally the top-level package, unless a non-empty fromlist
-    was given.
-
-    Failing imports remove incomplete module objects, like with
-    PyImport_ImportModule()."""
-    raise NotImplementedError
-
-@cpython_api([rffi.CCHARP, PyObject, PyObject, PyObject, rffi.INT_real], 
PyObject)
-def PyImport_ImportModuleLevel(space, name, globals, locals, fromlist, level):
-    """Import a module.  This is best described by referring to the built-in 
Python
-    function __import__(), as the standard __import__() function calls
-    this function directly.
-
-    The return value is a new reference to the imported module or top-level 
package,
-    or NULL with an exception set on failure.  Like for __import__(),
-    the return value when a submodule of a package was requested is normally 
the
-    top-level package, unless a non-empty fromlist was given."""
-    raise NotImplementedError
-
 @cpython_api([rffi.CCHARP, PyObject, rffi.CCHARP, rffi.CCHARP], PyObject)
 def PyImport_ExecCodeModuleWithPathnames(space, name, co, pathname, cpathname):
     """Like PyImport_ExecCodeModuleEx(), but the __cached__
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to