@@ -61,8 +61,24 @@ def __truediv__(self, child):
6161
6262
6363def from_package (package ):
64- """Return a Traversable object for the given package"""
64+ """
65+ Return a Traversable object for the given package.
66+
67+ """
6568 spec = package_spec (package )
69+ return from_traversable_resources (spec ) or fallback_resources (spec )
70+
71+
72+ def from_traversable_resources (spec ):
73+ """
74+ If the spec.loader implements TraversableResources,
75+ directly or implicitly, it will have a ``files()`` method.
76+ """
77+ with contextlib .suppress (AttributeError ):
78+ return spec .loader .files ()
79+
80+
81+ def fallback_resources (spec ):
6682 package_directory = Path (spec .origin ).parent
6783 try :
6884 archive_path = spec .loader .archive
@@ -94,7 +110,7 @@ def _tempfile(reader):
94110@contextlib .contextmanager
95111def as_file (path ):
96112 """
97- Given a path-like object, return that object as a
113+ Given a Traversable object, return that object as a
98114 path on the local file system in a context manager.
99115 """
100116 with _tempfile (path .read_bytes ) as local :
@@ -105,6 +121,6 @@ def as_file(path):
105121@contextlib .contextmanager
106122def _ (path ):
107123 """
108- Degenerate behavior for pathlib.Path objects
124+ Degenerate behavior for pathlib.Path objects.
109125 """
110126 yield path
0 commit comments