bpo-43651: PEP 597: Fix EncodingWarning in some tests (GH-25181)

* Fix test_shutil
* Fix test_imp
* Fix test_import
* Fix test_importlib
This commit is contained in:
Inada Naoki 2021-04-05 13:11:23 +09:00 committed by GitHub
parent ee952b5c73
commit c8e5eb904e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 55 additions and 50 deletions

View file

@ -124,7 +124,7 @@ class SimpleTest(abc.LoaderTests):
module = loader.load_module('_temp')
module_id = id(module)
module_dict_id = id(module.__dict__)
with open(mapping['_temp'], 'w') as file:
with open(mapping['_temp'], 'w', encoding='utf-8') as file:
file.write("testing_var = 42\n")
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
@ -145,7 +145,7 @@ class SimpleTest(abc.LoaderTests):
orig_module = types.ModuleType(name)
for attr in attributes:
setattr(orig_module, attr, value)
with open(mapping[name], 'w') as file:
with open(mapping[name], 'w', encoding='utf-8') as file:
file.write('+++ bad syntax +++')
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
with self.assertRaises(SyntaxError):
@ -162,7 +162,7 @@ class SimpleTest(abc.LoaderTests):
# [syntax error]
def test_bad_syntax(self):
with util.create_modules('_temp') as mapping:
with open(mapping['_temp'], 'w') as file:
with open(mapping['_temp'], 'w', encoding='utf-8') as file:
file.write('=')
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
with self.assertRaises(SyntaxError):
@ -175,7 +175,7 @@ class SimpleTest(abc.LoaderTests):
# Loading a module found from an empty string entry on sys.path should
# not only work, but keep all attributes relative.
file_path = '_temp.py'
with open(file_path, 'w') as file:
with open(file_path, 'w', encoding='utf-8') as file:
file.write("# test file for importlib")
try:
with util.uncache('_temp'):
@ -199,7 +199,7 @@ class SimpleTest(abc.LoaderTests):
with util.create_modules('_temp') as mapping:
source = mapping['_temp']
compiled = self.util.cache_from_source(source)
with open(source, 'w') as f:
with open(source, 'w', encoding='utf-8') as f:
f.write("x = 5")
try:
os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))

View file

@ -127,7 +127,7 @@ class FinderTests(abc.FinderTests):
# The empty string from sys.path means to search in the cwd.
finder = self.machinery.FileFinder('', (self.machinery.SourceFileLoader,
self.machinery.SOURCE_SUFFIXES))
with open('mod.py', 'w') as file:
with open('mod.py', 'w', encoding='utf-8') as file:
file.write("# test file for importlib")
try:
loader = self._find(finder, 'mod', loader_only=True)