[Python-checkins] bpo-40924: Ensure importlib.resources.path returns an extant path (GH-20857)

Jason R. Coombs webhook-mailer at python.org
Mon Jun 29 16:59:30 EDT 2020


https://github.com/python/cpython/commit/2fb5f038f2a2e91a7293d62dfd5601e6eb500c55
commit: 2fb5f038f2a2e91a7293d62dfd5601e6eb500c55
branch: master
author: Jason R. Coombs <jaraco at jaraco.com>
committer: GitHub <noreply at github.com>
date: 2020-06-29T22:59:22+02:00
summary:

bpo-40924: Ensure importlib.resources.path returns an extant path (GH-20857)

files:
A Misc/NEWS.d/next/Library/2020-06-13-12-04-50.bpo-40924.SM_luS.rst
M Lib/importlib/readers.py
M Lib/test/test_importlib/test_path.py

diff --git a/Lib/importlib/readers.py b/Lib/importlib/readers.py
index fb49ebe2b1642..6331e4daf4313 100644
--- a/Lib/importlib/readers.py
+++ b/Lib/importlib/readers.py
@@ -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):
     def __init__(self, loader, module):
         _, _, name = module.rpartition('.')
         prefix = loader.prefix.replace('\\', '/') + name + '/'
@@ -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
diff --git a/Lib/test/test_importlib/test_path.py b/Lib/test/test_importlib/test_path.py
index c4e7285411322..abf8086558158 100644
--- a/Lib/test/test_importlib/test_path.py
+++ b/Lib/test/test_importlib/test_path.py
@@ -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):
diff --git a/Misc/NEWS.d/next/Library/2020-06-13-12-04-50.bpo-40924.SM_luS.rst b/Misc/NEWS.d/next/Library/2020-06-13-12-04-50.bpo-40924.SM_luS.rst
new file mode 100644
index 0000000000000..4e4c6e88ac572
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-06-13-12-04-50.bpo-40924.SM_luS.rst
@@ -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.



More information about the Python-checkins mailing list