Skip to content

Commit 1e98e35

Browse files
committed
Adapt to importlib_resources
1 parent 189d15f commit 1e98e35

File tree

2 files changed

+46
-48
lines changed

2 files changed

+46
-48
lines changed

importlib_resources/functional.py

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
"""Simplified function-based API for importlib.resources
2-
3-
42
"""
53

6-
import os
74
import warnings
85

96
from ._common import files, as_file

importlib_resources/tests/test_functional.py

+46-45
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33

44
from test.support.warnings_helper import ignore_warnings, check_warnings
55

6-
import importlib.resources
6+
import importlib_resources as resources
77

88
# Since the functional API forwards to Traversable, we only test
99
# filesystem resources here -- not zip files, namespace packages etc.
1010
# We do test for two kinds of Anchor, though.
1111

1212

1313
class StringAnchorMixin:
14-
anchor01 = 'test.test_importlib.resources.data01'
15-
anchor02 = 'test.test_importlib.resources.data02'
14+
anchor01 = 'importlib_resources.tests.data01'
15+
anchor02 = 'importlib_resources.tests.data02'
1616

1717

1818
class ModuleAnchorMixin:
19-
from test.test_importlib.resources import data01 as anchor01
20-
from test.test_importlib.resources import data02 as anchor02
19+
from . import data01 as anchor01
20+
from . import data02 as anchor02
2121

2222

2323
class FunctionalAPIBase():
@@ -34,39 +34,39 @@ def _gen_resourcetxt_path_parts(self):
3434

3535
def test_read_text(self):
3636
self.assertEqual(
37-
importlib.resources.read_text(self.anchor01, 'utf-8.file'),
37+
resources.read_text(self.anchor01, 'utf-8.file'),
3838
'Hello, UTF-8 world!\n',
3939
)
4040
self.assertEqual(
41-
importlib.resources.read_text(
41+
resources.read_text(
4242
self.anchor02, 'subdirectory', 'subsubdir', 'resource.txt',
4343
encoding='utf-8',
4444
),
4545
'a resource',
4646
)
4747
for path_parts in self._gen_resourcetxt_path_parts():
4848
self.assertEqual(
49-
importlib.resources.read_text(
49+
resources.read_text(
5050
self.anchor02, *path_parts, encoding='utf-8',
5151
),
5252
'a resource',
5353
)
5454
# Use generic OSError, since e.g. attempting to read a directory can
5555
# fail with PermissionError rather than IsADirectoryError
5656
with self.assertRaises(OSError):
57-
importlib.resources.read_text(self.anchor01)
57+
resources.read_text(self.anchor01)
5858
with self.assertRaises(OSError):
59-
importlib.resources.read_text(self.anchor01, 'no-such-file')
59+
resources.read_text(self.anchor01, 'no-such-file')
6060
with self.assertRaises(UnicodeDecodeError):
61-
importlib.resources.read_text(self.anchor01, 'utf-16.file')
61+
resources.read_text(self.anchor01, 'utf-16.file')
6262
self.assertEqual(
63-
importlib.resources.read_text(
63+
resources.read_text(
6464
self.anchor01, 'binary.file', encoding='latin1',
6565
),
6666
'\x00\x01\x02\x03',
6767
)
6868
self.assertEqual(
69-
importlib.resources.read_text(
69+
resources.read_text(
7070
self.anchor01, 'utf-16.file',
7171
errors='backslashreplace',
7272
),
@@ -77,38 +77,38 @@ def test_read_text(self):
7777

7878
def test_read_binary(self):
7979
self.assertEqual(
80-
importlib.resources.read_binary(self.anchor01, 'utf-8.file'),
80+
resources.read_binary(self.anchor01, 'utf-8.file'),
8181
b'Hello, UTF-8 world!\n',
8282
)
8383
for path_parts in self._gen_resourcetxt_path_parts():
8484
self.assertEqual(
85-
importlib.resources.read_binary(self.anchor02, *path_parts),
85+
resources.read_binary(self.anchor02, *path_parts),
8686
b'a resource',
8787
)
8888

8989
def test_open_text(self):
90-
with importlib.resources.open_text(self.anchor01, 'utf-8.file') as f:
90+
with resources.open_text(self.anchor01, 'utf-8.file') as f:
9191
self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
9292
for path_parts in self._gen_resourcetxt_path_parts():
93-
with importlib.resources.open_text(
93+
with resources.open_text(
9494
self.anchor02, *path_parts,
9595
encoding='utf-8',
9696
) as f:
9797
self.assertEqual(f.read(), 'a resource')
9898
# Use generic OSError, since e.g. attempting to read a directory can
9999
# fail with PermissionError rather than IsADirectoryError
100100
with self.assertRaises(OSError):
101-
importlib.resources.open_text(self.anchor01)
101+
resources.open_text(self.anchor01)
102102
with self.assertRaises(OSError):
103-
importlib.resources.open_text(self.anchor01, 'no-such-file')
104-
with importlib.resources.open_text(self.anchor01, 'utf-16.file') as f:
103+
resources.open_text(self.anchor01, 'no-such-file')
104+
with resources.open_text(self.anchor01, 'utf-16.file') as f:
105105
with self.assertRaises(UnicodeDecodeError):
106106
f.read()
107-
with importlib.resources.open_text(
107+
with resources.open_text(
108108
self.anchor01, 'binary.file', encoding='latin1',
109109
) as f:
110110
self.assertEqual(f.read(), '\x00\x01\x02\x03')
111-
with importlib.resources.open_text(
111+
with resources.open_text(
112112
self.anchor01, 'utf-16.file',
113113
errors='backslashreplace',
114114
) as f:
@@ -120,24 +120,24 @@ def test_open_text(self):
120120
)
121121

122122
def test_open_binary(self):
123-
with importlib.resources.open_binary(self.anchor01, 'utf-8.file') as f:
123+
with resources.open_binary(self.anchor01, 'utf-8.file') as f:
124124
self.assertEqual(f.read(), b'Hello, UTF-8 world!\n')
125125
for path_parts in self._gen_resourcetxt_path_parts():
126-
with importlib.resources.open_binary(
126+
with resources.open_binary(
127127
self.anchor02, *path_parts,
128128
) as f:
129129
self.assertEqual(f.read(), b'a resource')
130130

131131
def test_path(self):
132-
with importlib.resources.path(self.anchor01, 'utf-8.file') as path:
133-
with open(str(path)) as f:
132+
with resources.path(self.anchor01, 'utf-8.file') as path:
133+
with open(str(path), encoding='utf-8') as f:
134134
self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
135-
with importlib.resources.path(self.anchor01) as path:
136-
with open(os.path.join(path, 'utf-8.file')) as f:
135+
with resources.path(self.anchor01) as path:
136+
with open(os.path.join(path, 'utf-8.file'), encoding='utf-8') as f:
137137
self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
138138

139139
def test_is_resource(self):
140-
is_resource = importlib.resources.is_resource
140+
is_resource = resources.is_resource
141141
self.assertTrue(is_resource(self.anchor01, 'utf-8.file'))
142142
self.assertFalse(is_resource(self.anchor01, 'no_such_file'))
143143
self.assertFalse(is_resource(self.anchor01))
@@ -146,24 +146,25 @@ def test_is_resource(self):
146146
self.assertTrue(is_resource(self.anchor02, *path_parts))
147147

148148
def test_contents(self):
149-
is_resource = importlib.resources.is_resource
150149
with check_warnings((".*contents.*", DeprecationWarning)):
151-
c = importlib.resources.contents(self.anchor01)
150+
c = resources.contents(self.anchor01)
152151
self.assertGreaterEqual(
153152
set(c),
154153
{'utf-8.file', 'utf-16.file', 'binary.file', 'subdirectory'},
155154
)
156-
with (self.assertRaises(OSError),
155+
with (
156+
self.assertRaises(OSError),
157157
check_warnings((".*contents.*", DeprecationWarning)),
158158
):
159-
importlib.resources.contents(self.anchor01, 'utf-8.file')
159+
list(resources.contents(self.anchor01, 'utf-8.file'))
160160
for path_parts in self._gen_resourcetxt_path_parts():
161-
with (self.assertRaises(OSError),
161+
with (
162+
self.assertRaises(OSError),
162163
check_warnings((".*contents.*", DeprecationWarning)),
163164
):
164-
importlib.resources.contents(self.anchor01, *path_parts)
165+
list(resources.contents(self.anchor01, *path_parts))
165166
with check_warnings((".*contents.*", DeprecationWarning)):
166-
c = importlib.resources.contents(self.anchor01, 'subdirectory')
167+
c = resources.contents(self.anchor01, 'subdirectory')
167168
self.assertGreaterEqual(
168169
set(c),
169170
{'binary.file'},
@@ -172,13 +173,13 @@ def test_contents(self):
172173
@ignore_warnings(category=DeprecationWarning)
173174
def test_common_errors(self):
174175
for func in (
175-
importlib.resources.read_text,
176-
importlib.resources.read_binary,
177-
importlib.resources.open_text,
178-
importlib.resources.open_binary,
179-
importlib.resources.path,
180-
importlib.resources.is_resource,
181-
importlib.resources.contents,
176+
resources.read_text,
177+
resources.read_binary,
178+
resources.open_text,
179+
resources.open_binary,
180+
resources.path,
181+
resources.is_resource,
182+
resources.contents,
182183
):
183184
with self.subTest(func=func):
184185
# Rejecting None anchor
@@ -193,8 +194,8 @@ def test_common_errors(self):
193194

194195
def test_text_errors(self):
195196
for func in (
196-
importlib.resources.read_text,
197-
importlib.resources.open_text,
197+
resources.read_text,
198+
resources.open_text,
198199
):
199200
with self.subTest(func=func):
200201
# Multiple path arguments need explicit encoding argument.

0 commit comments

Comments
 (0)