diff options
| author | shadchin <[email protected]> | 2022-02-10 16:44:39 +0300 |
|---|---|---|
| committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:44:39 +0300 |
| commit | e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (patch) | |
| tree | 64175d5cadab313b3e7039ebaa06c5bc3295e274 /contrib/tools/python3/src/Lib/pkgutil.py | |
| parent | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (diff) | |
Restoring authorship annotation for <[email protected]>. Commit 2 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Lib/pkgutil.py')
| -rw-r--r-- | contrib/tools/python3/src/Lib/pkgutil.py | 142 |
1 files changed, 71 insertions, 71 deletions
diff --git a/contrib/tools/python3/src/Lib/pkgutil.py b/contrib/tools/python3/src/Lib/pkgutil.py index c1e5e9239ce..9608d0e0ede 100644 --- a/contrib/tools/python3/src/Lib/pkgutil.py +++ b/contrib/tools/python3/src/Lib/pkgutil.py @@ -7,7 +7,7 @@ import importlib.util import importlib.machinery import os import os.path -import re +import re import sys from types import ModuleType import warnings @@ -412,7 +412,7 @@ def get_importer(path_item): The cache (or part of it) can be cleared manually if a rescan of sys.path_hooks is necessary. """ - path_item = os.fsdecode(path_item) + path_item = os.fsdecode(path_item) try: importer = sys.path_importer_cache[path_item] except KeyError: @@ -637,72 +637,72 @@ def get_data(package, resource): parts.insert(0, os.path.dirname(mod.__file__)) resource_name = os.path.join(*parts) return loader.get_data(resource_name) - - -_DOTTED_WORDS = r'(?!\d)(\w+)(\.(?!\d)(\w+))*' -_NAME_PATTERN = re.compile(f'^(?P<pkg>{_DOTTED_WORDS})(?P<cln>:(?P<obj>{_DOTTED_WORDS})?)?$', re.U) -del _DOTTED_WORDS - -def resolve_name(name): - """ - Resolve a name to an object. - - It is expected that `name` will be a string in one of the following - formats, where W is shorthand for a valid Python identifier and dot stands - for a literal period in these pseudo-regexes: - - W(.W)* - W(.W)*:(W(.W)*)? - - The first form is intended for backward compatibility only. It assumes that - some part of the dotted name is a package, and the rest is an object - somewhere within that package, possibly nested inside other objects. - Because the place where the package stops and the object hierarchy starts - can't be inferred by inspection, repeated attempts to import must be done - with this form. - - In the second form, the caller makes the division point clear through the - provision of a single colon: the dotted name to the left of the colon is a - package to be imported, and the dotted name to the right is the object - hierarchy within that package. Only one import is needed in this form. If - it ends with the colon, then a module object is returned. - - The function will return an object (which might be a module), or raise one - of the following exceptions: - - ValueError - if `name` isn't in a recognised format - ImportError - if an import failed when it shouldn't have - AttributeError - if a failure occurred when traversing the object hierarchy - within the imported package to get to the desired object. - """ - m = _NAME_PATTERN.match(name) - if not m: - raise ValueError(f'invalid format: {name!r}') - gd = m.groupdict() - if gd.get('cln'): - # there is a colon - a one-step import is all that's needed - mod = importlib.import_module(gd['pkg']) - parts = gd.get('obj') - parts = parts.split('.') if parts else [] - else: - # no colon - have to iterate to find the package boundary - parts = name.split('.') - modname = parts.pop(0) - # first part *must* be a module/package. - mod = importlib.import_module(modname) - while parts: - p = parts[0] - s = f'{modname}.{p}' - try: - mod = importlib.import_module(s) - parts.pop(0) - modname = s - except ImportError: - break - # if we reach this point, mod is the module, already imported, and - # parts is the list of parts in the object hierarchy to be traversed, or - # an empty list if just the module is wanted. - result = mod - for p in parts: - result = getattr(result, p) - return result + + +_DOTTED_WORDS = r'(?!\d)(\w+)(\.(?!\d)(\w+))*' +_NAME_PATTERN = re.compile(f'^(?P<pkg>{_DOTTED_WORDS})(?P<cln>:(?P<obj>{_DOTTED_WORDS})?)?$', re.U) +del _DOTTED_WORDS + +def resolve_name(name): + """ + Resolve a name to an object. + + It is expected that `name` will be a string in one of the following + formats, where W is shorthand for a valid Python identifier and dot stands + for a literal period in these pseudo-regexes: + + W(.W)* + W(.W)*:(W(.W)*)? + + The first form is intended for backward compatibility only. It assumes that + some part of the dotted name is a package, and the rest is an object + somewhere within that package, possibly nested inside other objects. + Because the place where the package stops and the object hierarchy starts + can't be inferred by inspection, repeated attempts to import must be done + with this form. + + In the second form, the caller makes the division point clear through the + provision of a single colon: the dotted name to the left of the colon is a + package to be imported, and the dotted name to the right is the object + hierarchy within that package. Only one import is needed in this form. If + it ends with the colon, then a module object is returned. + + The function will return an object (which might be a module), or raise one + of the following exceptions: + + ValueError - if `name` isn't in a recognised format + ImportError - if an import failed when it shouldn't have + AttributeError - if a failure occurred when traversing the object hierarchy + within the imported package to get to the desired object. + """ + m = _NAME_PATTERN.match(name) + if not m: + raise ValueError(f'invalid format: {name!r}') + gd = m.groupdict() + if gd.get('cln'): + # there is a colon - a one-step import is all that's needed + mod = importlib.import_module(gd['pkg']) + parts = gd.get('obj') + parts = parts.split('.') if parts else [] + else: + # no colon - have to iterate to find the package boundary + parts = name.split('.') + modname = parts.pop(0) + # first part *must* be a module/package. + mod = importlib.import_module(modname) + while parts: + p = parts[0] + s = f'{modname}.{p}' + try: + mod = importlib.import_module(s) + parts.pop(0) + modname = s + except ImportError: + break + # if we reach this point, mod is the module, already imported, and + # parts is the list of parts in the object hierarchy to be traversed, or + # an empty list if just the module is wanted. + result = mod + for p in parts: + result = getattr(result, p) + return result |
