[pypy-svn] pypy default: Extract and test the functionality of getting an importer.
arigo
commits-noreply at bitbucket.org
Mon Feb 14 21:59:01 CET 2011
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r41927:ed3ce9c8044b
Date: 2011-02-14 19:53 +0100
http://bitbucket.org/pypy/pypy/changeset/ed3ce9c8044b/
Log: Extract and test the functionality of getting an importer. Expose it
to app-level under a pypy-specific name.
diff --git a/pypy/module/imp/__init__.py b/pypy/module/imp/__init__.py
--- a/pypy/module/imp/__init__.py
+++ b/pypy/module/imp/__init__.py
@@ -19,7 +19,8 @@
'load_module': 'interp_imp.load_module',
'load_source': 'interp_imp.load_source',
'load_compiled': 'interp_imp.load_compiled',
- '_run_compiled_module': 'interp_imp._run_compiled_module',
+ '_run_compiled_module': 'interp_imp._run_compiled_module', # pypy
+ '_getimporter': 'importing._getimporter', # pypy
#'run_module': 'interp_imp.run_module',
'new_module': 'interp_imp.new_module',
'init_builtin': 'interp_imp.init_builtin',
diff --git a/pypy/module/imp/test/test_import.py b/pypy/module/imp/test/test_import.py
--- a/pypy/module/imp/test/test_import.py
+++ b/pypy/module/imp/test/test_import.py
@@ -975,6 +975,12 @@
sys.meta_path.pop()
sys.path_hooks.pop()
+
+class AppTestPyPyExtension(object):
+ def setup_class(cls):
+ cls.space = gettestobjspace(usemodules=['imp', 'zipimport'])
+ cls.w_udir = cls.space.wrap(str(udir))
+
def test_run_compiled_module(self):
# XXX minimal test only
import imp, new
@@ -982,6 +988,37 @@
raises(IOError, imp._run_compiled_module,
'foobar', 'this_file_does_not_exist', None, module)
+ def test_getimporter(self):
+ import imp, os
+ # an existing directory
+ importer = imp._getimporter(self.udir)
+ assert importer is None
+ # an existing file
+ path = os.path.join(self.udir, 'test_getimporter')
+ open(path, 'w').close()
+ importer = imp._getimporter(path)
+ assert isinstance(importer, imp.NullImporter)
+ # a non-existing path
+ path = os.path.join(self.udir, 'does_not_exist_at_all')
+ importer = imp._getimporter(path)
+ assert isinstance(importer, imp.NullImporter)
+ # a mostly-empty zip file
+ path = os.path.join(self.udir, 'test_getimporter.zip')
+ f = open(path, 'wb')
+ f.write('PK\x03\x04\n\x00\x00\x00\x00\x00P\x9eN>\x00\x00\x00\x00\x00'
+ '\x00\x00\x00\x00\x00\x00\x00\x05\x00\x15\x00emptyUT\t\x00'
+ '\x03wyYMwyYMUx\x04\x00\xf4\x01d\x00PK\x01\x02\x17\x03\n\x00'
+ '\x00\x00\x00\x00P\x9eN>\x00\x00\x00\x00\x00\x00\x00\x00\x00'
+ '\x00\x00\x00\x05\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00'
+ '\xa4\x81\x00\x00\x00\x00emptyUT\x05\x00\x03wyYMUx\x00\x00PK'
+ '\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00@\x00\x00\x008\x00'
+ '\x00\x00\x00\x00')
+ f.close()
+ importer = imp._getimporter(path)
+ import zipimport
+ assert isinstance(importer, zipimport.zipimporter)
+
+
class AppTestNoPycFile(object):
spaceconfig = {
"objspace.usepycfiles": False,
diff --git a/pypy/module/imp/importing.py b/pypy/module/imp/importing.py
--- a/pypy/module/imp/importing.py
+++ b/pypy/module/imp/importing.py
@@ -291,7 +291,8 @@
if space.is_true(w_loader):
return w_loader
-def find_in_path_hooks(space, w_modulename, w_pathitem):
+def _getimporter(space, w_pathitem):
+ # the function 'imp._getimporter' is a pypy-only extension
w_path_importer_cache = space.sys.get("path_importer_cache")
w_importer = space.finditem(w_path_importer_cache, w_pathitem)
if w_importer is None:
@@ -311,11 +312,15 @@
)
except OperationError, e:
if e.match(space, space.w_ImportError):
- return
+ return None
raise
if space.is_true(w_importer):
space.setitem(w_path_importer_cache, w_pathitem, w_importer)
- if space.is_true(w_importer):
+ return w_importer
+
+def find_in_path_hooks(space, w_modulename, w_pathitem):
+ w_importer = _getimporter(space, w_pathitem)
+ if w_importer is not None and space.is_true(w_importer):
w_loader = space.call_method(w_importer, "find_module", w_modulename)
if space.is_true(w_loader):
return w_loader
More information about the Pypy-commit
mailing list