@@ -117,7 +117,7 @@ def test_from_import_missing_attr_path_is_canonical(self):
117117 def test_from_import_star_invalid_type (self ):
118118 import re
119119 with _ready_to_import () as (name , path ):
120- with open (path , 'w' ) as f :
120+ with open (path , 'w' , encoding = 'utf-8' ) as f :
121121 f .write ("__all__ = [b'invalid_type']" )
122122 globals = {}
123123 with self .assertRaisesRegex (
@@ -126,7 +126,7 @@ def test_from_import_star_invalid_type(self):
126126 exec (f"from { name } import *" , globals )
127127 self .assertNotIn (b"invalid_type" , globals )
128128 with _ready_to_import () as (name , path ):
129- with open (path , 'w' ) as f :
129+ with open (path , 'w' , encoding = 'utf-8' ) as f :
130130 f .write ("globals()[b'invalid_type'] = object()" )
131131 globals = {}
132132 with self .assertRaisesRegex (
@@ -155,7 +155,7 @@ def test_with_extension(ext):
155155 else :
156156 pyc = TESTFN + ".pyc"
157157
158- with open (source , "w" ) as f :
158+ with open (source , "w" , encoding = 'utf-8' ) as f :
159159 print ("# This tests Python's ability to import a" ,
160160 ext , "file." , file = f )
161161 a = random .randrange (1000 )
@@ -195,7 +195,7 @@ def test_module_with_large_stack(self, module='longlist'):
195195 filename = module + '.py'
196196
197197 # Create a file with a list of 65000 elements.
198- with open (filename , 'w' ) as f :
198+ with open (filename , 'w' , encoding = 'utf-8' ) as f :
199199 f .write ('d = [\n ' )
200200 for i in range (65000 ):
201201 f .write ('"",\n ' )
@@ -232,7 +232,7 @@ def test_module_with_large_stack(self, module='longlist'):
232232
233233 def test_failing_import_sticks (self ):
234234 source = TESTFN + ".py"
235- with open (source , "w" ) as f :
235+ with open (source , "w" , encoding = 'utf-8' ) as f :
236236 print ("a = 1/0" , file = f )
237237
238238 # New in 2.4, we shouldn't be able to import that no matter how often
@@ -281,7 +281,7 @@ def test_issue31286(self):
281281 def test_failing_reload (self ):
282282 # A failing reload should leave the module object in sys.modules.
283283 source = TESTFN + os .extsep + "py"
284- with open (source , "w" ) as f :
284+ with open (source , "w" , encoding = 'utf-8' ) as f :
285285 f .write ("a = 1\n b=2\n " )
286286
287287 sys .path .insert (0 , os .curdir )
@@ -298,7 +298,7 @@ def test_failing_reload(self):
298298 remove_files (TESTFN )
299299
300300 # Now damage the module.
301- with open (source , "w" ) as f :
301+ with open (source , "w" , encoding = 'utf-8' ) as f :
302302 f .write ("a = 10\n b=20//0\n " )
303303
304304 self .assertRaises (ZeroDivisionError , importlib .reload , mod )
@@ -320,7 +320,7 @@ def test_failing_reload(self):
320320 def test_file_to_source (self ):
321321 # check if __file__ points to the source file where available
322322 source = TESTFN + ".py"
323- with open (source , "w" ) as f :
323+ with open (source , "w" , encoding = 'utf-8' ) as f :
324324 f .write ("test = None\n " )
325325
326326 sys .path .insert (0 , os .curdir )
@@ -369,7 +369,7 @@ def test_timestamp_overflow(self):
369369 try :
370370 source = TESTFN + ".py"
371371 compiled = importlib .util .cache_from_source (source )
372- with open (source , 'w' ) as f :
372+ with open (source , 'w' , encoding = 'utf-8' ) as f :
373373 pass
374374 try :
375375 os .utime (source , (2 ** 33 - 5 , 2 ** 33 - 5 ))
@@ -574,7 +574,7 @@ def test_pyc_always_writable(self):
574574 # with later updates, see issue #6074 for details
575575 with _ready_to_import () as (name , path ):
576576 # Write a Python file, make it read-only and import it
577- with open (path , 'w' ) as f :
577+ with open (path , 'w' , encoding = 'utf-8' ) as f :
578578 f .write ("x = 'original'\n " )
579579 # Tweak the mtime of the source to ensure pyc gets updated later
580580 s = os .stat (path )
@@ -584,7 +584,7 @@ def test_pyc_always_writable(self):
584584 self .assertEqual (m .x , 'original' )
585585 # Change the file and then reimport it
586586 os .chmod (path , 0o600 )
587- with open (path , 'w' ) as f :
587+ with open (path , 'w' , encoding = 'utf-8' ) as f :
588588 f .write ("x = 'rewritten'\n " )
589589 unload (name )
590590 importlib .invalidate_caches ()
@@ -623,7 +623,7 @@ def setUp(self):
623623 self .sys_path = sys .path [:]
624624 self .orig_module = sys .modules .pop (self .module_name , None )
625625 os .mkdir (self .dir_name )
626- with open (self .file_name , "w" ) as f :
626+ with open (self .file_name , "w" , encoding = 'utf-8' ) as f :
627627 f .write (self .module_source )
628628 sys .path .insert (0 , self .dir_name )
629629 importlib .invalidate_caches ()
@@ -704,7 +704,8 @@ def tearDown(self):
704704
705705 # Regression test for http://bugs.python.org/issue1293.
706706 def test_trailing_slash (self ):
707- with open (os .path .join (self .path , 'test_trailing_slash.py' ), 'w' ) as f :
707+ with open (os .path .join (self .path , 'test_trailing_slash.py' ),
708+ 'w' , encoding = 'utf-8' ) as f :
708709 f .write ("testdata = 'test_trailing_slash'" )
709710 sys .path .append (self .path + '/' )
710711 mod = __import__ ("test_trailing_slash" )
@@ -842,7 +843,7 @@ def _clean(self):
842843 def setUp (self ):
843844 self .source = TESTFN + '.py'
844845 self ._clean ()
845- with open (self .source , 'w' ) as fp :
846+ with open (self .source , 'w' , encoding = 'utf-8' ) as fp :
846847 print ('# This is a test file written by test_import.py' , file = fp )
847848 sys .path .insert (0 , os .curdir )
848849 importlib .invalidate_caches ()
@@ -941,9 +942,9 @@ def cleanup():
941942 os .mkdir ('pep3147' )
942943 self .addCleanup (cleanup )
943944 # Touch the __init__.py
944- with open (os .path .join ('pep3147' , '__init__.py' ), 'w ' ):
945+ with open (os .path .join ('pep3147' , '__init__.py' ), 'wb ' ):
945946 pass
946- with open (os .path .join ('pep3147' , 'foo.py' ), 'w ' ):
947+ with open (os .path .join ('pep3147' , 'foo.py' ), 'wb ' ):
947948 pass
948949 importlib .invalidate_caches ()
949950 m = __import__ ('pep3147.foo' )
@@ -964,9 +965,9 @@ def cleanup():
964965 os .mkdir ('pep3147' )
965966 self .addCleanup (cleanup )
966967 # Touch the __init__.py
967- with open (os .path .join ('pep3147' , '__init__.py' ), 'w ' ):
968+ with open (os .path .join ('pep3147' , '__init__.py' ), 'wb ' ):
968969 pass
969- with open (os .path .join ('pep3147' , 'foo.py' ), 'w ' ):
970+ with open (os .path .join ('pep3147' , 'foo.py' ), 'wb ' ):
970971 pass
971972 importlib .invalidate_caches ()
972973 m = __import__ ('pep3147.foo' )
@@ -986,7 +987,7 @@ def test_recompute_pyc_same_second(self):
986987 # source size is enough to trigger recomputation of the pyc file.
987988 __import__ (TESTFN )
988989 unload (TESTFN )
989- with open (self .source , 'a' ) as fp :
990+ with open (self .source , 'a' , encoding = 'utf-8' ) as fp :
990991 print ("x = 5" , file = fp )
991992 m = __import__ (TESTFN )
992993 self .assertEqual (m .x , 5 )
@@ -1118,7 +1119,7 @@ def tearDown(self):
11181119
11191120 def create_module (self , mod , contents , ext = ".py" ):
11201121 fname = os .path .join (TESTFN , mod + ext )
1121- with open (fname , "w" ) as f :
1122+ with open (fname , "w" , encoding = 'utf-8' ) as f :
11221123 f .write (contents )
11231124 self .addCleanup (unload , mod )
11241125 importlib .invalidate_caches ()
@@ -1195,10 +1196,10 @@ def _setup_broken_package(self, parent, child):
11951196 os .mkdir (pkg_path )
11961197 # Touch the __init__.py
11971198 init_path = os .path .join (pkg_path , '__init__.py' )
1198- with open (init_path , 'w' ) as f :
1199+ with open (init_path , 'w' , encoding = 'utf-8' ) as f :
11991200 f .write (parent )
12001201 bar_path = os .path .join (pkg_path , 'bar.py' )
1201- with open (bar_path , 'w' ) as f :
1202+ with open (bar_path , 'w' , encoding = 'utf-8' ) as f :
12021203 f .write (child )
12031204 importlib .invalidate_caches ()
12041205 return init_path , bar_path
0 commit comments