[Python-checkins] r52943 - sandbox/trunk/import_in_py/importer.py sandbox/trunk/import_in_py/test_importer.py
brett.cannon
python-checkins at python.org
Thu Dec 7 00:09:15 CET 2006
Author: brett.cannon
Date: Thu Dec 7 00:09:14 2006
New Revision: 52943
Modified:
sandbox/trunk/import_in_py/importer.py
sandbox/trunk/import_in_py/test_importer.py
Log:
When doing a relative import, the "return up to the first dot in the name when
fromlist is empty" rule applies to the *relative* name, not the absolute name.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py (original)
+++ sandbox/trunk/import_in_py/importer.py Thu Dec 7 00:09:14 2006
@@ -744,7 +744,13 @@
"""
if not fromlist:
- return sys.modules[absolute_name.split('.', 1)[0]]
+ if relative_name:
+ absolute_base = absolute_name.rpartition(relative_name)[0]
+ relative_head = relative_name.split('.', 1)[0]
+ to_return = absolute_base + relative_head
+ else:
+ to_return = absolute_name.split('.', 1)[0]
+ return sys.modules[to_return]
# When fromlist is not empty, return the actual module specified in
# the import.
else:
@@ -841,6 +847,6 @@
imported_name = name
self.import_full_module(name)
relative_name = '' if imported_name != name else name
- return self.return_module(imported_name, relative_name, fromlist)
+ return self.return_module(imported_name, name, fromlist)
finally:
imp.release_lock()
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 Dec 7 00:09:14 2006
@@ -946,6 +946,26 @@
module = self.importer.return_module(self.full_child_name, '',
[test_attr_name])
self.failUnless(module is self.child_module)
+
+ def test_relative_import_empty_fromlist(self):
+ # If a relative import (with no dot) is performed with an empty fromlist
+ # then the module specified in the relative name is returned.
+ sys.modules[self.full_child_name] = self.child_module
+ sys.modules[self.parent_name] = self.parent_module
+ module = self.importer.return_module(self.full_child_name,
+ self.child_name, [])
+ self.failUnless(module is self.child_module)
+
+ def test_deep_relative_import_empty_fromlist(self):
+ # If the relative name of a module has a dot, return the module that
+ # resolves to the absolute name of the module up to the first dot.
+ sys.modules[self.full_child_name] = self.child_module
+ sys.modules[self.parent_name] = self.parent_module
+ extra_depth = '.baz'
+ absolute_name = self.full_child_name + extra_depth
+ relative_name = self.child_name + extra_depth
+ module = self.importer.return_module(absolute_name, relative_name, [])
+ self.failUnless(module is self.child_module)
class ImportMiscTests(ImportHelper2):
@@ -1330,6 +1350,14 @@
module_globals = {'__name__':self.pkg_name + '.another_module'}
module = self.import_(self.module_name, module_globals, level=1)
self.verify_package(module, self.pkg_module_name)
+
+ def test_relative_import_return(self):
+ # When importing from a relative name, the module up to the first dot
+ # of that relative name (made absolute) should be returned.
+ module_globals = {'__name__':self.pkg_module_name}
+ relative_name = self.sub_pkg_tail_name + '.' + self.module_name
+ module = self.import_(relative_name, module_globals)
+ self.verify_package(module, self.sub_pkg_name)
More information about the Python-checkins
mailing list