Skip to content

Commit 9992a54

Browse files
committed
Add test capturing deleting zips. From python/cpython#21748.
1 parent 56072db commit 9992a54

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

importlib_resources/tests/_compat.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
try:
2+
# Python 3.10
3+
from test.support import import_helper
4+
except ImportError:
5+
class import_helper:
6+
from test.support import modules_setup, modules_cleanup
7+
8+
9+
try:
10+
# Python 3.10
11+
from test.support.os_helper import unlink
12+
except ImportError:
13+
from test.support import unlink # noqa

importlib_resources/tests/test_resource.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import sys
22
import unittest
33
import importlib_resources as resources
4+
import uuid
5+
6+
from importlib_resources._compat import Path
47

58
from . import data01
69
from . import zipdata01, zipdata02
710
from . import util
811
from importlib import import_module
12+
from ._compat import import_helper, unlink
913

1014

1115
class ResourceTests:
@@ -166,5 +170,72 @@ def test_namespaces_cannot_have_resources(self):
166170
'importlib_resources.tests.data03.namespace', 'resource1.txt')
167171

168172

173+
class DeletingZipsTest(unittest.TestCase):
174+
"""Having accessed resources in a zip file should not keep an open
175+
reference to the zip.
176+
"""
177+
ZIP_MODULE = zipdata01
178+
179+
def setUp(self):
180+
modules = import_helper.modules_setup()
181+
self.addCleanup(import_helper.modules_cleanup, *modules)
182+
183+
data_path = Path(self.ZIP_MODULE.__file__)
184+
data_dir = data_path.parent
185+
self.source_zip_path = data_dir / 'ziptestdata.zip'
186+
self.zip_path = Path.cwd() / '{}.zip'.format(uuid.uuid4())
187+
self.zip_path.write_bytes(self.source_zip_path.read_bytes())
188+
sys.path.append(str(self.zip_path))
189+
self.data = import_module('ziptestdata')
190+
191+
def tearDown(self):
192+
try:
193+
sys.path.remove(str(self.zip_path))
194+
except ValueError:
195+
pass
196+
197+
try:
198+
del sys.path_importer_cache[str(self.zip_path)]
199+
del sys.modules[self.data.__name__]
200+
except KeyError:
201+
pass
202+
203+
try:
204+
unlink(self.zip_path)
205+
except OSError:
206+
# If the test fails, this will probably fail too
207+
pass
208+
209+
def test_contents_does_not_keep_open(self):
210+
c = resources.contents('ziptestdata')
211+
self.zip_path.unlink()
212+
213+
def test_is_resource_does_not_keep_open(self):
214+
c = resources.is_resource('ziptestdata', 'binary.file')
215+
self.zip_path.unlink()
216+
217+
def test_is_resource_failure_does_not_keep_open(self):
218+
c = resources.is_resource('ziptestdata', 'not-present')
219+
self.zip_path.unlink()
220+
221+
def test_path_does_not_keep_open(self):
222+
c = resources.path('ziptestdata', 'binary.file')
223+
self.zip_path.unlink()
224+
225+
def test_entered_path_does_not_keep_open(self):
226+
# This is what certifi does on import to make its bundle
227+
# available for the process duration.
228+
c = resources.path('ziptestdata', 'binary.file').__enter__()
229+
self.zip_path.unlink()
230+
231+
def test_read_binary_does_not_keep_open(self):
232+
c = resources.read_binary('ziptestdata', 'binary.file')
233+
self.zip_path.unlink()
234+
235+
def test_read_text_does_not_keep_open(self):
236+
c = resources.read_text('ziptestdata', 'utf-8.file', encoding='utf-8')
237+
self.zip_path.unlink()
238+
239+
169240
if __name__ == '__main__':
170241
unittest.main()

0 commit comments

Comments
 (0)