]> granicus.if.org Git - python/commitdiff
Simplify importib._resolve_name().
authorBrett Cannon <brett@python.org>
Fri, 24 Feb 2012 16:20:54 +0000 (11:20 -0500)
committerBrett Cannon <brett@python.org>
Fri, 24 Feb 2012 16:20:54 +0000 (11:20 -0500)
Lib/importlib/_bootstrap.py

index d880d31aacf55cda999575c4140554cdc951baaf..de3df2ebed619f01bb0f50475716a242038ad04f 100644 (file)
@@ -890,17 +890,11 @@ class _ImportLockContext:
 
 def _resolve_name(name, package, level):
     """Resolve a relative module name to an absolute one."""
-    dot = len(package)
-    for x in range(level, 1, -1):
-        try:
-            dot = package.rindex('.', 0, dot)
-        except ValueError:
-            raise ValueError("attempted relative import beyond "
-                             "top-level package")
-    if name:
-        return "{0}.{1}".format(package[:dot], name)
-    else:
-        return package[:dot]
+    bits = package.rsplit('.', level-1)
+    if len(bits) < level:
+        raise ValueError('attempted relative import beyond top-level package')
+    base = bits[0]
+    return '{0}.{1}'.format(base, name) if name else base
 
 
 def _find_module(name, path):