Skip to content

bpo-43651: PEP 597: Fix EncodingWarning in some tests #25181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Lib/test/test_imp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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

Expand Down
45 changes: 23 additions & 22 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_from_import_missing_attr_path_is_canonical(self):
def test_from_import_star_invalid_type(self):
import re
with _ready_to_import() as (name, path):
with open(path, 'w') as f:
with open(path, 'w', encoding='utf-8') as f:
f.write("__all__ = [b'invalid_type']")
globals = {}
with self.assertRaisesRegex(
Expand All @@ -126,7 +126,7 @@ def test_from_import_star_invalid_type(self):
exec(f"from {name} import *", globals)
self.assertNotIn(b"invalid_type", globals)
with _ready_to_import() as (name, path):
with open(path, 'w') as f:
with open(path, 'w', encoding='utf-8') as f:
f.write("globals()[b'invalid_type'] = object()")
globals = {}
with self.assertRaisesRegex(
Expand Down Expand Up @@ -155,7 +155,7 @@ def test_with_extension(ext):
else:
pyc = TESTFN + ".pyc"

with open(source, "w") as f:
with open(source, "w", encoding='utf-8') as f:
print("# This tests Python's ability to import a",
ext, "file.", file=f)
a = random.randrange(1000)
Expand Down Expand Up @@ -195,7 +195,7 @@ def test_module_with_large_stack(self, module='longlist'):
filename = module + '.py'

# Create a file with a list of 65000 elements.
with open(filename, 'w') as f:
with open(filename, 'w', encoding='utf-8') as f:
f.write('d = [\n')
for i in range(65000):
f.write('"",\n')
Expand Down Expand Up @@ -232,7 +232,7 @@ def test_module_with_large_stack(self, module='longlist'):

def test_failing_import_sticks(self):
source = TESTFN + ".py"
with open(source, "w") as f:
with open(source, "w", encoding='utf-8') as f:
print("a = 1/0", file=f)

# New in 2.4, we shouldn't be able to import that no matter how often
Expand Down Expand Up @@ -281,7 +281,7 @@ def test_issue31286(self):
def test_failing_reload(self):
# A failing reload should leave the module object in sys.modules.
source = TESTFN + os.extsep + "py"
with open(source, "w") as f:
with open(source, "w", encoding='utf-8') as f:
f.write("a = 1\nb=2\n")

sys.path.insert(0, os.curdir)
Expand All @@ -298,7 +298,7 @@ def test_failing_reload(self):
remove_files(TESTFN)

# Now damage the module.
with open(source, "w") as f:
with open(source, "w", encoding='utf-8') as f:
f.write("a = 10\nb=20//0\n")

self.assertRaises(ZeroDivisionError, importlib.reload, mod)
Expand All @@ -320,7 +320,7 @@ def test_failing_reload(self):
def test_file_to_source(self):
# check if __file__ points to the source file where available
source = TESTFN + ".py"
with open(source, "w") as f:
with open(source, "w", encoding='utf-8') as f:
f.write("test = None\n")

sys.path.insert(0, os.curdir)
Expand Down Expand Up @@ -369,7 +369,7 @@ def test_timestamp_overflow(self):
try:
source = TESTFN + ".py"
compiled = importlib.util.cache_from_source(source)
with open(source, 'w') as f:
with open(source, 'w', encoding='utf-8') as f:
pass
try:
os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
Expand Down Expand Up @@ -574,7 +574,7 @@ def test_pyc_always_writable(self):
# with later updates, see issue #6074 for details
with _ready_to_import() as (name, path):
# Write a Python file, make it read-only and import it
with open(path, 'w') as f:
with open(path, 'w', encoding='utf-8') as f:
f.write("x = 'original'\n")
# Tweak the mtime of the source to ensure pyc gets updated later
s = os.stat(path)
Expand All @@ -584,7 +584,7 @@ def test_pyc_always_writable(self):
self.assertEqual(m.x, 'original')
# Change the file and then reimport it
os.chmod(path, 0o600)
with open(path, 'w') as f:
with open(path, 'w', encoding='utf-8') as f:
f.write("x = 'rewritten'\n")
unload(name)
importlib.invalidate_caches()
Expand Down Expand Up @@ -623,7 +623,7 @@ def setUp(self):
self.sys_path = sys.path[:]
self.orig_module = sys.modules.pop(self.module_name, None)
os.mkdir(self.dir_name)
with open(self.file_name, "w") as f:
with open(self.file_name, "w", encoding='utf-8') as f:
f.write(self.module_source)
sys.path.insert(0, self.dir_name)
importlib.invalidate_caches()
Expand Down Expand Up @@ -704,7 +704,8 @@ def tearDown(self):

# Regression test for http://bugs.python.org/issue1293.
def test_trailing_slash(self):
with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f:
with open(os.path.join(self.path, 'test_trailing_slash.py'),
'w', encoding='utf-8') as f:
f.write("testdata = 'test_trailing_slash'")
sys.path.append(self.path+'/')
mod = __import__("test_trailing_slash")
Expand Down Expand Up @@ -842,7 +843,7 @@ def _clean(self):
def setUp(self):
self.source = TESTFN + '.py'
self._clean()
with open(self.source, 'w') as fp:
with open(self.source, 'w', encoding='utf-8') as fp:
print('# This is a test file written by test_import.py', file=fp)
sys.path.insert(0, os.curdir)
importlib.invalidate_caches()
Expand Down Expand Up @@ -941,9 +942,9 @@ def cleanup():
os.mkdir('pep3147')
self.addCleanup(cleanup)
# Touch the __init__.py
with open(os.path.join('pep3147', '__init__.py'), 'w'):
with open(os.path.join('pep3147', '__init__.py'), 'wb'):
pass
with open(os.path.join('pep3147', 'foo.py'), 'w'):
with open(os.path.join('pep3147', 'foo.py'), 'wb'):
pass
importlib.invalidate_caches()
m = __import__('pep3147.foo')
Expand All @@ -964,9 +965,9 @@ def cleanup():
os.mkdir('pep3147')
self.addCleanup(cleanup)
# Touch the __init__.py
with open(os.path.join('pep3147', '__init__.py'), 'w'):
with open(os.path.join('pep3147', '__init__.py'), 'wb'):
pass
with open(os.path.join('pep3147', 'foo.py'), 'w'):
with open(os.path.join('pep3147', 'foo.py'), 'wb'):
pass
importlib.invalidate_caches()
m = __import__('pep3147.foo')
Expand All @@ -986,7 +987,7 @@ def test_recompute_pyc_same_second(self):
# source size is enough to trigger recomputation of the pyc file.
__import__(TESTFN)
unload(TESTFN)
with open(self.source, 'a') as fp:
with open(self.source, 'a', encoding='utf-8') as fp:
print("x = 5", file=fp)
m = __import__(TESTFN)
self.assertEqual(m.x, 5)
Expand Down Expand Up @@ -1118,7 +1119,7 @@ def tearDown(self):

def create_module(self, mod, contents, ext=".py"):
fname = os.path.join(TESTFN, mod + ext)
with open(fname, "w") as f:
with open(fname, "w", encoding='utf-8') as f:
f.write(contents)
self.addCleanup(unload, mod)
importlib.invalidate_caches()
Expand Down Expand Up @@ -1195,10 +1196,10 @@ def _setup_broken_package(self, parent, child):
os.mkdir(pkg_path)
# Touch the __init__.py
init_path = os.path.join(pkg_path, '__init__.py')
with open(init_path, 'w') as f:
with open(init_path, 'w', encoding='utf-8') as f:
f.write(parent)
bar_path = os.path.join(pkg_path, 'bar.py')
with open(bar_path, 'w') as f:
with open(bar_path, 'w', encoding='utf-8') as f:
f.write(child)
importlib.invalidate_caches()
return init_path, bar_path
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def build_files(file_defs, prefix=pathlib.Path()):
with full_name.open('wb') as f:
f.write(contents)
else:
with full_name.open('w') as f:
with full_name.open('w', encoding='utf-8') as f:
f.write(DALS(contents))


Expand Down
10 changes: 5 additions & 5 deletions Lib/test/test_importlib/source/test_file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_module_reuse(self):
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)
Expand All @@ -145,7 +145,7 @@ def test_state_after_failure(self):
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):
Expand All @@ -162,7 +162,7 @@ def test_state_after_failure(self):
# [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):
Expand All @@ -175,7 +175,7 @@ def test_file_from_empty_string_dir(self):
# 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'):
Expand All @@ -199,7 +199,7 @@ def test_timestamp_overflow(self):
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))
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/source/test_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_empty_string_for_dir(self):
# 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)
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def test_reload_namespace_changed(self):
'__file__': None,
}
os.mkdir(name)
with open(bad_path, 'w') as init_file:
with open(bad_path, 'w', encoding='utf-8') as init_file:
init_file.write('eggs = None')
module = self.init.import_module(name)
ns = vars(module).copy()
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_read_bytes(self):

def test_read_text(self):
files = resources.files(self.data)
actual = files.joinpath('utf-8.file').read_text()
actual = files.joinpath('utf-8.file').read_text(encoding='utf-8')
assert actual == 'Hello, UTF-8 world!\n'

@unittest.skipUnless(
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_importlib/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def pkg_with_dashes(site_dir):
metadata_dir = site_dir / 'my_pkg.dist-info'
metadata_dir.mkdir()
metadata = metadata_dir / 'METADATA'
with metadata.open('w') as strm:
with metadata.open('w', encoding='utf-8') as strm:
strm.write('Version: 1.0\n')
return 'my-pkg'

Expand All @@ -102,7 +102,7 @@ def pkg_with_mixed_case(site_dir):
metadata_dir = site_dir / 'CherryPy.dist-info'
metadata_dir.mkdir()
metadata = metadata_dir / 'METADATA'
with metadata.open('w') as strm:
with metadata.open('w', encoding='utf-8') as strm:
strm.write('Version: 1.0\n')
return 'CherryPy'

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/test_pkg_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def rewrite_file(self, contents):
compiled_path = cache_from_source(self.module_path)
if os.path.exists(compiled_path):
os.remove(compiled_path)
with open(self.module_path, 'w') as f:
with open(self.module_path, 'w', encoding='utf-8') as f:
f.write(contents)

def test_package_import__semantics(self):
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_importlib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def case_insensitive_tests(test):

def submodule(parent, name, pkg_dir, content=''):
path = os.path.join(pkg_dir, name + '.py')
with open(path, 'w') as subfile:
with open(path, 'w', encoding='utf-8') as subfile:
subfile.write(content)
return '{}.{}'.format(parent, name), path

Expand Down Expand Up @@ -176,7 +176,7 @@ def temp_module(name, content='', *, pkg=False):
content = ''
if content is not None:
# not a namespace package
with open(modpath, 'w') as modfile:
with open(modpath, 'w', encoding='utf-8') as modfile:
modfile.write(content)
yield location

Expand Down Expand Up @@ -384,7 +384,7 @@ def create_modules(*names):
os.mkdir(file_path)
created_paths.append(file_path)
file_path = os.path.join(file_path, name_parts[-1] + '.py')
with open(file_path, 'w') as file:
with open(file_path, 'w', encoding='utf-8') as file:
file.write(source.format(name))
created_paths.append(file_path)
mapping[name] = file_path
Expand Down
Loading