Skip to content

bpo-41006: pkgutil imports lazily re #20939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions Lib/pkgutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import importlib.machinery
import os
import os.path
import re
import sys
from types import ModuleType
import warnings
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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}')
Expand Down