summaryrefslogtreecommitdiffstats
path: root/Lib/importlib
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2009-03-04 01:00:53 (GMT)
committerBrett Cannon <bcannon@gmail.com>2009-03-04 01:00:53 (GMT)
commit417937733fdcb2aa23b2cf726a5cc6484fe803f5 (patch)
treeffb93b79e652695f356bad2da1da4b4de1ab9e30 /Lib/importlib
parent131af6505ace852a5754f380d451636c09e7d597 (diff)
downloadcpython-417937733fdcb2aa23b2cf726a5cc6484fe803f5.zip
cpython-417937733fdcb2aa23b2cf726a5cc6484fe803f5.tar.gz
cpython-417937733fdcb2aa23b2cf726a5cc6484fe803f5.tar.bz2
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.
Diffstat (limited to 'Lib/importlib')
-rw-r--r--Lib/importlib/__init__.py31
1 files changed, 9 insertions, 22 deletions
diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py
index 35af00b..ad31a1a 100644
--- a/Lib/importlib/__init__.py
+++ b/Lib/importlib/__init__.py
@@ -5,24 +5,16 @@ import sys
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 @@ def import_module(name, package=None):
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]