[Python-checkins] r70153 - in python/trunk/Lib: importlib/__init__.py test/test_importlib.py
brett.cannon
python-checkins at python.org
Wed Mar 4 02:00:54 CET 2009
Author: brett.cannon
Date: Wed Mar 4 02:00:53 2009
New Revision: 70153
Log:
Fix some more bugs caused by the backport from 3.x for importlib.
Do a more exact copy of the final 3.x code to resolve bugs and add
appropriate tests.
Modified:
python/trunk/Lib/importlib/__init__.py
python/trunk/Lib/test/test_importlib.py
Modified: python/trunk/Lib/importlib/__init__.py
==============================================================================
--- python/trunk/Lib/importlib/__init__.py (original)
+++ python/trunk/Lib/importlib/__init__.py Wed Mar 4 02:00:53 2009
@@ -5,24 +5,16 @@
def _resolve_name(name, package, level):
"""Return the absolute name of the module to be imported."""
- level -= 1
- try:
- if package.count('.') < level:
+ if not hasattr(package, 'rindex'):
+ raise ValueError("'package' not set to a string")
+ dot = len(package)
+ for x in xrange(level, 1, -1):
+ try:
+ dot = package.rindex('.', 0, dot)
+ except ValueError:
raise ValueError("attempted relative import beyond top-level "
"package")
- except AttributeError:
- raise ValueError("'package' not set to a string")
- try:
- # rpartition is more "correct" and rfind is just as easy to use, but
- # neither are in Python 2.3.
- dot_rindex = package.rindex('.', level)
- base = package[:dot_rindex]
- except ValueError:
- base = package
- if name:
- return "%s.%s" % (base, name)
- else:
- return base
+ return "%s.%s" % (package[:dot], name)
def import_module(name, package=None):
@@ -42,10 +34,5 @@
break
level += 1
name = _resolve_name(name[level:], package, level)
- # Try to import specifying the level to be as accurate as possible, but
- # realize that keyword arguments are not found in Python 2.3.
- try:
- __import__(name, level=0)
- except TypeError:
- __import__(name)
+ __import__(name)
return sys.modules[name]
Modified: python/trunk/Lib/test/test_importlib.py
==============================================================================
--- python/trunk/Lib/test/test_importlib.py (original)
+++ python/trunk/Lib/test/test_importlib.py Wed Mar 4 02:00:53 2009
@@ -135,20 +135,20 @@
module = importlib.import_module(name)
self.assertEqual(module.__name__, name)
- def test_relative_package_import(self):
+ def test_shallow_relative_package_import(self):
+ modules = ['a.__init__', 'a.b.__init__', 'a.b.c.__init__', 'a.b.c.d']
+ with mock_modules(*modules) as mock:
+ with import_state(meta_path=[mock]):
+ module = importlib.import_module('.d', 'a.b.c')
+ self.assertEqual(module.__name__, 'a.b.c.d')
+
+ def test_deep_relative_package_import(self):
# Test importing a module from a package through a relatve import.
- pkg_name = 'pkg'
- pkg_long_name = '{0}.__init__'.format(pkg_name)
- module_name = 'mod'
- subpkg_name = '{0}.subpkg'.format(pkg_name)
- subpkg_long_name = '{0}.__init__'.format(subpkg_name)
- absolute_name = '{0}.{1}'.format(pkg_name, module_name)
- relative_name = '..{0}'.format(module_name)
- with mock_modules(pkg_long_name, subpkg_long_name,
- absolute_name) as mock:
+ modules = ['a.__init__', 'a.b.__init__', 'a.c']
+ with mock_modules(*modules) as mock:
with import_state(meta_path=[mock]):
- module = importlib.import_module(relative_name, subpkg_name)
- self.assertEqual(module.__name__, absolute_name)
+ module = importlib.import_module('..c', 'a.b')
+ self.assertEqual(module.__name__, 'a.c')
def test_absolute_import_with_package(self):
# Test importing a module from a package with an absolute name with
More information about the Python-checkins
mailing list