1111import importlib_resources as resources
1212from ..abc import Traversable
1313from . import util
14+ from .compat .py39 import os_helper , import_helper
1415
1516
1617@contextlib .contextmanager
@@ -112,6 +113,10 @@ class ImplicitContextFiles:
112113 'submod.py' : set_val ,
113114 'res.txt' : 'resources are the best' ,
114115 },
116+ 'frozenpkg' : {
117+ '__init__.py' : set_val .replace ('importlib_resources' , 'c_resources' ),
118+ 'res.txt' : 'resources are the best' ,
119+ },
115120 }
116121
117122 def test_implicit_files_package (self ):
@@ -126,32 +131,26 @@ def test_implicit_files_submodule(self):
126131 """
127132 assert importlib .import_module ('somepkg.submod' ).val == 'resources are the best'
128133
129- def _compile_importlib (self , target_dir ):
130- importlib_dir = pathlib .Path (importlib .__file__ ).parent
131- shutil .copytree (importlib_dir , target_dir , ignore = lambda * _ : ['__pycache__' ])
134+ def _compile_importlib (self ):
135+ """
136+ Make a compiled-only copy of the importlib resources package.
137+ """
138+ bin_site = self .fixtures .enter_context (os_helper .temp_dir ())
139+ c_resources = pathlib .Path (bin_site , 'c_resources' )
140+ sources = pathlib .Path (resources .__file__ ).parent
141+ shutil .copytree (sources , c_resources , ignore = lambda * _ : ['__pycache__' ])
132142
133- for dirpath , _ , filenames in os .walk (target_dir ):
143+ for dirpath , _ , filenames in os .walk (c_resources ):
134144 for filename in filenames :
135145 source_path = pathlib .Path (dirpath ) / filename
136146 cfile = source_path .with_suffix ('.pyc' )
137147 py_compile .compile (source_path , cfile )
138148 pathlib .Path .unlink (source_path )
149+ self .fixtures .enter_context (import_helper .DirsOnSysPath (bin_site ))
139150
140151 def test_implicit_files_with_compiled_importlib (self ):
141- self ._compile_importlib (pathlib .Path (self .site_dir ) / 'cimportlib' )
142- spec = {
143- 'somepkg' : {
144- '__init__.py' : textwrap .dedent (
145- """
146- import cimportlib.resources as res
147- val = res.files().joinpath('res.txt').read_text(encoding='utf-8')
148- """
149- ),
150- 'res.txt' : 'resources are the best' ,
151- },
152- }
153- _path .build (spec , self .site_dir )
154- assert importlib .import_module ('somepkg' ).val == 'resources are the best'
152+ self ._compile_importlib ()
153+ assert importlib .import_module ('frozenpkg' ).val == 'resources are the best'
155154
156155
157156class ImplicitContextFilesDiskTests (
0 commit comments