diff options
author | Victor Stinner <vstinner@python.org> | 2020-06-17 17:11:50 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-17 17:11:50 (GMT) |
commit | 98ce7b107e6611d04dc35a4f5b02ea215ef122cf (patch) | |
tree | a17119f4787234af852ed843331296755a1edc96 /Lib/pkgutil.py | |
parent | 7824cc05bfe7f8181b21848a52007ddaf5612b9b (diff) | |
download | cpython-98ce7b107e6611d04dc35a4f5b02ea215ef122cf.zip cpython-98ce7b107e6611d04dc35a4f5b02ea215ef122cf.tar.gz cpython-98ce7b107e6611d04dc35a4f5b02ea215ef122cf.tar.bz2 |
bpo-41006: pkgutil imports lazily re (GH-20939)
The pkgutil module now imports lazily the re module to speedup Python
startup time.
Diffstat (limited to 'Lib/pkgutil.py')
-rw-r--r-- | Lib/pkgutil.py | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py index 4c18467..3d7f19f 100644 --- a/Lib/pkgutil.py +++ b/Lib/pkgutil.py @@ -7,7 +7,6 @@ import importlib.util import importlib.machinery import os import os.path -import re import sys from types import ModuleType import warnings @@ -638,9 +637,7 @@ def get_data(package, resource): 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 +_NAME_PATTERN = None def resolve_name(name): """ @@ -674,6 +671,15 @@ def resolve_name(name): AttributeError - if a failure occurred when traversing the object hierarchy within the imported package to get to the desired object) """ + global _NAME_PATTERN + if _NAME_PATTERN is None: + # Lazy import to speedup Python startup time + import re + dotted_words = r'(?!\d)(\w+)(\.(?!\d)(\w+))*' + _NAME_PATTERN = re.compile(f'^(?P<pkg>{dotted_words})' + f'(?P<cln>:(?P<obj>{dotted_words})?)?$', + re.UNICODE) + m = _NAME_PATTERN.match(name) if not m: raise ValueError(f'invalid format: {name!r}') |