[Python-checkins] r52807 - sandbox/trunk/import_in_py/importer.py
brett.cannon
python-checkins at python.org
Tue Nov 21 00:11:38 CET 2006
Author: brett.cannon
Date: Tue Nov 21 00:11:37 2006
New Revision: 52807
Modified:
sandbox/trunk/import_in_py/importer.py
Log:
First stab at handling fromlist for packages and doing a relative import as
necessary (w/o worrying about '*'). Tests forthcoming.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py (original)
+++ sandbox/trunk/import_in_py/importer.py Tue Nov 21 00:11:37 2006
@@ -757,9 +757,9 @@
(e.g. has a value of 2 for ``from .. import foo``).
"""
- if level and '__name__' in globals:
- caller_name = globals['__name__']
- is_pkg = True if '__path__' in globals else False
+ is_pkg = True if '__path__' in globals else False
+ caller_name = globals.get('__name__')
+ if level and caller_name:
# Handle the classic style of import: relative first, then
# absolute.
if level == -1:
@@ -798,4 +798,19 @@
# When fromlist is not empty, return the actual module specified in
# the import.
else:
- return sys.modules[imported_name]
+ module = sys.modules[imported_name]
+ if is_pkg and caller_name:
+ # When fromlist has a value and the caller is a package, then
+ # if a name in fromlist is not found as an attribute on module,
+ # try a relative import to find it. Failure is fine and is the
+ # exception is suppressed.
+ for item in fromlist:
+ # XXX Handle '*'.
+ if not hasattr(module, item):
+ resolved_name = self.resolve_name(item, caller_name,
+ True, 1)
+ try:
+ self.import_full_module(resolved_name)
+ except ImportError:
+ pass
+ return module
More information about the Python-checkins
mailing list