Skip to content

Commit c3cefa6

Browse files
committed
Replace StopIteration with a TraversalError for use in capturing a failed traversal.
1 parent b923ce2 commit c3cefa6

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

importlib_resources/abc.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def contents(self) -> Iterable[str]:
5252
raise FileNotFoundError
5353

5454

55+
class TraversalError(Exception):
56+
pass
57+
58+
5559
@runtime_checkable
5660
class Traversable(Protocol):
5761
"""
@@ -108,9 +112,15 @@ def joinpath(self, *descendants: StrPath) -> "Traversable":
108112
path.parts for path in map(pathlib.PurePosixPath, descendants)
109113
)
110114
target = next(names)
111-
return next(
115+
matches = (
112116
traversable for traversable in self.iterdir() if traversable.name == target
113-
).joinpath(*names)
117+
)
118+
try:
119+
return next(matches).joinpath(*names)
120+
except StopIteration:
121+
raise TraversalError(
122+
"Target not found during traversal.", target, list(names)
123+
)
114124

115125
def __truediv__(self, child: StrPath) -> "Traversable":
116126
"""

0 commit comments

Comments
 (0)