[Python-checkins] r52922 - 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
Tue Dec 5 23:11:41 CET 2006
Author: brett.cannon
Date: Tue Dec 5 23:11:40 2006
New Revision: 52922
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:
Add support for '*' in fromlist causing __all__ to be checked and used for automatic relative imports.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py (original)
+++ sandbox/trunk/import_in_py/importer.py Tue Dec 5 23:11:40 2006
@@ -28,11 +28,6 @@
XXX Semantics
=============
-Packages
---------
-* ``from ... import *`` for packages uses __all__ to extend fromlist
- [package essay].
-
Bytecode
--------
* What to do when magic number and timestamp works, but bytecode is bad?
@@ -798,8 +793,12 @@
# 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 '*'.
+ check_for = fromlist[:]
+ if '*' in check_for and hasattr(module, '__all__'):
+ check_for.extend(module.__all__)
+ for item in check_for:
+ if item == '*':
+ continue
if not hasattr(module, item):
resolved_name = self.resolve_name(item, module.__name__,
True, 1)
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 Tue Dec 5 23:11:40 2006
@@ -16,13 +16,15 @@
"""A mock module."""
- def __init__(self, name=None, file_path=None, pkg_list=None):
+ def __init__(self, name=None, file_path=None, pkg_list=None, __all__=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
+ if __all__ is not None:
+ self.__all__ = __all__
class MockHandler(object):
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 Tue Dec 5 23:11:40 2006
@@ -952,10 +952,22 @@
imported_module = self.importer(module_name, fromlist=['sadfsdd'])
self.failUnless(not hasattr(failing_import, 'find_request'))
- def XXX_test_fromlist_relative_import_all(self):
+ def test_fromlist_relative_import_all(self):
# When '*' is passed in for fromlist, __all__ should be used for the
# possibility of a relative import.
- pass
+ module_name = '<module name>'
+ pkg_name = '<pkg name>'
+ full_module_name = pkg_name + '.' + module_name
+ pkg_module = mock_importer.MockModule(pkg_name, pkg_list=['some path'],
+ __all__=[module_name])
+ sys.modules[pkg_name] = pkg_module
+ succeed = mock_importer.SucceedImporter()
+ sys.meta_path.append(succeed)
+ module = self.importer(pkg_name, fromlist=['*'])
+ self.failUnless(hasattr(module, module_name))
+ relative_module = getattr(module, module_name)
+ self.failUnlessEqual(relative_module.__name__, full_module_name)
+
class ImportMetaPathTests(ImportHelper):
@@ -1270,6 +1282,7 @@
self.verify_package(module, self.pkg_module_name)
+
def test_main():
test_classes = [cls for cls in globals().itervalues()
if isinstance(cls, type) and
More information about the Python-checkins
mailing list