8000 Catch `TraversalError`, raised by `Traversable.joinpath()` · python/importlib_resources@f10a2e9 · GitHub
[go: up one dir, main page]

Skip to content

Commit f10a2e9

Browse files
kurtmckeejaraco
authored andcommitted
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 912a9e5 commit f10a2e9

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 as_file, files
6+
from .abc import TraversalError
67

78
_MISSING = object()
89

@@ -41,7 +42,10 @@ def is_resource(anchor, *path_names):
4142
4243
Otherwise returns ``False``.
4344
"""
44-
return _get_resource(anchor, path_names).is_file()
45+
try:
46+
return _get_resource(anchor, path_names).is_file()
47+
except TraversalError:
48+
return False
4549

4650

4751
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
@@ -72,7 +72,7 @@ def test_read_text(self):
7272
# fail with PermissionError rather than IsADirectoryError
7373
with self.assertRaises(OSError):
7474
resources.read_text(self.anchor01)
75-
with self.assertRaises(OSError):
75+
with self.assertRaises((OSError, resources.abc.TraversalError)):
7676
resources.read_text(self.anchor01, 'no-such-file')
7777
with self.assertRaises(UnicodeDecodeError):
7878
resources.read_text(self.anchor01, 'utf-16.file')
@@ -120,7 +120,7 @@ def test_open_text(self):
120120
# fail with PermissionError rather than IsADirectoryError
121121
with self.assertRaises(OSError):
122122
resources.open_text(self.anchor01)
123-
with self.assertRaises(OSError):
123+
with self.assertRaises((OSError, resources.abc.TraversalError)):
124124
resources.open_text(self.anchor01, 'no-such-file')
125125
with resources.open_text(self.anchor01, 'utf-16.file') as f:
126126
with self.assertRaises(UnicodeDecodeError):
@@ -188,7 +188,7 @@ def test_contents(self):
188188

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

0 commit comments

Comments
 (0)
0