summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-02-24 16:20:54 (GMT)
committerBrett Cannon <brett@python.org>2012-02-24 16:20:54 (GMT)
commit625cd23da4e760311bd81d8f3f7edba809e981d8 (patch)
tree290b3ecd55d9f41cba55f2f751c19b39fa1e03e8
parent842c0f17eb7acea93eebfcf1531b583445ce3782 (diff)
downloadcpython-625cd23da4e760311bd81d8f3f7edba809e981d8.zip
cpython-625cd23da4e760311bd81d8f3f7edba809e981d8.tar.gz
cpython-625cd23da4e760311bd81d8f3f7edba809e981d8.tar.bz2
Simplify importib._resolve_name().
-rw-r--r--Lib/importlib/_bootstrap.py16
1 files changed, 5 insertions, 11 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index d880d31..de3df2e 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -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):