# HG changeset patch # User Ronny Pfannschmidt # Date 1338468514 -7200 # Node ID 60c6c8967d508381f1724ffdfd4c3921ec4eb9e7 # Parent 747eec42e7ae9cfe821f534760846b361578d27c clean up distutils2.util.resolve_name diff --git a/distutils2/util.py b/distutils2/util.py --- a/distutils2/util.py +++ b/distutils2/util.py @@ -648,28 +648,30 @@ def resolve_name(name): __import__(name) return sys.modules[name] - # FIXME clean up this code! parts = name.split('.') - cursor = len(parts) - module_name = parts[:cursor] - ret = '' + attributes = [] + ret = None - while cursor > 0: + while parts: try: - ret = __import__('.'.join(module_name)) + mod = ret = __import__('.'.join(parts), + fromlist=['dummy_value_to_get_the_module']) break except ImportError: - cursor -= 1 - module_name = parts[:cursor] + attributes.append(parts.pop()) + # we reached the toplevel, reraise + if not parts: + raise - if ret == '': - raise ImportError(parts[0]) - - for part in parts[1:]: + attributes.reverse() + for idx, attr in enumerate(attributes): try: - ret = getattr(ret, part) + ret = getattr(ret, attr) except AttributeError, exc: - raise ImportError(exc) + raise ImportError('cannot find attribute %s on module %s' % ( + mod.__name__, + '.'.join(attributes[:idx]) + )) return ret