8000 Backport the test capturing the behavior selectively. · python/cpython@5994e7e · GitHub
[go: up one dir, main page]

Skip to content

Commit 5994e7e

Browse files
committed
Backport the test capturing the behavior selectively.
1 parent dde843d commit 5994e7e

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Lib/test/test_importlib/resources/test_files.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,42 @@ def test_implicit_files(self):
108108
_path.build(spec, self.site_dir)
109109
assert importlib.import_module('somepkg').val == 'resources are the best'
110110

111+
def test_implicit_files_zip_submodule(self):
112+
"""
113+
Special test for gh-121735 for Python 3.12.
114+
"""
115+
import os
116+
import zipfile
117+
118+
def create_zip_from_directory(source_dir, zip_filename):
119+
with zipfile.ZipFile(zip_filename, 'w') as zipf:
120+
for root, _, files in os.walk(source_dir):
121+
for file in files:
122+
file_path = os.path.join(root, file)
123+
# Ensure files are at the root
124+
arcname = os.path.relpath(file_path, source_dir)
125+
zipf.write(file_path, arcname)
126+
127+
set_val = textwrap.dedent(
128+
"""
129+
import importlib.resources as res
130+
val = res.files().joinpath('res.txt').read_text(encoding='utf-8')
131+
"""
132+
)
133+
spec = {
134+
'somepkg': {
135+
'__init__.py': set_val,
136+
'submod.py': set_val,
137+
'res.txt': 'resources are the best',
138+
},
139+
}
140+
build_dir = self.fixtures.enter_context(os_helper.temp_dir())
141+
_path.build(spec, build_dir)
142+
zip_file = os.path.join(self.site_dir, 'thepkg.zip')
143+
create_zip_from_directory(build_dir, zip_file)
144+
self.fixtures.enter_context(import_helper.DirsOnSysPath(zip_file))
145+
assert importlib.import_module('somepkg.submod').val == 'resources are the best'
146+
111147

112148
if __name__ == '__main__':
113149
unittest.main()

0 commit comments

Comments
 (0)
0