mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Expand tests and fix bugs in packaging.util.resolve_name.
The code is still ugly, but at least it works better now. Patches to make it easier to read are welcome, as well as support in #12915.
This commit is contained in:
parent
4f996449bf
commit
8ccd18fff3
2 changed files with 57 additions and 30 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue