[Python-checkins] r52169 - sandbox/trunk/import_in_py/importer.py sandbox/trunk/import_in_py/test_importer.py
brett.cannon
python-checkins at python.org
Thu Oct 5 20:43:09 CEST 2006
Author: brett.cannon
Date: Thu Oct 5 20:43:09 2006
New Revision: 52169
Modified:
sandbox/trunk/import_in_py/importer.py
sandbox/trunk/import_in_py/test_importer.py
Log:
Cause a call to load_module() that cannot load a module to raise ImportError
(PEP 302 does not specify what to do).
Also fleshed out tests to make sure the proper number of arguments are expected
for find_module() (can be a common error when using static and classmethods;
thanks Paul Moore for the tests).
Finally, comments about the included frozen modules in the stdlib were added so
as to not forget them.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py (original)
+++ sandbox/trunk/import_in_py/importer.py Thu Oct 5 20:43:09 2006
@@ -13,6 +13,10 @@
* __import__ function
http://docs.python.org/lib/built-in-funcs.html
+Some design decisions have been made in this implementation. One is to raise
+an ImportError if a call to load_module() on a loader fails to load a module.
+PEP 302 does not specify what to do in the failing case.
+
"""
import imp
import sys
@@ -57,7 +61,10 @@
try:
return sys.modules[fullname]
except KeyError:
- return imp.init_builtin(fullname)
+ mod = imp.init_builtin(fullname)
+ if not mod:
+ raise ImportError("expected built-in module not loaded")
+ return mod
class FrozenImporter(object):
@@ -86,4 +93,7 @@
try:
return sys.modules[fullname]
except KeyError:
- return imp.init_frozen(fullname)
+ mod = imp.init_frozen(fullname)
+ if not mod:
+ raise ImportError("expected frozen module not loaded")
+ return mod
Modified: sandbox/trunk/import_in_py/test_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/test_importer.py (original)
+++ sandbox/trunk/import_in_py/test_importer.py Thu Oct 5 20:43:09 2006
@@ -15,6 +15,10 @@
'incorrectly found an extension module')
self.failUnless(not importer.BuiltinImporter.find_module('token'),
'incorrectly found a Python module')
+ # Cover proper number of arguments.
+ self.failUnlessRaises(TypeError, importer.BuiltinImporter.find_module)
+ self.failUnlessRaises(TypeError, importer.BuiltinImporter.find_module,
+ 'sys', None, None)
def test_find_module_path_shortcut(self):
# Test short-circuiting of finding a module if 'path' argument is given
@@ -43,10 +47,26 @@
self.failUnlessRaises(TypeError, importer.BuiltinImporter.load_module,
'sys', '')
+ def test_load_module_ImportError(self):
+ self.failUnlessRaises(ImportError,
+ importer.BuiltinImporter.load_module,
+ 'gobbledeegook')
+
+ def test_load_module_arg_count(self):
+ self.failUnlessRaises(TypeError, importer.BuiltinImporter.load_module)
+ self.failUnlessRaises(TypeError, importer.BuiltinImporter.load_module,
+ 'sys', None, None)
+
class FrozenImporterTests(unittest.TestCase):
- """Test the frozen module importer."""
+ """Test the frozen module importer.
+
+ Python include the __hello__ and __phello__ frozen modules (as defined in
+ Python/frozen.c). Both do output to the screen, though, which should be
+ suppressed during testing.
+
+ """
pass
More information about the Python-checkins
mailing list