11import unittest
22import os
33
4- from test .support import warnings_helper
4+ from test .support . warnings_helper import ignore_warnings , check_warnings
55
6- from importlib import 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.
@@ -22,7 +22,8 @@ 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"""
25+ """Yield various names of a text file in anchor02, each in a subTest
26+ """
2627 for path_parts in (
2728 ('subdirectory' , 'subsubdir' , 'resource.txt' ),
2829 ('subdirectory/subsubdir/resource.txt' ,),
@@ -35,7 +36,7 @@ def assertEndsWith(self, string, suffix):
3536 """Assert that `string` ends with `suffix`.
3637
3738 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 )
3940
4041 def test_read_text (self ):
4142 self .assertEqual (
@@ -44,20 +45,15 @@ def test_read_text(self):
4445 )
4546 self .assertEqual (
4647 resources .read_text (
47- self .anchor02 ,
48- 'subdirectory' ,
49- 'subsubdir' ,
50- 'resource.txt' ,
48+ self .anchor02 , 'subdirectory' , 'subsubdir' , 'resource.txt' ,
5149 encoding = 'utf-8' ,
5250 ),
5351 'a resource' ,
5452 )
5553 for path_parts in self ._gen_resourcetxt_path_parts ():
5654 self .assertEqual (
5755 resources .read_text (
58- self .anchor02 ,
59- * path_parts ,
60- encoding = 'utf-8' ,
56+ self .anchor02 , * path_parts , encoding = 'utf-8' ,
6157 ),
6258 'a resource' ,
6359 )
@@ -71,16 +67,13 @@ def test_read_text(self):
7167 resources .read_text (self .anchor01 , 'utf-16.file' )
7268 self .assertEqual (
7369 resources .read_text (
74- self .anchor01 ,
75- 'binary.file' ,
76- encoding = 'latin1' ,
70+ self .anchor01 , 'binary.file' , encoding = 'latin1' ,
7771 ),
7872 '\x00 \x01 \x02 \x03 ' ,
7973 )
8074 self .assertEndsWith ( # ignore the BOM
8175 resources .read_text (
82- self .anchor01 ,
83- 'utf-16.file' ,
76+ self .anchor01 , 'utf-16.file' ,
8477 errors = 'backslashreplace' ,
8578 ),
8679 'Hello, UTF-16 world!\n ' .encode ('utf-16-le' ).decode (
@@ -104,8 +97,7 @@ def test_open_text(self):
10497 self .assertEqual (f .read (), 'Hello, UTF-8 world!\n ' )
10598 for path_parts in self ._gen_resourcetxt_path_parts ():
10699 with resources .open_text (
107- self .anchor02 ,
108- * path_parts ,
100+ self .anchor02 , * path_parts ,
109101 encoding = 'utf-8' ,
110102 ) as f :
111103 self .assertEqual (f .read (), 'a resource' )
@@ -119,14 +111,11 @@ def test_open_text(self):
119111 with self .assertRaises (UnicodeDecodeError ):
120112 f .read ()
121113 with resources .open_text (
122- self .anchor01 ,
123- 'binary.file' ,
124- encoding = 'latin1' ,
114+ self .anchor01 , 'binary.file' , encoding = 'latin1' ,
125115 ) as f :
126116 self .assertEqual (f .read (), '\x00 \x01 \x02 \x03 ' )
127117 with resources .open_text (
128- self .anchor01 ,
129- 'utf-16.file' ,
118+ self .anchor01 , 'utf-16.file' ,
130119 errors = 'backslashreplace' ,
131120 ) as f :
132121 self .assertEndsWith ( # ignore the BOM
@@ -141,17 +130,16 @@ def test_open_binary(self):
141130 self .assertEqual (f .read (), b'Hello, UTF-8 world!\n ' )
142131 for path_parts in self ._gen_resourcetxt_path_parts ():
143132 with resources .open_binary (
144- self .anchor02 ,
145- * path_parts ,
133+ self .anchor02 , * path_parts ,
146134 ) as f :
147135 self .assertEqual (f .read (), b'a resource' )
148136
149137 def test_path (self ):
150138 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 :
152140 self .assertEqual (f .read (), 'Hello, UTF-8 world!\n ' )
153141 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 :
155143 self .assertEqual (f .read (), 'Hello, UTF-8 world!\n ' )
156144
157145 def test_is_resource (self ):
@@ -164,32 +152,32 @@ def test_is_resource(self):
164152 self .assertTrue (is_resource (self .anchor02 , * path_parts ))
165153
166154 def test_contents (self ):
167- with warnings_helper .check_warnings ((".*contents.*" , DeprecationWarning )):
155+ is_resource = resources .is_resource
156+ with check_warnings ((".*contents.*" , DeprecationWarning )):
168157 c = resources .contents (self .anchor01 )
169158 self .assertGreaterEqual (
170159 set (c ),
171160 {'utf-8.file' , 'utf-16.file' , 'binary.file' , 'subdirectory' },
172161 )
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+ ):
177166 list (resources .contents (self .anchor01 , 'utf-8.file' ))
178-
179167 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+ ):
184172 list (resources .contents (self .anchor01 , * path_parts ))
185- with warnings_helper . check_warnings ((".*contents.*" , DeprecationWarning )):
173+ with check_warnings ((".*contents.*" , DeprecationWarning )):
186174 c = resources .contents (self .anchor01 , 'subdirectory' )
187175 self .assertGreaterEqual (
188176 set (c ),
189177 {'binary.file' },
190178 )
191179
192- @warnings_helper . ignore_warnings (category = DeprecationWarning )
180+ @ignore_warnings (category = DeprecationWarning )
193181 def test_common_errors (self ):
194182 for func in (
195183 resources .read_text ,
@@ -220,24 +208,18 @@ def test_text_errors(self):
220208 # Multiple path arguments need explicit encoding argument.
221209 with self .assertRaises (TypeError ):
222210 func (
223- self .anchor02 ,
224- 'subdirectory' ,
225- 'subsubdir' ,
226- 'resource.txt' ,
211+ self .anchor02 , 'subdirectory' ,
212+ 'subsubdir' , 'resource.txt' ,
227213 )
228214
229215
230216class FunctionalAPITest_StringAnchor (
231- unittest .TestCase ,
232- FunctionalAPIBase ,
233- StringAnchorMixin ,
217+ unittest .TestCase , FunctionalAPIBase , StringAnchorMixin ,
234218):
235219 pass
236220
237221
238222class FunctionalAPITest_ModuleAnchor (
239- unittest .TestCase ,
240- FunctionalAPIBase ,
241- ModuleAnchorMixin ,
223+ unittest .TestCase , FunctionalAPIBase , ModuleAnchorMixin ,
242224):
243225 pass
0 commit comments