Commits

Christoph Reiter  committed 8e35187

More tests and fixes.

Also unskip test_import_logic, since that seems to pass.

  • Participants
  • Parent commits 09e0aaf
  • Branches cpyext-import-ex

Comments (0)

Files changed (4)

File pypy/module/cpyext/import_.py

     requested is normally the top-level package, unless a non-empty
     fromlist was given."""
     caller = space.getexecutioncontext().gettopframe_nohidden()
-    if caller is not None:
-        w_builtin = space.getitem(w_globals, space.wrap('__builtins__'))
-    else:
+    w_builtin = None
+    if caller is not None and w_globals is not None:
+        try:
+            w_builtin = space.getitem(w_globals, space.wrap('__builtins__'))
+        except OperationError, ex:
+            if not ex.match(space, space.w_KeyError):
+                raise
+
+    if w_builtin is None:
         w_builtin = space.getbuiltinmodule('__builtin__')
 
     w_name = space.wrap(rffi.charp2str(name))

File pypy/module/cpyext/test/test_cpyext.py

                     space.sys.get('modules'),
                     space.wrap(name))
             else:
-                return os.path.dirname(mod)
+                return space.wrap(os.path.dirname(mod))
 
         @unwrap_spec(mod=str, name=str)
         def reimport_module(space, mod, name):

File pypy/module/cpyext/test/test_import.py

 
 class AppTestImportLogic(AppTestCpythonExtensionBase):
     def test_import_logic(self):
-        skip("leak?")
         path = self.import_module(name='test_import_module', load_it=False)
         import sys
         sys.path.append(path)
         import test_import_module
         assert test_import_module.TEST is None
 
+    def test_import_module_ex(self):
+        path = self.import_module(name='test_import_c', load_it=False)
+        import sys
+        sys.path.append(path)
+        import test_import_c
+        assert test_import_c.TEST == "OK"

File pypy/module/cpyext/test/test_import_c.c

+#include "Python.h"
+
+void inittest_import_c(void)
+{
+     PyObject *from_list, *globals, *locals, *dict;
+     PyObject *module, *mod_name;
+
+     // PyImport_Import
+
+     mod_name = PyString_FromString ("os");
+     if(!mod_name)
+          return;
+     module = PyImport_Import (mod_name);
+     if (!module)
+          return;
+
+     Py_DECREF (module);
+     Py_DECREF (mod_name);
+
+     // PyImport_ImportModuleEx
+
+     from_list = Py_BuildValue ("(s)", "foobar");
+     globals = PyDict_New ();
+     locals = PyDict_New ();
+
+     if (!from_list || !globals || !locals)
+          return;
+
+     module = PyImport_ImportModuleEx ("os.path", NULL, NULL, NULL);
+     if (!module)
+          return;
+     Py_DECREF (module);
+
+     module = PyImport_ImportModuleEx ("os.path", globals, locals, from_list);
+     if (!module)
+          return;
+     Py_DECREF (module);
+
+     // PyImport_ImportModuleLevel
+
+     module = PyImport_ImportModuleLevel ("os.path", NULL, NULL, NULL, -1);
+     if (!module)
+          return;
+     Py_DECREF (module);
+
+     module = PyImport_ImportModuleLevel ("os.path", globals, locals,
+                                          from_list, 0);
+     if (!module)
+          return;
+     Py_DECREF (module);
+
+     Py_DECREF (from_list);
+     Py_DECREF (locals);
+     Py_DECREF (globals);
+
+     // signal the tests that everything passed
+     module = Py_InitModule("test_import", NULL);
+     if (!module)
+          return;
+     PyModule_AddStringConstant (module, "TEST", "OK");
+}