gh-123085: _compile_importlib: Avoid copying sources before compilation (GH-124131)

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
This commit is contained in:
Petr Viktorin 2024-09-18 14:14:34 +02:00 committed by GitHub
parent 8a284e1896
commit 42c8b0556c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 7 deletions

View File

@ -134,18 +134,17 @@ class ImplicitContextFiles:
def _compile_importlib(self): def _compile_importlib(self):
""" """
Make a compiled-only copy of the importlib resources package. Make a compiled-only copy of the importlib resources package.
Currently only code is copied, as importlib resources doesn't itself
have any resources.
""" """
bin_site = self.fixtures.enter_context(os_helper.temp_dir()) bin_site = self.fixtures.enter_context(os_helper.temp_dir())
c_resources = pathlib.Path(bin_site, 'c_resources') c_resources = pathlib.Path(bin_site, 'c_resources')
sources = pathlib.Path(resources.__file__).parent sources = pathlib.Path(resources.__file__).parent
shutil.copytree(sources, c_resources, ignore=lambda *_: ['__pycache__'])
for dirpath, _, filenames in os.walk(c_resources): for source_path in sources.glob('**/*.py'):
for filename in filenames: c_path = c_resources.joinpath(source_path.relative_to(sources)).with_suffix('.pyc')
source_path = pathlib.Path(dirpath) / filename py_compile.compile(source_path, c_path)
cfile = source_path.with_suffix('.pyc')
py_compile.compile(source_path, cfile)
pathlib.Path.unlink(source_path)
self.fixtures.enter_context(import_helper.DirsOnSysPath(bin_site)) self.fixtures.enter_context(import_helper.DirsOnSysPath(bin_site))
def test_implicit_files_with_compiled_importlib(self): def test_implicit_files_with_compiled_importlib(self):