[Python-checkins] bpo-33151: Handle submodule resources (GH-6268) (GH-6270)

Barry Warsaw webhook-mailer at python.org
Tue Mar 27 13:25:34 EDT 2018


https://github.com/python/cpython/commit/fd1b8f87b3e63495cadd37c8f1b90191b46ee52a
commit: fd1b8f87b3e63495cadd37c8f1b90191b46ee52a
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Barry Warsaw <barry at python.org>
date: 2018-03-27T10:25:28-07:00
summary:

bpo-33151: Handle submodule resources (GH-6268) (GH-6270)

(cherry picked from commit 30e507dff465a31901d87df791a2bac40dc88530)

Co-authored-by: Barry Warsaw <barry at python.org>

files:
M Lib/importlib/resources.py
M Lib/test/test_importlib/test_read.py
M Lib/test/test_importlib/test_resource.py

diff --git a/Lib/importlib/resources.py b/Lib/importlib/resources.py
index c4f6bbde45fa..e5654273c06a 100644
--- a/Lib/importlib/resources.py
+++ b/Lib/importlib/resources.py
@@ -267,11 +267,12 @@ def __init__(self, zipimporter, fullname):
         self.fullname = fullname
 
     def open_resource(self, resource):
-        path = f'{self.fullname}/{resource}'
+        fullname_as_path = self.fullname.replace('.', '/')
+        path = f'{fullname_as_path}/{resource}'
         try:
             return BytesIO(self.zipimporter.get_data(path))
         except OSError:
-            raise FileNotFoundError
+            raise FileNotFoundError(path)
 
     def resource_path(self, resource):
         # All resources are in the zip file, so there is no path to the file.
@@ -282,7 +283,8 @@ def resource_path(self, resource):
     def is_resource(self, name):
         # Maybe we could do better, but if we can get the data, it's a
         # resource.  Otherwise it isn't.
-        path = f'{self.fullname}/{name}'
+        fullname_as_path = self.fullname.replace('.', '/')
+        path = f'{fullname_as_path}/{name}'
         try:
             self.zipimporter.get_data(path)
         except OSError:
diff --git a/Lib/test/test_importlib/test_read.py b/Lib/test/test_importlib/test_read.py
index 231f5017b688..ff78d0b2948f 100644
--- a/Lib/test/test_importlib/test_read.py
+++ b/Lib/test/test_importlib/test_read.py
@@ -1,6 +1,6 @@
 import unittest
 
-from importlib import resources
+from importlib import import_module, resources
 from . import data01
 from . import util
 
@@ -46,7 +46,16 @@ class ReadDiskTests(ReadTests, unittest.TestCase):
 
 
 class ReadZipTests(ReadTests, util.ZipSetup, unittest.TestCase):
-    pass
+    def test_read_submodule_resource(self):
+        submodule = import_module('ziptestdata.subdirectory')
+        result = resources.read_binary(
+            submodule, 'binary.file')
+        self.assertEqual(result, b'\0\1\2\3')
+
+    def test_read_submodule_resource_by_name(self):
+        result = resources.read_binary(
+            'ziptestdata.subdirectory', 'binary.file')
+        self.assertEqual(result, b'\0\1\2\3')
 
 
 if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/test_resource.py b/Lib/test/test_importlib/test_resource.py
index c38ad0358a07..d717e1dd04da 100644
--- a/Lib/test/test_importlib/test_resource.py
+++ b/Lib/test/test_importlib/test_resource.py
@@ -4,7 +4,7 @@
 from . import data01
 from . import zipdata02
 from . import util
-from importlib import resources
+from importlib import resources, import_module
 
 
 class ResourceTests:
@@ -109,6 +109,26 @@ def test_unrelated_contents(self):
             set(resources.contents('ziptestdata.two')),
             {'__init__.py', 'resource2.txt'})
 
+    def test_is_submodule_resource(self):
+        submodule = import_module('ziptestdata.subdirectory')
+        self.assertTrue(
+            resources.is_resource(submodule, 'binary.file'))
+
+    def test_read_submodule_resource_by_name(self):
+        self.assertTrue(
+            resources.is_resource('ziptestdata.subdirectory', 'binary.file'))
+
+    def test_submodule_contents(self):
+        submodule = import_module('ziptestdata.subdirectory')
+        self.assertEqual(
+            set(resources.contents(submodule)),
+            {'__init__.py', 'binary.file'})
+
+    def test_submodule_contents_by_name(self):
+        self.assertEqual(
+            set(resources.contents('ziptestdata.subdirectory')),
+            {'__init__.py', 'binary.file'})
+
 
 class NamespaceTest(unittest.TestCase):
     def test_namespaces_cant_have_resources(self):



More information about the Python-checkins mailing list