Skip to content

Commit

Permalink
Merge pull request #254 from python/bugfix/253-multiplexed-path-compo…
Browse files Browse the repository at this point in the history
…und-path-non-existent-parent

Restore lenience in MultiplexedPath.joinpath for missing directories
  • Loading branch information
jaraco authored Jul 22, 2022
2 parents 5a14cdc + 41d8e32 commit b8da844
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
v5.8.1
======

* #253: In ``MultiplexedPath``, restore expectation that
a compound path with a non-existent directory does not
raise an exception.

v5.8.0
======

Expand Down
11 changes: 4 additions & 7 deletions importlib_resources/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,10 @@ def is_file(self):
def joinpath(self, *descendants):
try:
return super().joinpath(*descendants)
except abc.TraversalError as exc:
# One of the paths didn't resolve.
msg, target, names = exc.args
if names: # pragma: nocover
raise
# It was the last; construct result with the first path.
return self._paths[0].joinpath(target)
except abc.TraversalError:
# One of the paths did not resolve (a directory does not exist).
# Just return something that will not exist.
return self._paths[0].joinpath(*descendants)

def open(self, *args, **kwargs):
raise FileNotFoundError(f'{self} is not a file')
Expand Down
4 changes: 4 additions & 0 deletions importlib_resources/tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def test_join_path(self):
)
self.assertEqual(path.joinpath(), path)

def test_join_path_compound(self):
path = MultiplexedPath(self.folder)
assert not path.joinpath('imaginary/foo.py').exists()

def test_repr(self):
self.assertEqual(
repr(MultiplexedPath(self.folder)),
Expand Down

0 comments on commit b8da844

Please sign in to comment.