1
1
import unittest
2
2
import os
3
3
4
- from test .support . warnings_helper import ignore_warnings , check_warnings
4
+ from test .support import warnings_helper
5
5
6
- import importlib . resources as resources
6
+ from importlib import resources
7
7
8
8
# Since the functional API forwards to Traversable, we only test
9
9
# filesystem resources here -- not zip files, namespace packages etc.
@@ -22,8 +22,7 @@ class ModuleAnchorMixin:
22
22
23
23
class FunctionalAPIBase :
24
24
def _gen_resourcetxt_path_parts (self ):
25
- """Yield various names of a text file in anchor02, each in a subTest
26
- """
25
+ """Yield various names of a text file in anchor02, each in a subTest"""
27
26
for path_parts in (
28
27
('subdirectory' , 'subsubdir' , 'resource.txt' ),
29
28
('subdirectory/subsubdir/resource.txt' ,),
@@ -36,7 +35,7 @@ def assertEndsWith(self, string, suffix):
36
35
"""Assert that `string` ends with `suffix`.
37
36
38
37
Used to ignore an architecture-specific UTF-16 byte-order mark."""
39
- self .assertEqual (string [- len (suffix ):], suffix )
38
+ self .assertEqual (string [- len (suffix ) :], suffix )
40
39
41
40
def test_read_text (self ):
42
41
self .assertEqual (
@@ -45,15 +44,20 @@ def test_read_text(self):
45
44
)
46
45
self .assertEqual (
47
46
resources .read_text (
48
- self .anchor02 , 'subdirectory' , 'subsubdir' , 'resource.txt' ,
47
+ self .anchor02 ,
48
+ 'subdirectory' ,
49
+ 'subsubdir' ,
50
+ 'resource.txt' ,
49
51
encoding = 'utf-8' ,
50
52
),
51
53
'a resource' ,
52
54
)
53
55
for path_parts in self ._gen_resourcetxt_path_parts ():
54
56
self .assertEqual (
55
57
resources .read_text (
56
- self .anchor02 , * path_parts , encoding = 'utf-8' ,
58
+ self .anchor02 ,
59
+ * path_parts ,
60
+ encoding = 'utf-8' ,
57
61
),
58
62
'a resource' ,
59
63
)
@@ -67,13 +71,16 @@ def test_read_text(self):
67
71
resources .read_text (self .anchor01 , 'utf-16.file' )
68
72
self .assertEqual (
69
73
resources .read_text (
70
- self .anchor01 , 'binary.file' , encoding = 'latin1' ,
74
+ self .anchor01 ,
75
+ 'binary.file' ,
76
+ encoding = 'latin1' ,
71
77
),
72
78
'\x00 \x01 \x02 \x03 ' ,
73
79
)
74
80
self .assertEndsWith ( # ignore the BOM
75
81
resources .read_text (
76
- self .anchor01 , 'utf-16.file' ,
82
+ self .anchor01 ,
83
+ 'utf-16.file' ,
77
84
errors = 'backslashreplace' ,
78
85
),
79
86
'Hello, UTF-16 world!\n ' .encode ('utf-16-le' ).decode (
@@ -97,7 +104,8 @@ def test_open_text(self):
97
104
self .assertEqual (f .read (), 'Hello, UTF-8 world!\n ' )
98
105
for path_parts in self ._gen_resourcetxt_path_parts ():
99
106
with resources .open_text (
100
- self .anchor02 , * path_parts ,
107
+ self .anchor02 ,
108
+ * path_parts ,
101
109
encoding = 'utf-8' ,
102
110
) as f :
103
111
self .assertEqual (f .read (), 'a resource' )
@@ -111,11 +119,14 @@ def test_open_text(self):
111
119
with self .assertRaises (UnicodeDecodeError ):
112
120
f .read ()
113
121
with resources .open_text (
114
- self .anchor01 , 'binary.file' , encoding = 'latin1' ,
122
+ self .anchor01 ,
123
+ 'binary.file' ,
124
+ encoding = 'latin1' ,
115
125
) as f :
116
126
self .assertEqual (f .read (), '\x00 \x01 \x02 \x03 ' )
117
127
with resources .open_text (
118
- self .anchor01 , 'utf-16.file' ,
128
+ self .anchor01 ,
129
+ 'utf-16.file' ,
119
130
errors = 'backslashreplace' ,
120
131
) as f :
121
132
self .assertEndsWith ( # ignore the BOM
@@ -130,16 +141,17 @@ def test_open_binary(self):
130
141
self .assertEqual (f .read (), b'Hello, UTF-8 world!\n ' )
131
142
for path_parts in self ._gen_resourcetxt_path_parts ():
132
143
with resources .open_binary (
133
- self .anchor02 , * path_parts ,
144
+ self .anchor02 ,
145
+ * path_parts ,
134
146
) as f :
135
147
self .assertEqual (f .read (), b'a resource' )
136
148
137
149
def test_path (self ):
138
150
with resources .path (self .anchor01 , 'utf-8.file' ) as path :
139
- with open (str (path )) as f :
151
+ with open (str (path ), encoding = 'utf-8' ) as f :
140
152
self .assertEqual (f .read (), 'Hello, UTF-8 world!\n ' )
141
153
with resources .path (self .anchor01 ) as path :
142
- with open (os .path .join (path , 'utf-8.file' )) as f :
154
+ with open (os .path .join (path , 'utf-8.file' ), encoding = 'utf-8' ) as f :
143
155
self .assertEqual (f .read (), 'Hello, UTF-8 world!\n ' )
144
156
145
157
def test_is_resource (self ):
@@ -152,32 +164,32 @@ def test_is_resource(self):
152
164
self .assertTrue (is_resource (self .anchor02 , * path_parts ))
153
165
154
166
def test_contents (self ):
155
- is_resource = resources .is_resource
156
- with check_warnings ((".*contents.*" , DeprecationWarning )):
167
+ with warnings_helper .check_warnings ((".*contents.*" , DeprecationWarning )):
157
168
c = resources .contents (self .anchor01 )
158
169
self .assertGreaterEqual (
159
170
set (c ),
160
171
{'utf-8.file' , 'utf-16.file' , 'binary.file' , 'subdirectory' },
161
172
)
162
- with (
163
- self . assertRaises ( OSError ) ,
164
- check_warnings (( ".*contents.*" , DeprecationWarning )) ,
165
- ):
173
+ with self . assertRaises ( OSError ), warnings_helper . check_warnings ( (
174
+ ".*contents.*" ,
175
+ DeprecationWarning ,
176
+ )) :
166
177
list (resources .contents (self .anchor01 , 'utf-8.file' ))
178
+
167
179
for path_parts in self ._gen_resourcetxt_path_parts ():
168
- with (
169
- self . assertRaises ( OSError ) ,
170
- check_warnings (( ".*contents.*" , DeprecationWarning )) ,
171
- ):
180
+ with self . assertRaises ( OSError ), warnings_helper . check_warnings ( (
181
+ ".*contents.*" ,
182
+ DeprecationWarning ,
183
+ )) :
172
184
list (resources .contents (self .anchor01 , * path_parts ))
173
- with check_warnings ((".*contents.*" , DeprecationWarning )):
185
+ with warnings_helper . check_warnings ((".*contents.*" , DeprecationWarning )):
174
186
c = resources .contents (self .anchor01 , 'subdirectory' )
175
187
self .assertGreaterEqual (
176
188
set (c ),
177
189
{'binary.file' },
178
190
)
179
191
180
- @ignore_warnings (category = DeprecationWarning )
192
+ @warnings_helper . ignore_warnings (category = DeprecationWarning )
181
193
def test_common_errors (self ):
182
194
for func in (
183
195
resources .read_text ,
@@ -208,18 +220,24 @@ def test_text_errors(self):
208
220
# Multiple path arguments need explicit encoding argument.
209
221
with self .assertRaises (TypeError ):
210
222
func (
211
- self .anchor02 , 'subdirectory' ,
212
- 'subsubdir' , 'resource.txt' ,
223
+ self .anchor02 ,
224
+ 'subdirectory' ,
225
+ 'subsubdir' ,
226
+ 'resource.txt' ,
213
227
)
214
228
215
229
216
230
class FunctionalAPITest_StringAnchor (
217
- unittest .TestCase , FunctionalAPIBase , StringAnchorMixin ,
231
+ unittest .TestCase ,
232
+ FunctionalAPIBase ,
233
+ StringAnchorMixin ,
218
234
):
219
235
pass
220
236
221
237
222
238
class FunctionalAPITest_ModuleAnchor (
223
- unittest .TestCase , FunctionalAPIBase , ModuleAnchorMixin ,
239
+ unittest .TestCase ,
240
+ FunctionalAPIBase ,
241
+ ModuleAnchorMixin ,
224
242
):
225
243
pass
0 commit comments