bpo-40924: Ensure importlib.resources.path returns an extant path (GH-20857)
This commit is contained in:
parent
e67f7db3c3
commit
2fb5f038f2
|
@ -7,11 +7,19 @@ class FileReader(abc.TraversableResources):
|
||||||
def __init__(self, loader):
|
def __init__(self, loader):
|
||||||
self.path = pathlib.Path(loader.path).parent
|
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):
|
def files(self):
|
||||||
return self.path
|
return self.path
|
||||||
|
|
||||||
|
|
||||||
class ZipReader(FileReader):
|
class ZipReader(abc.TraversableResources):
|
||||||
def __init__(self, loader, module):
|
def __init__(self, loader, module):
|
||||||
_, _, name = module.rpartition('.')
|
_, _, name = module.rpartition('.')
|
||||||
prefix = loader.prefix.replace('\\', '/') + name + '/'
|
prefix = loader.prefix.replace('\\', '/') + name + '/'
|
||||||
|
@ -28,3 +36,6 @@ class ZipReader(FileReader):
|
||||||
# for non-existent paths.
|
# for non-existent paths.
|
||||||
target = self.files().joinpath(path)
|
target = self.files().joinpath(path)
|
||||||
return target.is_file() and target.exists()
|
return target.is_file() and target.exists()
|
||||||
|
|
||||||
|
def files(self):
|
||||||
|
return self.path
|
||||||
|
|
|
@ -27,6 +27,15 @@ class PathTests:
|
||||||
class PathDiskTests(PathTests, unittest.TestCase):
|
class PathDiskTests(PathTests, unittest.TestCase):
|
||||||
data = data01
|
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):
|
class PathZipTests(PathTests, util.ZipSetup, unittest.TestCase):
|
||||||
def test_remove_in_context_manager(self):
|
def test_remove_in_context_manager(self):
|
||||||
|
|
|
@ -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.
|
Loading…
Reference in New Issue