Skip to content

Commit a1c3c9e

Browse files
authored
Fix EncodingWarning in test_tools. (GH-28846)
1 parent 3d1ca86 commit a1c3c9e

File tree

7 files changed

+29
-25
lines changed

7 files changed

+29
-25
lines changed

Lib/test/test_tools/test_fixcid.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ def test_directory(self):
6161
os.mkdir(os_helper.TESTFN)
6262
self.addCleanup(os_helper.rmtree, os_helper.TESTFN)
6363
c_filename = os.path.join(os_helper.TESTFN, "file.c")
64-
with open(c_filename, "w") as file:
64+
with open(c_filename, "w", encoding="utf-8") as file:
6565
file.write("int xx;\n")
66-
with open(os.path.join(os_helper.TESTFN, "file.py"), "w") as file:
66+
with open(os.path.join(os_helper.TESTFN, "file.py"), "w",
67+
encoding="utf-8") as file:
6768
file.write("xx = 'unaltered'\n")
6869
script = os.path.join(scriptsdir, "fixcid.py")
6970
output = self.run_script(args=(os_helper.TESTFN,))
@@ -76,7 +77,7 @@ def test_directory(self):
7677

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

Lib/test/test_tools/test_gprof2html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_gprof(self):
2525
with mock.patch.object(self.gprof, 'webbrowser') as wmock, \
2626
tempfile.TemporaryDirectory() as tmpdir:
2727
fn = os.path.join(tmpdir, 'abc')
28-
open(fn, 'w').close()
28+
open(fn, 'wb').close()
2929
sys.argv = ['gprof2html', fn]
3030
self.gprof.main()
3131
self.assertTrue(wmock.open.called)

Lib/test/test_tools/test_i18n.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ def extract_docstrings_from_str(self, module_content):
5757
""" utility: return all msgids extracted from module_content """
5858
filename = 'test_docstrings.py'
5959
with temp_cwd(None) as cwd:
60-
with open(filename, 'w') as fp:
60+
with open(filename, 'w', encoding='utf-8') as fp:
6161
fp.write(module_content)
6262
assert_python_ok(self.script, '-D', filename)
63-
with open('messages.pot') as fp:
63+
with open('messages.pot', encoding='utf-8') as fp:
6464
data = fp.read()
6565
return self.get_msgids(data)
6666

@@ -70,7 +70,7 @@ def test_header(self):
7070
"""
7171
with temp_cwd(None) as cwd:
7272
assert_python_ok(self.script)
73-
with open('messages.pot') as fp:
73+
with open('messages.pot', encoding='utf-8') as fp:
7474
data = fp.read()
7575
header = self.get_header(data)
7676

@@ -97,7 +97,7 @@ def test_POT_Creation_Date(self):
9797
from datetime import datetime
9898
with temp_cwd(None) as cwd:
9999
assert_python_ok(self.script)
100-
with open('messages.pot') as fp:
100+
with open('messages.pot', encoding='utf-8') as fp:
101101
data = fp.read()
102102
header = self.get_header(data)
103103
creationDate = header['POT-Creation-Date']
@@ -299,16 +299,19 @@ def test_files_list(self):
299299
text3 = 'Text to ignore'
300300
with temp_cwd(None), temp_dir(None) as sdir:
301301
os.mkdir(os.path.join(sdir, 'pypkg'))
302-
with open(os.path.join(sdir, 'pypkg', 'pymod.py'), 'w') as sfile:
302+
with open(os.path.join(sdir, 'pypkg', 'pymod.py'), 'w',
303+
encoding='utf-8') as sfile:
303304
sfile.write(f'_({text1!r})')
304305
os.mkdir(os.path.join(sdir, 'pkg.py'))
305-
with open(os.path.join(sdir, 'pkg.py', 'pymod2.py'), 'w') as sfile:
306+
with open(os.path.join(sdir, 'pkg.py', 'pymod2.py'), 'w',
307+
encoding='utf-8') as sfile:
306308
sfile.write(f'_({text2!r})')
307309
os.mkdir(os.path.join(sdir, 'CVS'))
308-
with open(os.path.join(sdir, 'CVS', 'pymod3.py'), 'w') as sfile:
310+
with open(os.path.join(sdir, 'CVS', 'pymod3.py'), 'w',
311+
encoding='utf-8') as sfile:
309312
sfile.write(f'_({text3!r})')
310313
assert_python_ok(self.script, sdir)
311-
with open('messages.pot') as fp:
314+
with open('messages.pot', encoding='utf-8') as fp:
312315
data = fp.read()
313316
self.assertIn(f'msgid "{text1}"', data)
314317
self.assertIn(f'msgid "{text2}"', data)

Lib/test/test_tools/test_lll.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_lll_multiple_dirs(self):
2222
fn1 = os.path.join(dir1, 'foo1')
2323
fn2 = os.path.join(dir2, 'foo2')
2424
for fn, dir in (fn1, dir1), (fn2, dir2):
25-
open(fn, 'w').close()
25+
open(fn, 'wb').close()
2626
os.symlink(fn, os.path.join(dir, 'symlink'))
2727

2828
with support.captured_stdout() as output:

Lib/test/test_tools/test_pdeps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_process_errors(self):
1919
# Issue #14492: m_import.match(line) can be None.
2020
with tempfile.TemporaryDirectory() as tmpdir:
2121
fn = os.path.join(tmpdir, 'foo')
22-
with open(fn, 'w') as stream:
22+
with open(fn, 'w', encoding='utf-8') as stream:
2323
stream.write("#!/this/will/fail")
2424
self.pdeps.process(fn, {})
2525

Lib/test/test_tools/test_pindent.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ def test_selftest(self):
3737
self.maxDiff = None
3838
with os_helper.temp_dir() as directory:
3939
data_path = os.path.join(directory, '_test.py')
40-
with open(self.script) as f:
40+
with open(self.script, encoding='utf-8') as f:
4141
closed = f.read()
42-
with open(data_path, 'w') as f:
42+
with open(data_path, 'w', encoding='utf-8') as f:
4343
f.write(closed)
4444

4545
rc, out, err = assert_python_ok(self.script, '-d', data_path)
4646
self.assertEqual(out, b'')
4747
self.assertEqual(err, b'')
4848
backup = data_path + '~'
4949
self.assertTrue(os.path.exists(backup))
50-
with open(backup) as f:
50+
with open(backup, encoding='utf-8') as f:
5151
self.assertEqual(f.read(), closed)
52-
with open(data_path) as f:
52+
with open(data_path, encoding='utf-8') as f:
5353
clean = f.read()
5454
compile(clean, '_test.py', 'exec')
5555
self.assertEqual(self.pindent(clean, '-c'), closed)
@@ -58,20 +58,20 @@ def test_selftest(self):
5858
rc, out, err = assert_python_ok(self.script, '-c', data_path)
5959
self.assertEqual(out, b'')
6060
self.assertEqual(err, b'')
61-
with open(backup) as f:
61+
with open(backup, encoding='utf-8') as f:
6262
self.assertEqual(f.read(), clean)
63-
with open(data_path) as f:
63+
with open(data_path, encoding='utf-8') as f:
6464
self.assertEqual(f.read(), closed)
6565

6666
broken = self.lstriplines(closed)
67-
with open(data_path, 'w') as f:
67+
with open(data_path, 'w', encoding='utf-8') as f:
6868
f.write(broken)
6969
rc, out, err = assert_python_ok(self.script, '-r', data_path)
7070
self.assertEqual(out, b'')
7171
self.assertEqual(err, b'')
72-
with open(backup) as f:
72+
with open(backup, encoding='utf-8') as f:
7373
self.assertEqual(f.read(), broken)
74-
with open(data_path) as f:
74+
with open(data_path, encoding='utf-8') as f:
7575
indented = f.read()
7676
compile(indented, '_test.py', 'exec')
7777
self.assertEqual(self.pindent(broken, '-r'), indented)

Tools/scripts/gprof2html.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"""
2525

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

@@ -79,7 +79,7 @@ def main():
7979
filename = sys.argv[1]
8080
outputfilename = filename + ".html"
8181
input = add_escapes(filename)
82-
with open(outputfilename, "w") as output:
82+
with open(outputfilename, "w", encoding="utf-8") as output:
8383
gprof2html(input, output, filename)
8484
webbrowser.open("file:" + os.path.abspath(outputfilename))
8585

0 commit comments

Comments
 (0)