Skip to content

Commit d93e241

Browse files
sfc-gh-wzhaojaraco
authored andcommitted
Fix NotADirectory error when calling files on a submodule of a zipped namespace package
1 parent eec758b commit d93e241

File tree

8 files changed

+49
-6
lines changed

8 files changed

+49
-6
lines changed

importlib_resources/readers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def _candidate_paths(cls, path_str):
156156
def _resolve_zip_path(path_str):
157157
for match in reversed(list(re.finditer(r'[\\/]', path_str))):
158158
with contextlib.suppress(
159-
FileNotFoundError, IsADirectoryError, PermissionError
159+
FileNotFoundError, IsADirectoryError, NotADirectoryError, PermissionError
160160
):
161161
inner = path_str[match.end() :].replace('\\', '/') + '/'
162162
yield ZipPath(path_str[: match.start()], inner.lstrip('/'))
Binary file not shown.

importlib_resources/tests/test_contents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class ContentsZipTests(ContentsTests, util.ZipSetup, unittest.TestCase):
3131
class ContentsNamespaceTests(ContentsTests, unittest.TestCase):
3232
expected = {
3333
# no __init__ because of namespace design
34-
# no subdirectory as incidental difference in fixture
3534
'binary.file',
35+
'subdirectory',
3636
'utf-16.file',
3737
'utf-8.file',
3838
}

importlib_resources/tests/test_files.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def setUp(self):
5858
self.data = namespacedata01
5959

6060

61+
class OpenNamespaceZipTests(FilesTests, util.ZipSetup, unittest.TestCase):
62+
ZIP_MODULE = 'namespacedata01'
63+
64+
6165
class SiteDir:
6266
def setUp(self):
6367
self.fixtures = contextlib.ExitStack()

importlib_resources/tests/test_open.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,9 @@ class OpenZipTests(OpenTests, util.ZipSetup, unittest.TestCase):
8181
pass
8282

8383

84+
class OpenNamespaceZipTests(OpenTests, util.ZipSetup, unittest.TestCase):
85+
ZIP_MODULE = 'namespacedata01'
86+
87+
8488
if __name__ == '__main__':
8589
unittest.main()

importlib_resources/tests/test_read.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,20 @@ def setUp(self):
7676
self.data = namespacedata01
7777

7878

79+
class ReadNamespaceZipTests(ReadTests, util.ZipSetup, unittest.TestCase):
80+
ZIP_MODULE = 'namespacedata01'
81+
82+
def test_read_submodule_resource(self):
83+
submodule = import_module('namespacedata01.subdirectory')
84+
result = resources.files(submodule).joinpath('binary.file').read_bytes()
85+
self.assertEqual(result, b'\0\1\2\3')
86+
87+
def test_read_submodule_resource_by_name(self):
88+
result = (
89+
resources.files('namespacedata01.subdirectory').joinpath('binary.file').read_bytes()
90+
)
91+
self.assertEqual(result, b'\0\1\2\3')
92+
93+
7994
if __name__ == '__main__':
8095
unittest.main()

importlib_resources/tests/test_reader.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_iterdir(self):
2626
contents.remove('__pycache__')
2727
except (KeyError, ValueError):
2828
pass
29-
self.assertEqual(contents, {'binary.file', 'utf-16.file', 'utf-8.file'})
29+
self.assertEqual(contents, {'subdirectory', 'binary.file', 'utf-16.file', 'utf-8.file'})
3030

3131
def test_iterdir_duplicate(self):
3232
data01 = pathlib.Path(__file__).parent.joinpath('data01')
@@ -67,7 +67,11 @@ def test_join_path(self):
6767
os.path.join('namespacedata01', 'binary.file'),
6868
)
6969
self.assertEqual(
70-
str(path.joinpath('subdirectory'))[len(prefix) + 1 :],
70+
str(path.joinpath('subdirectory')._paths[0])[len(prefix) + 1 :],
71+
os.path.join('namespacedata01', 'subdirectory'),
72+
)
73+
self.assertEqual(
74+
str(path.joinpath('subdirectory')._paths[1])[len(prefix) + 1 :],
7175
os.path.join('data01', 'subdirectory'),
7276
)
7377
self.assertEqual(

importlib_resources/tests/test_resource.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,31 @@ def test_submodule_contents(self):
186186
contents.remove('__pycache__')
187187
except KeyError:
188188
pass
189-
self.assertEqual(contents, {'binary.file', 'utf-8.file', 'utf-16.file'})
189+
self.assertEqual(contents, {'subdirectory', 'binary.file', 'utf-8.file', 'utf-16.file'})
190190

191191
def test_submodule_contents_by_name(self):
192192
contents = names(resources.files('namespacedata01'))
193193
try:
194194
contents.remove('__pycache__')
195195
except KeyError:
196196
pass
197-
self.assertEqual(contents, {'binary.file', 'utf-8.file', 'utf-16.file'})
197+
self.assertEqual(contents, {'subdirectory', 'binary.file', 'utf-8.file', 'utf-16.file'})
198+
199+
def test_submodule_sub_contents(self):
200+
contents = names(resources.files(import_module('namespacedata01.subdirectory')))
201+
try:
202+
contents.remove('__pycache__')
203+
except KeyError:
204+
pass
205+
self.assertEqual(contents, {'binary.file'})
206+
207+
def test_submodule_sub_contents_by_name(self):
208+
contents = names(resources.files('namespacedata01.subdirectory'))
209+
try:
210+
contents.remove('__pycache__')
211+
except KeyError:
212+
pass
213+
self.assertEqual(contents, {'binary.file'})
198214

199215

200216
class ResourceFromNamespaceDiskTests(ResourceFromNamespaceTests, unittest.TestCase):

0 commit comments

Comments
 (0)