diff options
author | Éric Araujo <merwok@netwok.org> | 2011-10-19 19:32:39 (GMT) |
---|---|---|
committer | Éric Araujo <merwok@netwok.org> | 2011-10-19 19:32:39 (GMT) |
commit | 3bb8be6d78130dfcf49c4860f0009300508ff92b (patch) | |
tree | 90596cfdab8b6969ee3ae7f6b4abeace9a8d2e06 /Lib/packaging/util.py | |
parent | 1a129c882cbe2f3b51babc047b08d9266634de2b (diff) | |
parent | 784cd4cc543f3685bee3b5e65e78ad5b68d77e04 (diff) | |
download | cpython-3bb8be6d78130dfcf49c4860f0009300508ff92b.zip cpython-3bb8be6d78130dfcf49c4860f0009300508ff92b.tar.gz cpython-3bb8be6d78130dfcf49c4860f0009300508ff92b.tar.bz2 |
Branch merge
Diffstat (limited to 'Lib/packaging/util.py')
-rw-r--r-- | Lib/packaging/util.py | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/Lib/packaging/util.py b/Lib/packaging/util.py index f8a8058..2af1149 100644 --- a/Lib/packaging/util.py +++ b/Lib/packaging/util.py @@ -630,22 +630,35 @@ def find_packages(paths=(os.curdir,), exclude=()): def resolve_name(name): """Resolve a name like ``module.object`` to an object and return it. - Raise ImportError if the module or name is not found. + This functions supports packages and attributes without depth limitation: + ``package.package.module.class.class.function.attr`` is valid input. + However, looking up builtins is not directly supported: use + ``builtins.name``. + + Raises ImportError if importing the module fails or if one requested + attribute is not found. """ + if '.' not in name: + # shortcut + __import__(name) + return sys.modules[name] + + # FIXME clean up this code! parts = name.split('.') cursor = len(parts) module_name = parts[:cursor] + ret = '' while cursor > 0: try: ret = __import__('.'.join(module_name)) break except ImportError: - if cursor == 0: - raise cursor -= 1 module_name = parts[:cursor] - ret = '' + + if ret == '': + raise ImportError(parts[0]) for part in parts[1:]: try: |