Skip to content

Commit ec22ea9

Browse files
committed
Catch TraversalError, raised by Traversable.joinpath()
Exercising the `Traversable` protocol's concrete methods has highlighted that `.joinpath()` raises `TraversalError`, which needs to be caught in several places. This is primarily resolved within the test suite, but implicates the `is_resource()` function as well.
1 parent 0f15ae6 commit ec22ea9

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

importlib_resources/_functional.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import warnings
44

55
from ._common import files, as_file
6+
from .abc import TraversalError
67

78

89
_MISSING = object()
@@ -42,7 +43,10 @@ def is_resource(anchor, *path_names):
4243
4344
Otherwise returns ``False``.
4445
"""
45-
return _get_resource(anchor, path_names).is_file()
46+
try:
47+
return _get_resource(anchor, path_names).is_file()
48+
except TraversalError:
49+
return False
4650

4751

4852
def contents(anchor, *path_names):

importlib_resources/tests/test_functional.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_read_text(self):
7373
# fail with PermissionError rather than IsADirectoryError
7474
with self.assertRaises(OSError):
7575
resources.read_text(self.anchor01)
76-
with self.assertRaises(OSError):
76+
with self.assertRaises((OSError, resources.abc.TraversalError)):
7777
resources.read_text(self.anchor01, 'no-such-file')
7878
with self.assertRaises(UnicodeDecodeError):
7979
resources.read_text(self.anchor01, 'utf-16.file')
@@ -121,7 +121,7 @@ def test_open_text(self):
121121
# fail with PermissionError rather than IsADirectoryError
122122
with self.assertRaises(OSError):
123123
resources.open_text(self.anchor01)
124-
with self.assertRaises(OSError):
124+
with self.assertRaises((OSError, resources.abc.TraversalError)):
125125
resources.open_text(self.anchor01, 'no-such-file')
126126
with resources.open_text(self.anchor01, 'utf-16.file') as f:
127127
with self.assertRaises(UnicodeDecodeError):
@@ -189,7 +189,7 @@ def test_contents(self):
189189

190190
for path_parts in self._gen_resourcetxt_path_parts():
191191
with (
192-
self.assertRaises(OSError),
192+
self.assertRaises((OSError, resources.abc.TraversalError)),
193193
warnings_helper.check_warnings((
194194
".*contents.*",
195195
DeprecationWarning,

0 commit comments

Comments
 (0)