diff options
author | Brett Cannon <brett@python.org> | 2012-05-13 17:45:09 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-05-13 17:45:09 (GMT) |
commit | d200bf534b6d97ee607e1071d0cb2d93e4506268 (patch) | |
tree | 2aab0e9eb36b8362d0d6ee2afe1e2245b69d1cbf /Lib/importlib/util.py | |
parent | 61009468c5be2757c50c9b7bd14582788b83a646 (diff) | |
download | cpython-d200bf534b6d97ee607e1071d0cb2d93e4506268.zip cpython-d200bf534b6d97ee607e1071d0cb2d93e4506268.tar.gz cpython-d200bf534b6d97ee607e1071d0cb2d93e4506268.tar.bz2 |
Add importlib.util.resolve_name().
Diffstat (limited to 'Lib/importlib/util.py')
-rw-r--r-- | Lib/importlib/util.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/Lib/importlib/util.py b/Lib/importlib/util.py index 7b44fa1..1316437 100644 --- a/Lib/importlib/util.py +++ b/Lib/importlib/util.py @@ -3,3 +3,19 @@ from ._bootstrap import module_for_loader from ._bootstrap import set_loader from ._bootstrap import set_package +from ._bootstrap import _resolve_name + + +def resolve_name(name, package): + """Resolve a relative module name to an absolute one.""" + if not name.startswith('.'): + return name + elif not package: + raise ValueError('{!r} is not a relative name ' + '(no leading dot)'.format(name)) + level = 0 + for character in name: + if character != '.': + break + level += 1 + return _resolve_name(name[level:], package, level) |