bpo-32248: Implement importlib.abc.ResourceReader by brettcannon · Pull Request #4892 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Doc/library/importlib.rst
38 changes: 38 additions & 0 deletions Lib/importlib/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,41 @@ def set_data(self, path, data):
"""

_register(SourceLoader, machinery.SourceFileLoader)


class ResourceReader(Loader):

"""Abstract base class for loaders to provide resource reading support."""

@abc.abstractmethod
def open_resource(self, resource):
"""Return an opened, file-like object for binary reading.

The 'resource' argument is expected to represent only a file name
and thus not contain any subdirectory components.

If the resource cannot be found, FileNotFoundError is raised.
"""
raise FileNotFoundError

@abc.abstractmethod
def resource_path(self, resource):
"""Return the file system path to the specified resource.

The 'resource' argument is expected to represent only a file name
and thus not contain any subdirectory components.

If the resource does not exist on the file system, raise
FileNotFoundError.
"""
raise FileNotFoundError

@abc.abstractmethod
def is_resource(self, name):
"""Return True if the named 'name' is consider a resource."""
raise FileNotFoundError

@abc.abstractmethod
def contents(self):
"""Return an iterator of strings over the contents of the package."""
return iter([])
39 changes: 39 additions & 0 deletions Lib/test/test_importlib/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,45 @@ def test_get_filename(self):
) = test_util.test_both(InspectLoaderDefaultsTests)


class ResourceReader:

def open_resource(self, *args, **kwargs):
return super().open_resource(*args, **kwargs)

def resource_path(self, *args, **kwargs):
return super().resource_path(*args, **kwargs)

def is_resource(self, *args, **kwargs):
return super().is_resource(*args, **kwargs)

def contents(self, *args, **kwargs):
return super().contents(*args, **kwargs)


class ResourceReaderDefaultsTests(ABCTestHarness):

SPLIT = make_abc_subclasses(ResourceReader)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is SPLIT required by ABCTestHarness? It seems an odd choice of terms.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and I didn't name it.


def test_open_resource(self):
with self.assertRaises(FileNotFoundError):
self.ins.open_resource('dummy_file')

def test_resource_path(self):
with self.assertRaises(FileNotFoundError):
self.ins.resource_path('dummy_file')

def test_is_resource(self):
with self.assertRaises(FileNotFoundError):
self.ins.is_resource('dummy_file')

def test_contents(self):
self.assertEqual([], list(self.ins.contents()))

(Frozen_RRDefaultTests,
Source_RRDefaultsTests
) = test_util.test_both(ResourceReaderDefaultsTests)


##### MetaPathFinder concrete methods ##########################################
class MetaPathFinderFindModuleTests:

Expand Down