11import unittest
22import os
33
4- from test .support . warnings_helper import ignore_warnings , check_warnings
4+ from test .support import warnings_helper
55
6- import importlib . resources as resources
6+ from importlib import resources
77
88# Since the functional API forwards to Traversable, we only test
99# filesystem resources here -- not zip files, namespace packages etc.
@@ -22,8 +22,7 @@ class ModuleAnchorMixin:
2222
2323class FunctionalAPIBase :
2424 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"""
2726 for path_parts in (
2827 ('subdirectory' , 'subsubdir' , 'resource.txt' ),
2928 ('subdirectory/subsubdir/resource.txt' ,),
@@ -36,7 +35,7 @@ def assertEndsWith(self, string, suffix):
3635 """Assert that `string` ends with `suffix`.
3736
3837 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 )
4039
4140 def test_read_text (self ):
4241 self .assertEqual (
@@ -45,15 +44,20 @@ def test_read_text(self):
4544 )
4645 self .assertEqual (
4746 resources .read_text (
48- self .anchor02 , 'subdirectory' , 'subsubdir' , 'resource.txt' ,
47+ self .anchor02 ,
48+ 'subdirectory' ,
49+ 'subsubdir' ,
50+ 'resource.txt' ,
4951 encoding = 'utf-8' ,
5052 ),
5153 'a resource' ,
5254 )
5355 for path_parts in self ._gen_resourcetxt_path_parts ():
5456 self .assertEqual (
5557 resources .read_text (
56- self .anchor02 , * path_parts , encoding = 'utf-8' ,
58+ self .anchor02 ,
59+ * path_parts ,
60+ encoding = 'utf-8' ,
5761 ),
5862 'a resource' ,
5963 )
@@ -67,13 +71,16 @@ def test_read_text(self):
6771 resources .read_text (self .anchor01 , 'utf-16.file' )
6872 self .assertEqual (
6973 resources .read_text (
70- self .anchor01 , 'binary.file' , encoding = 'latin1' ,
74+ self .anchor01 ,
75+ 'binary.file' ,
76+ encoding = 'latin1' ,
7177 ),
7278 '\x00 \x01 \x02 \x03 ' ,
7379 )
7480 self .assertEndsWith ( # ignore the BOM
7581 resources .read_text (
76- self .anchor01 , 'utf-16.file' ,
82+ self .anchor01 ,
83+ 'utf-16.file' ,
7784 errors = 'backslashreplace' ,
7885 ),
7986 'Hello, UTF-16 world!\n ' .encode ('utf-16-le' ).decode (
@@ -97,7 +104,8 @@ def test_open_text(self):
97104 self .assertEqual (f .read (), 'Hello, UTF-8 world!\n ' )
98105 for path_parts in self ._gen_resourcetxt_path_parts ():
99106 with resources .open_text (
100- self .anchor02 , * path_parts ,
107+ self .anchor02 ,
108+ * path_parts ,
101109 encoding = 'utf-8' ,
102110 ) as f :
103111 self .assertEqual (f .read (), 'a resource' )
@@ -111,11 +119,14 @@ def test_open_text(self):
111119 with self .assertRaises (UnicodeDecodeError ):
112120 f .read ()
113121 with resources .open_text (
114- self .anchor01 , 'binary.file' , encoding = 'latin1' ,
122+ self .anchor01 ,
123+ 'binary.file' ,
124+ encoding = 'latin1' ,
115125 ) as f :
116126 self .assertEqual (f .read (), '\x00 \x01 \x02 \x03 ' )
117127 with resources .open_text (
118- self .anchor01 , 'utf-16.file' ,
128+ self .anchor01 ,
129+ 'utf-16.file' ,
119130 errors = 'backslashreplace' ,
120131 ) as f :
121132 self .assertEndsWith ( # ignore the BOM
@@ -130,16 +141,17 @@ def test_open_binary(self):
130141 self .assertEqual (f .read (), b'Hello, UTF-8 world!\n ' )
131142 for path_parts in self ._gen_resourcetxt_path_parts ():
132143 with resources .open_binary (
133- self .anchor02 , * path_parts ,
144+ self .anchor02 ,
145+ * path_parts ,
134146 ) as f :
135147 self .assertEqual (f .read (), b'a resource' )
136148
137149 def test_path (self ):
138150 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 :
140152 self .assertEqual (f .read (), 'Hello, UTF-8 world!\n ' )
141153 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 :
143155 self .assertEqual (f .read (), 'Hello, UTF-8 world!\n ' )
144156
145157 def test_is_resource (self ):
@@ -152,32 +164,32 @@ def test_is_resource(self):
152164 self .assertTrue (is_resource (self .anchor02 , * path_parts ))
153165
154166 def test_contents (self ):
155- is_resource = resources .is_resource
156- with check_warnings ((".*contents.*" , DeprecationWarning )):
167+ with warnings_helper .check_warnings ((".*contents.*" , DeprecationWarning )):
157168 c = resources .contents (self .anchor01 )
158169 self .assertGreaterEqual (
159170 set (c ),
160171 {'utf-8.file' , 'utf-16.file' , 'binary.file' , 'subdirectory' },
161172 )
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+ )) :
166177 list (resources .contents (self .anchor01 , 'utf-8.file' ))
178+
167179 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+ )) :
172184 list (resources .contents (self .anchor01 , * path_parts ))
173- with check_warnings ((".*contents.*" , DeprecationWarning )):
185+ with warnings_helper . check_warnings ((".*contents.*" , DeprecationWarning )):
174186 c = resources .contents (self .anchor01 , 'subdirectory' )
175187 self .assertGreaterEqual (
176188 set (c ),
177189 {'binary.file' },
178190 )
179191
180- @ignore_warnings (category = DeprecationWarning )
192+ @warnings_helper . ignore_warnings (category = DeprecationWarning )
181193 def test_common_errors (self ):
182194 for func in (
183195 resources .read_text ,
@@ -208,18 +220,24 @@ def test_text_errors(self):
208220 # Multiple path arguments need explicit encoding argument.
209221 with self .assertRaises (TypeError ):
210222 func (
211- self .anchor02 , 'subdirectory' ,
212- 'subsubdir' , 'resource.txt' ,
223+ self .anchor02 ,
224+ 'subdirectory' ,
225+ 'subsubdir' ,
226+ 'resource.txt' ,
213227 )
214228
215229
216230class FunctionalAPITest_StringAnchor (
217- unittest .TestCase , FunctionalAPIBase , StringAnchorMixin ,
231+ unittest .TestCase ,
232+ FunctionalAPIBase ,
233+ StringAnchorMixin ,
218234):
219235 pass
220236
221237
222238class FunctionalAPITest_ModuleAnchor (
223- unittest .TestCase , FunctionalAPIBase , ModuleAnchorMixin ,
239+ unittest .TestCase ,
240+ FunctionalAPIBase ,
241+ ModuleAnchorMixin ,
224242):
225243 pass
0 commit comments