Skip to content

Commit

Permalink
Give precedence to native traversable readers.
Browse files Browse the repository at this point in the history
Ensure that standard library readers are replaced while third-party readers are passed along. Closes #295.
  • Loading branch information
jaraco committed Mar 6, 2024
1 parent f3f4b0a commit bf02178
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion importlib_resources/future/adapters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import pathlib
from contextlib import suppress
from types import SimpleNamespace
Expand All @@ -15,7 +16,18 @@ class TraversableResourcesLoader(_adapters.TraversableResourcesLoader):
"""

def get_resource_reader(self, name):
return self._standard_reader() or super().get_resource_reader(name)
with contextlib.suppress(Exception):
return self._block_standard(super().get_resource_reader(name))
return self._standard_reader()

def _block_standard(self, reader):
"""
If the reader is from the standard library, raise an exception to
allow likely newer implementations in this library to take precedence.
"""
if reader.__class__.__module__.startswith('importlib.resources.'):
raise RuntimeError("Reader blocked to be superseded.")
return reader

def _standard_reader(self):
return self._zip_reader() or self._namespace_reader() or self._file_reader()
Expand Down

0 comments on commit bf02178

Please sign in to comment.