]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41006: pkgutil imports lazily re (GH-20939)
authorVictor Stinner <vstinner@python.org>
Wed, 17 Jun 2020 17:11:50 +0000 (19:11 +0200)
committerGitHub <noreply@github.com>
Wed, 17 Jun 2020 17:11:50 +0000 (19:11 +0200)
The pkgutil module now imports lazily the re module to speedup Python
startup time.

Lib/pkgutil.py

index 4c184678a29128f03c9edbcdf0dd47f35dcbe9bd..3d7f19f39981de689f861418859e364f5f374211 100644 (file)
@@ -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}')