Skip to content

Commit

Permalink
loader: remove delayed imports from pymod02_importers
Browse files Browse the repository at this point in the history
Instead of performing delayed import of modules, add them (and
their depdendencies) to compat.PY3_BASE_MODULES to have them
available during the bootstrap.
  • Loading branch information
rokm authored and Legorooj committed Jul 28, 2022
1 parent c115bce commit 6cb49d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
15 changes: 10 additions & 5 deletions PyInstaller/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,11 +620,12 @@ def importlib_load_source(name, pathname):
return mod_loader.load_module()


# Patterns of module names that should be bundled into the base_library.zip.
# Patterns of module names that should be bundled into the base_library.zip to be available during bootstrap.
# These modules include direct or indirect dependencies of encodings.* modules. The encodings modules must be
# recursively included to set the I/O encoding during python startup. Similarly, this list should include
# modules used by PyInstaller's bootstrap scripts and modules (loader/pyi*.py)

PY3_BASE_MODULES = {
# These modules are direct or indirect dependencies of encodings.* modules. encodings modules must be recursively
# included to set the I/O encoding during python startup.
'_collections_abc',
'_weakrefset',
'abc',
Expand All @@ -633,26 +634,30 @@ def importlib_load_source(name, pathname):
'copyreg',
'encodings',
'enum',
'fnmatch', # dependency of pathlib
'functools',
'genericpath', # dependency of os.path
'io',
'io', # used by loader/pymod02_importers.py
'heapq',
'keyword',
'linecache',
'locale',
'ntpath', # dependency of os.path
'operator',
'os',
'pathlib', # used by loader/pymod02_importers.py
'posixpath', # dependency of os.path
're',
'reprlib',
'sre_compile',
'sre_constants',
'sre_parse',
'stat', # dependency of os.path
'tokenize', # used by loader/pymod03_importers.py
'token', # depdendency of tokenize
'tokenize', # used by loader/pymod02_importers.py
'traceback', # for startup errors
'types',
'urllib', # dependency of pathlib
'weakref',
'warnings',
}
Expand Down
15 changes: 7 additions & 8 deletions PyInstaller/loader/pyimod02_importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
"""

# **NOTE** This module is used during bootstrap.
# Import *ONLY* builtin modules.
# Import *ONLY* builtin modules or modules that are collected into the base_library.zip archive.
# List of built-in modules: sys.builtin_module_names
# List of modules collected into base_library.zip: PyInstaller.compat.PY3_BASE_MODULES

import sys
import os # guaranteed to be in baselib
import os
import pathlib
import io
import tokenize

import _frozen_importlib

from pyimod01_archive import ArchiveReadError, ZlibArchiveReader

SYS_PREFIX = sys._MEIPASS + os.sep
Expand Down Expand Up @@ -62,11 +67,6 @@ def _decode_source(source_bytes):
Based on CPython's implementation of the same functionality:
https://github.com/python/cpython/blob/3.9/Lib/importlib/_bootstrap_external.py#L679-L688
"""
# Local imports to avoid bootstrap issues
# NOTE: both modules are listed in compat.PY3_BASE_MODULES and collected into base_library.zip.
import io
import tokenize

source_bytes_readline = io.BytesIO(source_bytes).readline
encoding = tokenize.detect_encoding(source_bytes_readline)
newline_decoder = io.IncrementalNewlineDecoder(decoder=None, translate=True)
Expand Down Expand Up @@ -532,7 +532,6 @@ class FrozenResourceReader:
https://github.com/python/cpython/blob/839d7893943782ee803536a47f1d4de160314f85/Lib/importlib/abc.py#L312
"""
def __init__(self, importer, name):
import pathlib # Local import to avoid bootstrap issues.
self.importer = importer
self.path = pathlib.Path(sys._MEIPASS).joinpath(*name.split('.'))

Expand Down

0 comments on commit 6cb49d9

Please sign in to comment.