11
11
import importlib_resources as resources
12
12
from ..abc import Traversable
13
13
from . import util
14
+ from .compat .py39 import os_helper , import_helper
14
15
15
16
16
17
@contextlib .contextmanager
@@ -112,6 +113,10 @@ class ImplicitContextFiles:
112
113
'submod.py' : set_val ,
113
114
'res.txt' : 'resources are the best' ,
114
115
},
116
+ 'frozenpkg' : {
117
+ '__init__.py' : set_val .replace ('importlib_resources' , 'c_resources' ),
118
+ 'res.txt' : 'resources are the best' ,
119
+ },
115
120
}
116
121
117
122
def test_implicit_files_package (self ):
@@ -126,32 +131,26 @@ def test_implicit_files_submodule(self):
126
131
"""
127
132
assert importlib .import_module ('somepkg.submod' ).val == 'resources are the best'
128
133
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__' ])
132
142
133
- for dirpath , _ , filenames in os .walk (target_dir ):
143
+ for dirpath , _ , filenames in os .walk (c_resources ):
134
144
for filename in filenames :
135
145
source_path = pathlib .Path (dirpath ) / filename
136
146
cfile = source_path .with_suffix ('.pyc' )
137
147
py_compile .compile (source_path , cfile )
138
148
pathlib .Path .unlink (source_path )
149
+ self .fixtures .enter_context (import_helper .DirsOnSysPath (bin_site ))
139
150
140
151
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'
155
154
156
155
157
156
class ImplicitContextFilesDiskTests (
0 commit comments