Skip to content

Commit

Permalink
Fix NotADirectory error when calling files on a submodule of a zipped…
Browse files Browse the repository at this point in the history
… namespace package
  • Loading branch information
sfc-gh-wzhao committed Jan 3, 2024
1 parent 7d8b020 commit aba2a7e
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion importlib_resources/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def _candidate_paths(cls, path_str):
def _resolve_zip_path(path_str):
for match in reversed(list(re.finditer(r'[\\/]', path_str))):
with contextlib.suppress(
FileNotFoundError, IsADirectoryError, PermissionError
FileNotFoundError, IsADirectoryError, NotADirectoryError, PermissionError
):
inner = path_str[match.end() :].replace('\\', '/') + '/'
yield ZipPath(path_str[: match.start()], inner.lstrip('/'))
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion importlib_resources/tests/test_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class ContentsZipTests(ContentsTests, util.ZipSetup, unittest.TestCase):
class ContentsNamespaceTests(ContentsTests, unittest.TestCase):
expected = {
# no __init__ because of namespace design
# no subdirectory as incidental difference in fixture
'binary.file',
'subdirectory',
'utf-16.file',
'utf-8.file',
}
Expand Down
4 changes: 4 additions & 0 deletions importlib_resources/tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def setUp(self):
self.data = namespacedata01


class OpenNamespaceZipTests(FilesTests, util.ZipSetup, unittest.TestCase):
ZIP_MODULE = 'namespacedata01'


class SiteDir:
def setUp(self):
self.fixtures = contextlib.ExitStack()
Expand Down
4 changes: 4 additions & 0 deletions importlib_resources/tests/test_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,9 @@ class OpenZipTests(OpenTests, util.ZipSetup, unittest.TestCase):
pass


class OpenNamespaceZipTests(OpenTests, util.ZipSetup, unittest.TestCase):
ZIP_MODULE = 'namespacedata01'


if __name__ == '__main__':
unittest.main()
15 changes: 15 additions & 0 deletions importlib_resources/tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,20 @@ def setUp(self):
self.data = namespacedata01


class ReadNamespaceZipTests(ReadTests, util.ZipSetup, unittest.TestCase):
ZIP_MODULE = 'namespacedata01'

def test_read_submodule_resource(self):
submodule = import_module('namespacedata01.subdirectory')
result = resources.files(submodule).joinpath('binary.file').read_bytes()
self.assertEqual(result, b'\0\1\2\3')

def test_read_submodule_resource_by_name(self):
result = (
resources.files('namespacedata01.subdirectory').joinpath('binary.file').read_bytes()
)
self.assertEqual(result, b'\0\1\2\3')


if __name__ == '__main__':
unittest.main()
8 changes: 6 additions & 2 deletions importlib_resources/tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_iterdir(self):
contents.remove('__pycache__')
except (KeyError, ValueError):
pass
self.assertEqual(contents, {'binary.file', 'utf-16.file', 'utf-8.file'})
self.assertEqual(contents, {'subdirectory', 'binary.file', 'utf-16.file', 'utf-8.file'})

def test_iterdir_duplicate(self):
data01 = pathlib.Path(__file__).parent.joinpath('data01')
Expand Down Expand Up @@ -67,7 +67,11 @@ def test_join_path(self):
os.path.join('namespacedata01', 'binary.file'),
)
self.assertEqual(
str(path.joinpath('subdirectory'))[len(prefix) + 1 :],
str(path.joinpath('subdirectory')._paths[0])[len(prefix) + 1 :],
os.path.join('namespacedata01', 'subdirectory'),
)
self.assertEqual(
str(path.joinpath('subdirectory')._paths[1])[len(prefix) + 1 :],
os.path.join('data01', 'subdirectory'),
)
self.assertEqual(
Expand Down
20 changes: 18 additions & 2 deletions importlib_resources/tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,31 @@ def test_submodule_contents(self):
contents.remove('__pycache__')
except KeyError:
pass
self.assertEqual(contents, {'binary.file', 'utf-8.file', 'utf-16.file'})
self.assertEqual(contents, {'subdirectory', 'binary.file', 'utf-8.file', 'utf-16.file'})

def test_submodule_contents_by_name(self):
contents = names(resources.files('namespacedata01'))
try:
contents.remove('__pycache__')
except KeyError:
pass
self.assertEqual(contents, {'binary.file', 'utf-8.file', 'utf-16.file'})
self.assertEqual(contents, {'subdirectory', 'binary.file', 'utf-8.file', 'utf-16.file'})

def test_submodule_sub_contents(self):
contents = names(resources.files(import_module('namespacedata01.subdirectory')))
try:
contents.remove('__pycache__')
except KeyError:
pass
self.assertEqual(contents, {'binary.file'})

def test_submodule_sub_contents_by_name(self):
contents = names(resources.files('namespacedata01.subdirectory'))
try:
contents.remove('__pycache__')
except KeyError:
pass
self.assertEqual(contents, {'binary.file'})


class ResourceFromNamespaceDiskTests(ResourceFromNamespaceTests, unittest.TestCase):
Expand Down

0 comments on commit aba2a7e

Please sign in to comment.