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
13 changes: 12 additions & 1 deletion Lib/importlib/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ class FileReader(abc.TraversableResources):
def __init__(self, loader):
self.path = pathlib.Path(loader.path).parent

def resource_path(self, resource):
"""
Return the file system path to prevent
`resources.path()` from creating a temporary
copy.
"""
return str(self.path.joinpath(resource))

def files(self):
return self.path


class ZipReader(FileReader):
class ZipReader(abc.TraversableResources):

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 this a safe (as in backward-compatible) change?

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. The main reason this was here was to supply FileReader.files, but now it has its own copy.

def __init__(self, loader, module):
_, _, name = module.rpartition('.')
prefix = loader.prefix.replace('\\', '/') + name + '/'
Expand All @@ -28,3 +36,6 @@ def is_resource(self, path):
# for non-existent paths.
target = self.files().joinpath(path)
return target.is_file() and target.exists()

def files(self):
return self.path
9 changes: 9 additions & 0 deletions Lib/test/test_importlib/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ def test_reading(self):
class PathDiskTests(PathTests, unittest.TestCase):
data = data01

def test_natural_path(self):
"""
Guarantee the internal implementation detail that
file-system-backed resources do not get the tempdir
treatment.
"""
with resources.path(self.data, 'utf-8.file') as path:
assert 'data' in str(path)


class PathZipTests(PathTests, util.ZipSetup, unittest.TestCase):
def test_remove_in_context_manager(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Ensure ``importlib.resources.path`` returns an extant path for the
SourceFileLoader's resource reader. Avoids the regression identified in
master while a long-term solution is devised.