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