Skip to content

Commit bd5afac

Browse files
committed
Merge branch 'issue51' into 'master'
Back port latest changes to importlib.resources Closes python#51 See merge request python-devs/importlib_resources!55
2 parents e421311 + 1af55e3 commit bd5afac

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

importlib_resources/docs/using.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ directories, the model still holds. So zip files can contain packages and
5959
resources, as could databases or other storage medium. In fact, while
6060
``importlib_resources`` supports physical file systems and zip files by
6161
default, anything that can be loaded with a Python import system `loader`_ can
62-
provide resources, as long as the loader implements the :ref:`ResourceReader
63-
<abc>` abstract base class.
62+
provide resources, as long as the loader implements the `ResourceReader`_
63+
abstract base class.
6464

6565

6666
Example
@@ -172,3 +172,4 @@ manager.
172172
173173
.. _`pkg_resources API`: http://setuptools.readthedocs.io/en/latest/pkg_resources.html#basic-resource-access
174174
.. _`loader`: https://docs.python.org/3/reference/import.html#finders-and-loaders
175+
.. _`ResourceReader`: https://docs.python.org/3.7/library/importlib.html#importlib.abc.ResourceReader

importlib_resources/tests/test_read.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import unittest
2-
32
import importlib_resources as resources
3+
44
from . import data01
55
from . import util
6+
from importlib import import_module
67

78

89
class CommonBinaryTests(util.CommonTests, unittest.TestCase):
@@ -46,7 +47,16 @@ class ReadDiskTests(ReadTests, unittest.TestCase):
4647

4748

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

5161

5262
if __name__ == '__main__':

importlib_resources/tests/test_resource.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import sys
22
import unittest
3-
43
import importlib_resources as resources
4+
55
from . import data01
6-
from . import zipdata02
6+
from . import zipdata01, zipdata02
77
from . import util
8+
from importlib import import_module
89

910

1011
class ResourceTests:
@@ -94,7 +95,31 @@ def test_package_has_no_reader_fallback(self):
9495
self.assertFalse(resources.is_resource(module, 'A'))
9596

9697

97-
class ResourceFromZipsTest(util.ZipSetupBase, unittest.TestCase):
98+
class ResourceFromZipsTest01(util.ZipSetupBase, unittest.TestCase):
99+
ZIP_MODULE = zipdata01 # type: ignore
100+
101+
def test_is_submodule_resource(self):
102+
submodule = import_module('ziptestdata.subdirectory')
103+
self.assertTrue(
104+
resources.is_resource(submodule, 'binary.file'))
105+
106+
def test_read_submodule_resource_by_name(self):
107+
self.assertTrue(
108+
resources.is_resource('ziptestdata.subdirectory', 'binary.file'))
109+
110+
def test_submodule_contents(self):
111+
submodule = import_module('ziptestdata.subdirectory')
112+
self.assertEqual(
113+
set(resources.contents(submodule)),
114+
{'__init__.py', 'binary.file'})
115+
116+
def test_submodule_contents_by_name(self):
117+
self.assertEqual(
118+
set(resources.contents('ziptestdata.subdirectory')),
119+
{'__init__.py', 'binary.file'})
120+
121+
122+
class ResourceFromZipsTest02(util.ZipSetupBase, unittest.TestCase):
98123
ZIP_MODULE = zipdata02 # type: ignore
99124

100125
def test_unrelated_contents(self):

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ passenv =
2121
PYTHON*
2222
LANG*
2323
LC_*
24+
OMIT
2425
deps =
2526
cov,diffcov: coverage>=4.5
2627
diffcov: diff_cover
2728
setenv =
2829
cov: COVERAGE_PROCESS_START={[coverage]rcfile}
2930
cov: COVERAGE_OPTIONS="-p"
3031
cov: COVERAGE_FILE={toxinidir}/.coverage
32+
py27: OMIT=3
33+
py34,py35,py36,py37: OMIT=2
3134

3235

3336
[testenv:qa]

0 commit comments

Comments
 (0)