[Python-checkins] r52597 - sandbox/trunk/import_in_py/importer.py sandbox/trunk/import_in_py/mock_importer.py sandbox/trunk/import_in_py/test_importer.py
brett.cannon
python-checkins at python.org
Fri Nov 3 00:18:04 CET 2006
Author: brett.cannon
Date: Fri Nov 3 00:18:03 2006
New Revision: 52597
Modified:
sandbox/trunk/import_in_py/importer.py
sandbox/trunk/import_in_py/mock_importer.py
sandbox/trunk/import_in_py/test_importer.py
Log:
Complete implementing importing of a module from a top-level package.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py (original)
+++ sandbox/trunk/import_in_py/importer.py Fri Nov 3 00:18:03 2006
@@ -71,10 +71,9 @@
still have the full name of the module passed in)
[pep 302 and introspection(Python/import.c:1278)].
-Implementing
-------------
+XXX Need to Implement
+---------------------
#. Importing module within a package.
- + One level deep.
+ Arbitrarily deep.
+ Search __path__ and not sys.path.
+ Parent modules not already imported.
@@ -685,7 +684,7 @@
pass
module = self.import_(current_name, path_list)
if parent_module:
- setattr(parent_module, current_name, module)
+ setattr(parent_module, name_part, module)
parent_module = module
# When fromlist is not specified, return the root module (i.e., module
# up to first dot).
Modified: sandbox/trunk/import_in_py/mock_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/mock_importer.py (original)
+++ sandbox/trunk/import_in_py/mock_importer.py Fri Nov 3 00:18:03 2006
@@ -10,6 +10,18 @@
return method(self, *args, **kwargs)
return log_and_call
+class MockModule(object):
+
+ """A mock module."""
+
+ def __init__(self, name=None, file_path=None, pkg_list=None):
+ if name is not None:
+ self.__name__ = name
+ if file_path is not None:
+ self.__file__ = file_path
+ if pkg_list is not None:
+ self.__path__ = pkg_list
+
class MockHandler(object):
@@ -220,11 +232,10 @@
class SucceedImporter(object):
"""Mock importer that always succeed by returning 'self'."""
-
- module = 42
def __init__(self):
self.path_entries = []
+ self.loaded_modules = []
def __call__(self, path_entry):
self.path_entries.append(path_entry)
@@ -236,8 +247,10 @@
def load_module(self, fullname, path=None):
self.load_request = fullname, path
- sys.modules[fullname] = self.module
- return self.module
+ module = MockModule(fullname, '<succeed importer>', path)
+ self.loaded_modules.append(module)
+ sys.modules[fullname] = module
+ return module
@classmethod
def set_on_sys_path(cls):
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 Fri Nov 3 00:18:03 2006
@@ -580,6 +580,38 @@
# An import should fail if a parent module cannot be found.
sys.modules['a.b'] = 'a.b'
self.failUnlessRaises(ImportError, self.importer, 'a.b')
+
+ def test_parent_imported(self):
+ # If a parent module is already imported, importing a child module
+ # should work (along with setting the attribute on the parent for the
+ # child module).
+ succeed_importer = mock_importer.SucceedImporter()
+ sys.meta_path.append(succeed_importer)
+ parent_name = '<parent>'
+ parent_module = mock_importer.MockModule(parent_name)
+ sys.modules[parent_name] = parent_module
+ child_name = 'module'
+ full_child_name = parent_name + '.' + child_name
+ self.importer(full_child_name)
+ self.failUnless(hasattr(parent_module, child_name))
+ self.failUnlessEqual(getattr(parent_module, child_name),
+ sys.modules[full_child_name])
+
+ def test_parent_not_imported(self):
+ # If a parent module is not imported yet, it should be imported.
+ # The attribute on the parent module for the child module should also
+ # be set.
+ succeed_importer = mock_importer.SucceedImporter()
+ sys.meta_path.append(succeed_importer)
+ parent_name = '<parent>'
+ child_name = '<child>'
+ full_child_name = '.'.join([parent_name, child_name])
+ self.importer(full_child_name)
+ self.failUnless(parent_name in sys.modules)
+ self.failUnless(full_child_name in sys.modules)
+ self.failUnless(hasattr(sys.modules[parent_name], child_name))
+ self.failUnlessEqual(getattr(sys.modules[parent_name], child_name),
+ sys.modules[full_child_name])
def test_empty_fromlist(self):
# An empty fromlist means that the root module is returned.
@@ -622,7 +654,7 @@
module = importer_.import_('sys')
for meta_importer in (pass_importer, succeed_importer):
self.failUnlessEqual(meta_importer.find_request, ('sys', None))
- self.failUnless(module is mock_importer.SucceedImporter.module)
+ self.failUnless(module in succeed_importer.loaded_modules)
def test_parent_path(self):
# If a parent module has __path__ defined it should be passed as an
@@ -658,7 +690,7 @@
module = importer_.import_(module_name)
self.failUnlessEqual(succeed_importer.find_request,
(module_name, None))
- self.failUnless(module is mock_importer.SucceedImporter.module)
+ self.failUnless(module in succeed_importer.loaded_modules)
def test_search_std_path(self):
# Test sys.path searching for a loader.
@@ -666,12 +698,13 @@
self.clear_sys_modules(module_name)
importer_ = importer.Import(extended_meta_path=())
sys.path = []
- sys_path = (mock_importer.PassImporter.set_on_sys_path(),
- mock_importer.SucceedImporter.set_on_sys_path())
+ pass_importer = mock_importer.PassImporter.set_on_sys_path()
+ succeed_importer = mock_importer.SucceedImporter.set_on_sys_path()
+ sys_path = (pass_importer, succeed_importer)
module = importer_.import_(module_name)
for entry in sys_path:
self.failUnlessEqual(entry.find_request, (module_name, None))
- self.failUnless(module is mock_importer.SucceedImporter.module)
+ self.failUnless(module in succeed_importer.loaded_modules)
def test_importer_cache_preexisting(self):
# A pre-existing importer should be returned if it exists in
More information about the Python-checkins
mailing list