Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30e507d

Browse files
authored
bpo-33151: Handle submodule resources (GH-6268)
1 parent da1734c commit 30e507d

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

Lib/importlib/resources.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,12 @@ def __init__(self, zipimporter, fullname):
267267
self.fullname = fullname
268268

269269
def open_resource(self, resource):
270-
path = f'{self.fullname}/{resource}'
270+
fullname_as_path = self.fullname.replace('.', '/')
271+
path = f'{fullname_as_path}/{resource}'
271272
try:
272273
return BytesIO(self.zipimporter.get_data(path))
273274
except OSError:
274-
raise FileNotFoundError
275+
raise FileNotFoundError(path)
275276

276277
def resource_path(self, resource):
277278
# All resources are in the zip file, so there is no path to the file.
@@ -282,7 +283,8 @@ def resource_path(self, resource):
282283
def is_resource(self, name):
283284
# Maybe we could do better, but if we can get the data, it's a
284285
# resource. Otherwise it isn't.
285-
path = f'{self.fullname}/{name}'
286+
fullname_as_path = self.fullname.replace('.', '/')
287+
path = f'{fullname_as_path}/{name}'
286288
try:
287289
self.zipimporter.get_data(path)
288290
except OSError:

Lib/test/test_importlib/test_read.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from importlib import resources
3+
from importlib import import_module, resources
44
from . import data01
55
from . import util
66

@@ -46,7 +46,16 @@ class ReadDiskTests(ReadTests, unittest.TestCase):
4646

4747

4848
class ReadZipTests(ReadTests, util.ZipSetup, unittest.TestCase):
49-
pass
49+
def test_read_submodule_resource(self):
50+
submodule = import_module('ziptestdata.subdirectory')
51+
result = resources.read_binary(
52+
submodule, 'binary.file')
53+
self.assertEqual(result, b'\0\1\2\3')
54+
55+
def test_read_submodule_resource_by_name(self):
56+
result = resources.read_binary(
57+
'ziptestdata.subdirectory', 'binary.file')
58+
self.assertEqual(result, b'\0\1\2\3')
5059

5160

5261
if __name__ == '__main__':

Lib/test/test_importlib/test_resource.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from . import data01
55
from . import zipdata02
66
from . import util
7-
from importlib import resources
7+
from importlib import resources, import_module
88

99

1010
class ResourceTests:
@@ -109,6 +109,26 @@ def test_unrelated_contents(self):
109109
set(resources.contents('ziptestdata.two')),
110110
{'__init__.py', 'resource2.txt'})
111111

112+
def test_is_submodule_resource(self):
113+
submodule = import_module('ziptestdata.subdirectory')
114+
self.assertTrue(
115+
resources.is_resource(submodule, 'binary.file'))
116+
117+
def test_read_submodule_resource_by_name(self):
118+
self.assertTrue(
119+
resources.is_resource('ziptestdata.subdirectory', 'binary.file'))
120+
121+
def test_submodule_contents(self):
122+
submodule = import_module('ziptestdata.subdirectory')
123+
self.assertEqual(
124+
set(resources.contents(submodule)),
125+
{'__init__.py', 'binary.file'})
126+
127+
def test_submodule_contents_by_name(self):
128+
self.assertEqual(
129+
set(resources.contents('ziptestdata.subdirectory')),
130+
{'__init__.py', 'binary.file'})
131+
112132

113133
class NamespaceTest(unittest.TestCase):
114134
def test_namespaces_cant_have_resources(self):

0 commit comments

Comments
 (0)