Skip to content

Fix EncodingWarning in test_tools. #28846

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 1 commit into from
Oct 10, 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
7 changes: 4 additions & 3 deletions Lib/test/test_tools/test_fixcid.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ def test_directory(self):
os.mkdir(os_helper.TESTFN)
self.addCleanup(os_helper.rmtree, os_helper.TESTFN)
c_filename = os.path.join(os_helper.TESTFN, "file.c")
with open(c_filename, "w") as file:
with open(c_filename, "w", encoding="utf-8") as file:
file.write("int xx;\n")
with open(os.path.join(os_helper.TESTFN, "file.py"), "w") as file:
with open(os.path.join(os_helper.TESTFN, "file.py"), "w",
encoding="utf-8") as file:
file.write("xx = 'unaltered'\n")
script = os.path.join(scriptsdir, "fixcid.py")
output = self.run_script(args=(os_helper.TESTFN,))
Expand All @@ -76,7 +77,7 @@ def test_directory(self):

def run_script(self, input="", *, args=("-",), substfile="xx yy\n"):
substfilename = os_helper.TESTFN + ".subst"
with open(substfilename, "w") as file:
with open(substfilename, "w", encoding="utf-8") as file:
file.write(substfile)
self.addCleanup(os_helper.unlink, substfilename)

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_tools/test_gprof2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_gprof(self):
with mock.patch.object(self.gprof, 'webbrowser') as wmock, \
tempfile.TemporaryDirectory() as tmpdir:
fn = os.path.join(tmpdir, 'abc')
open(fn, 'w').close()
open(fn, 'wb').close()
sys.argv = ['gprof2html', fn]
self.gprof.main()
self.assertTrue(wmock.open.called)
Expand Down
19 changes: 11 additions & 8 deletions Lib/test/test_tools/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ def extract_docstrings_from_str(self, module_content):
""" utility: return all msgids extracted from module_content """
filename = 'test_docstrings.py'
with temp_cwd(None) as cwd:
with open(filename, 'w') as fp:
with open(filename, 'w', encoding='utf-8') as fp:
fp.write(module_content)
assert_python_ok(self.script, '-D', filename)
with open('messages.pot') as fp:
with open('messages.pot', encoding='utf-8') as fp:
data = fp.read()
return self.get_msgids(data)

Expand All @@ -70,7 +70,7 @@ def test_header(self):
"""
with temp_cwd(None) as cwd:
assert_python_ok(self.script)
with open('messages.pot') as fp:
with open('messages.pot', encoding='utf-8') as fp:
data = fp.read()
header = self.get_header(data)

Expand All @@ -97,7 +97,7 @@ def test_POT_Creation_Date(self):
from datetime import datetime
with temp_cwd(None) as cwd:
assert_python_ok(self.script)
with open('messages.pot') as fp:
with open('messages.pot', encoding='utf-8') as fp:
data = fp.read()
header = self.get_header(data)
creationDate = header['POT-Creation-Date']
Expand Down Expand Up @@ -299,16 +299,19 @@ def test_files_list(self):
text3 = 'Text to ignore'
with temp_cwd(None), temp_dir(None) as sdir:
os.mkdir(os.path.join(sdir, 'pypkg'))
with open(os.path.join(sdir, 'pypkg', 'pymod.py'), 'w') as sfile:
with open(os.path.join(sdir, 'pypkg', 'pymod.py'), 'w',
encoding='utf-8') as sfile:
sfile.write(f'_({text1!r})')
os.mkdir(os.path.join(sdir, 'pkg.py'))
with open(os.path.join(sdir, 'pkg.py', 'pymod2.py'), 'w') as sfile:
with open(os.path.join(sdir, 'pkg.py', 'pymod2.py'), 'w',
encoding='utf-8') as sfile:
sfile.write(f'_({text2!r})')
os.mkdir(os.path.join(sdir, 'CVS'))
with open(os.path.join(sdir, 'CVS', 'pymod3.py'), 'w') as sfile:
with open(os.path.join(sdir, 'CVS', 'pymod3.py'), 'w',
encoding='utf-8') as sfile:
sfile.write(f'_({text3!r})')
assert_python_ok(self.script, sdir)
with open('messages.pot') as fp:
with open('messages.pot', encoding='utf-8') as fp:
data = fp.read()
self.assertIn(f'msgid "{text1}"', data)
self.assertIn(f'msgid "{text2}"', data)
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_tools/test_lll.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_lll_multiple_dirs(self):
fn1 = os.path.join(dir1, 'foo1')
fn2 = os.path.join(dir2, 'foo2')
for fn, dir in (fn1, dir1), (fn2, dir2):
open(fn, 'w').close()
open(fn, 'wb').close()
os.symlink(fn, os.path.join(dir, 'symlink'))

with support.captured_stdout() as output:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_tools/test_pdeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_process_errors(self):
# Issue #14492: m_import.match(line) can be None.
with tempfile.TemporaryDirectory() as tmpdir:
fn = os.path.join(tmpdir, 'foo')
with open(fn, 'w') as stream:
with open(fn, 'w', encoding='utf-8') as stream:
stream.write("#!/this/will/fail")
self.pdeps.process(fn, {})

Expand Down
18 changes: 9 additions & 9 deletions Lib/test/test_tools/test_pindent.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ def test_selftest(self):
self.maxDiff = None
with os_helper.temp_dir() as directory:
data_path = os.path.join(directory, '_test.py')
with open(self.script) as f:
with open(self.script, encoding='utf-8') as f:
closed = f.read()
with open(data_path, 'w') as f:
with open(data_path, 'w', encoding='utf-8') as f:
f.write(closed)

rc, out, err = assert_python_ok(self.script, '-d', data_path)
self.assertEqual(out, b'')
self.assertEqual(err, b'')
backup = data_path + '~'
self.assertTrue(os.path.exists(backup))
with open(backup) as f:
with open(backup, encoding='utf-8') as f:
self.assertEqual(f.read(), closed)
with open(data_path) as f:
with open(data_path, encoding='utf-8') as f:
clean = f.read()
compile(clean, '_test.py', 'exec')
self.assertEqual(self.pindent(clean, '-c'), closed)
Expand All @@ -58,20 +58,20 @@ def test_selftest(self):
rc, out, err = assert_python_ok(self.script, '-c', data_path)
self.assertEqual(out, b'')
self.assertEqual(err, b'')
with open(backup) as f:
with open(backup, encoding='utf-8') as f:
self.assertEqual(f.read(), clean)
with open(data_path) as f:
with open(data_path, encoding='utf-8') as f:
self.assertEqual(f.read(), closed)

broken = self.lstriplines(closed)
with open(data_path, 'w') as f:
with open(data_path, 'w', encoding='utf-8') as f:
f.write(broken)
rc, out, err = assert_python_ok(self.script, '-r', data_path)
self.assertEqual(out, b'')
self.assertEqual(err, b'')
with open(backup) as f:
with open(backup, encoding='utf-8') as f:
self.assertEqual(f.read(), broken)
with open(data_path) as f:
with open(data_path, encoding='utf-8') as f:
indented = f.read()
compile(indented, '_test.py', 'exec')
self.assertEqual(self.pindent(broken, '-r'), indented)
Expand Down
4 changes: 2 additions & 2 deletions Tools/scripts/gprof2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"""

def add_escapes(filename):
with open(filename) as fp:
with open(filename, encoding="utf-8") as fp:
for line in fp:
yield html.escape(line)

Expand Down Expand Up @@ -79,7 +79,7 @@ def main():
filename = sys.argv[1]
outputfilename = filename + ".html"
input = add_escapes(filename)
with open(outputfilename, "w") as output:
with open(outputfilename, "w", encoding="utf-8") as output:
gprof2html(input, output, filename)
webbrowser.open("file:" + os.path.abspath(outputfilename))

Expand Down