From cade8d24bf02e3eb9f2516385e6d25e49228af48 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Mon, 16 Sep 2024 16:43:49 +0200 Subject: [PATCH 1/4] _compile_importlib: Avoid copying sources before compilation --- .../test_importlib/resources/test_files.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_importlib/resources/test_files.py b/Lib/test/test_importlib/resources/test_files.py index 3cdbee302c5e75..a1b853b7aebabc 100644 --- a/Lib/test/test_importlib/resources/test_files.py +++ b/Lib/test/test_importlib/resources/test_files.py @@ -138,14 +138,21 @@ def _compile_importlib(self): bin_site = self.fixtures.enter_context(os_helper.temp_dir()) c_resources = pathlib.Path(bin_site, 'c_resources') sources = pathlib.Path(resources.__file__).parent - shutil.copytree(sources, c_resources, ignore=lambda *_: ['__pycache__']) - for dirpath, _, filenames in os.walk(c_resources): + for dirpath, dirnames, filenames in os.walk(sources): + try: + dirnames.remove('__pycache__') + except ValueError: + pass + source_dir_path = pathlib.Path(dirpath) + dir_relpath = pathlib.Path(source_dir_path).relative_to(sources) + c_dir_path = c_resources.joinpath(dir_relpath) for filename in filenames: - source_path = pathlib.Path(dirpath) / filename - cfile = source_path.with_suffix('.pyc') - py_compile.compile(source_path, cfile) - pathlib.Path.unlink(source_path) + if filename.endswith('.py'): + source_path = source_dir_path / filename + cfile = c_dir_path.joinpath(filename).with_suffix('.pyc') + py_compile.compile(source_path, cfile) + print(source_path, cfile) self.fixtures.enter_context(import_helper.DirsOnSysPath(bin_site)) def test_implicit_files_with_compiled_importlib(self): From 5455d06d64b89b9a4314a4e19dd620d4dd102bb7 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 17 Sep 2024 14:25:19 +0200 Subject: [PATCH 2/4] Remove debug print --- Lib/test/test_importlib/resources/test_files.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_importlib/resources/test_files.py b/Lib/test/test_importlib/resources/test_files.py index a1b853b7aebabc..774d69f2288014 100644 --- a/Lib/test/test_importlib/resources/test_files.py +++ b/Lib/test/test_importlib/resources/test_files.py @@ -152,7 +152,6 @@ def _compile_importlib(self): source_path = source_dir_path / filename cfile = c_dir_path.joinpath(filename).with_suffix('.pyc') py_compile.compile(source_path, cfile) - print(source_path, cfile) self.fixtures.enter_context(import_helper.DirsOnSysPath(bin_site)) def test_implicit_files_with_compiled_importlib(self): From 211b436024b7c2771a91f477f847fae368ff9c6b Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 17 Sep 2024 14:27:06 +0200 Subject: [PATCH 3/4] Add comment --- Lib/test/test_importlib/resources/test_files.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_importlib/resources/test_files.py b/Lib/test/test_importlib/resources/test_files.py index 774d69f2288014..352fb9c694ae8e 100644 --- a/Lib/test/test_importlib/resources/test_files.py +++ b/Lib/test/test_importlib/resources/test_files.py @@ -134,6 +134,9 @@ def test_implicit_files_submodule(self): def _compile_importlib(self): """ 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()) c_resources = pathlib.Path(bin_site, 'c_resources') From 8cd8fe9e08064ef433571da879ba2073375b695e Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 17 Sep 2024 09:08:52 -0400 Subject: [PATCH 4/4] Rely on pathlib.Path.glob for simplicity. --- Lib/test/test_importlib/resources/test_files.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_importlib/resources/test_files.py b/Lib/test/test_importlib/resources/test_files.py index 352fb9c694ae8e..933894dce2c045 100644 --- a/Lib/test/test_importlib/resources/test_files.py +++ b/Lib/test/test_importlib/resources/test_files.py @@ -142,19 +142,9 @@ def _compile_importlib(self): c_resources = pathlib.Path(bin_site, 'c_resources') sources = pathlib.Path(resources.__file__).parent - for dirpath, dirnames, filenames in os.walk(sources): - try: - dirnames.remove('__pycache__') - except ValueError: - pass - source_dir_path = pathlib.Path(dirpath) - dir_relpath = pathlib.Path(source_dir_path).relative_to(sources) - c_dir_path = c_resources.joinpath(dir_relpath) - for filename in filenames: - if filename.endswith('.py'): - source_path = source_dir_path / filename - cfile = c_dir_path.joinpath(filename).with_suffix('.pyc') - py_compile.compile(source_path, cfile) + for source_path in sources.glob('**/*.py'): + c_path = c_resources.joinpath(source_path.relative_to(sources)).with_suffix('.pyc') + py_compile.compile(source_path, c_path) self.fixtures.enter_context(import_helper.DirsOnSysPath(bin_site)) def test_implicit_files_with_compiled_importlib(self):