bpo-43651: PEP 597: Fix EncodingWarning in some tests (GH-25181)
* Fix test_shutil
* Fix test_imp
* Fix test_import
* Fix test_importlib
diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py
index 4781d89..5abe28e 100644
--- a/Lib/test/test_imp.py
+++ b/Lib/test/test_imp.py
@@ -102,7 +102,7 @@ def test_issue3594(self):
temp_mod_name = 'test_imp_helper'
sys.path.insert(0, '.')
try:
- with open(temp_mod_name + '.py', 'w') as file:
+ with open(temp_mod_name + '.py', 'w', encoding="latin-1") as file:
file.write("# coding: cp1252\nu = 'test.test_imp'\n")
file, filename, info = imp.find_module(temp_mod_name)
file.close()
@@ -157,7 +157,7 @@ def test_issue5604(self):
# if the curdir is not in sys.path the test fails when run with
# ./python ./Lib/test/regrtest.py test_imp
sys.path.insert(0, os.curdir)
- with open(temp_mod_name + '.py', 'w') as file:
+ with open(temp_mod_name + '.py', 'w', encoding="utf-8") as file:
file.write('a = 1\n')
file, filename, info = imp.find_module(temp_mod_name)
with file:
@@ -185,7 +185,7 @@ def test_issue5604(self):
if not os.path.exists(test_package_name):
os.mkdir(test_package_name)
- with open(init_file_name, 'w') as file:
+ with open(init_file_name, 'w', encoding="utf-8") as file:
file.write('b = 2\n')
with warnings.catch_warnings():
warnings.simplefilter('ignore')
@@ -310,7 +310,7 @@ def test_bug7732(self):
def test_multiple_calls_to_get_data(self):
# Issue #18755: make sure multiple calls to get_data() can succeed.
loader = imp._LoadSourceCompatibility('imp', imp.__file__,
- open(imp.__file__))
+ open(imp.__file__, encoding="utf-8"))
loader.get_data(imp.__file__) # File should be closed
loader.get_data(imp.__file__) # Will need to create a newly opened file