Fix EncodingWarning in test_tools. (GH-28846)

This commit is contained in:
Inada Naoki 2021-10-10 16:14:40 +09:00 committed by GitHub
parent 3d1ca867ed
commit a1c3c9e824
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 25 deletions

View File

@ -61,9 +61,10 @@ class Test(unittest.TestCase):
os.mkdir(os_helper.TESTFN) os.mkdir(os_helper.TESTFN)
self.addCleanup(os_helper.rmtree, os_helper.TESTFN) self.addCleanup(os_helper.rmtree, os_helper.TESTFN)
c_filename = os.path.join(os_helper.TESTFN, "file.c") 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") 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") file.write("xx = 'unaltered'\n")
script = os.path.join(scriptsdir, "fixcid.py") script = os.path.join(scriptsdir, "fixcid.py")
output = self.run_script(args=(os_helper.TESTFN,)) output = self.run_script(args=(os_helper.TESTFN,))
@ -76,7 +77,7 @@ class Test(unittest.TestCase):
def run_script(self, input="", *, args=("-",), substfile="xx yy\n"): def run_script(self, input="", *, args=("-",), substfile="xx yy\n"):
substfilename = os_helper.TESTFN + ".subst" substfilename = os_helper.TESTFN + ".subst"
with open(substfilename, "w") as file: with open(substfilename, "w", encoding="utf-8") as file:
file.write(substfile) file.write(substfile)
self.addCleanup(os_helper.unlink, substfilename) self.addCleanup(os_helper.unlink, substfilename)

View File

@ -25,7 +25,7 @@ class Gprof2htmlTests(unittest.TestCase):
with mock.patch.object(self.gprof, 'webbrowser') as wmock, \ with mock.patch.object(self.gprof, 'webbrowser') as wmock, \
tempfile.TemporaryDirectory() as tmpdir: tempfile.TemporaryDirectory() as tmpdir:
fn = os.path.join(tmpdir, 'abc') fn = os.path.join(tmpdir, 'abc')
open(fn, 'w').close() open(fn, 'wb').close()
sys.argv = ['gprof2html', fn] sys.argv = ['gprof2html', fn]
self.gprof.main() self.gprof.main()
self.assertTrue(wmock.open.called) self.assertTrue(wmock.open.called)

View File

@ -57,10 +57,10 @@ class Test_pygettext(unittest.TestCase):
""" utility: return all msgids extracted from module_content """ """ utility: return all msgids extracted from module_content """
filename = 'test_docstrings.py' filename = 'test_docstrings.py'
with temp_cwd(None) as cwd: 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) fp.write(module_content)
assert_python_ok(self.script, '-D', filename) 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() data = fp.read()
return self.get_msgids(data) return self.get_msgids(data)
@ -70,7 +70,7 @@ class Test_pygettext(unittest.TestCase):
""" """
with temp_cwd(None) as cwd: with temp_cwd(None) as cwd:
assert_python_ok(self.script) assert_python_ok(self.script)
with open('messages.pot') as fp: with open('messages.pot', encoding='utf-8') as fp:
data = fp.read() data = fp.read()
header = self.get_header(data) header = self.get_header(data)
@ -97,7 +97,7 @@ class Test_pygettext(unittest.TestCase):
from datetime import datetime from datetime import datetime
with temp_cwd(None) as cwd: with temp_cwd(None) as cwd:
assert_python_ok(self.script) assert_python_ok(self.script)
with open('messages.pot') as fp: with open('messages.pot', encoding='utf-8') as fp:
data = fp.read() data = fp.read()
header = self.get_header(data) header = self.get_header(data)
creationDate = header['POT-Creation-Date'] creationDate = header['POT-Creation-Date']
@ -299,16 +299,19 @@ class Test_pygettext(unittest.TestCase):
text3 = 'Text to ignore' text3 = 'Text to ignore'
with temp_cwd(None), temp_dir(None) as sdir: with temp_cwd(None), temp_dir(None) as sdir:
os.mkdir(os.path.join(sdir, 'pypkg')) 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})') sfile.write(f'_({text1!r})')
os.mkdir(os.path.join(sdir, 'pkg.py')) 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})') sfile.write(f'_({text2!r})')
os.mkdir(os.path.join(sdir, 'CVS')) 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})') sfile.write(f'_({text3!r})')
assert_python_ok(self.script, sdir) assert_python_ok(self.script, sdir)
with open('messages.pot') as fp: with open('messages.pot', encoding='utf-8') as fp:
data = fp.read() data = fp.read()
self.assertIn(f'msgid "{text1}"', data) self.assertIn(f'msgid "{text1}"', data)
self.assertIn(f'msgid "{text2}"', data) self.assertIn(f'msgid "{text2}"', data)

View File

@ -22,7 +22,7 @@ class lllTests(unittest.TestCase):
fn1 = os.path.join(dir1, 'foo1') fn1 = os.path.join(dir1, 'foo1')
fn2 = os.path.join(dir2, 'foo2') fn2 = os.path.join(dir2, 'foo2')
for fn, dir in (fn1, dir1), (fn2, dir2): for fn, dir in (fn1, dir1), (fn2, dir2):
open(fn, 'w').close() open(fn, 'wb').close()
os.symlink(fn, os.path.join(dir, 'symlink')) os.symlink(fn, os.path.join(dir, 'symlink'))
with support.captured_stdout() as output: with support.captured_stdout() as output:

View File

@ -19,7 +19,7 @@ class PdepsTests(unittest.TestCase):
# Issue #14492: m_import.match(line) can be None. # Issue #14492: m_import.match(line) can be None.
with tempfile.TemporaryDirectory() as tmpdir: with tempfile.TemporaryDirectory() as tmpdir:
fn = os.path.join(tmpdir, 'foo') 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") stream.write("#!/this/will/fail")
self.pdeps.process(fn, {}) self.pdeps.process(fn, {})

View File

@ -37,9 +37,9 @@ class PindentTests(unittest.TestCase):
self.maxDiff = None self.maxDiff = None
with os_helper.temp_dir() as directory: with os_helper.temp_dir() as directory:
data_path = os.path.join(directory, '_test.py') 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() closed = f.read()
with open(data_path, 'w') as f: with open(data_path, 'w', encoding='utf-8') as f:
f.write(closed) f.write(closed)
rc, out, err = assert_python_ok(self.script, '-d', data_path) rc, out, err = assert_python_ok(self.script, '-d', data_path)
@ -47,9 +47,9 @@ class PindentTests(unittest.TestCase):
self.assertEqual(err, b'') self.assertEqual(err, b'')
backup = data_path + '~' backup = data_path + '~'
self.assertTrue(os.path.exists(backup)) self.assertTrue(os.path.exists(backup))
with open(backup) as f: with open(backup, encoding='utf-8') as f:
self.assertEqual(f.read(), closed) self.assertEqual(f.read(), closed)
with open(data_path) as f: with open(data_path, encoding='utf-8') as f:
clean = f.read() clean = f.read()
compile(clean, '_test.py', 'exec') compile(clean, '_test.py', 'exec')
self.assertEqual(self.pindent(clean, '-c'), closed) self.assertEqual(self.pindent(clean, '-c'), closed)
@ -58,20 +58,20 @@ class PindentTests(unittest.TestCase):
rc, out, err = assert_python_ok(self.script, '-c', data_path) rc, out, err = assert_python_ok(self.script, '-c', data_path)
self.assertEqual(out, b'') self.assertEqual(out, b'')
self.assertEqual(err, b'') self.assertEqual(err, b'')
with open(backup) as f: with open(backup, encoding='utf-8') as f:
self.assertEqual(f.read(), clean) 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) self.assertEqual(f.read(), closed)
broken = self.lstriplines(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) f.write(broken)
rc, out, err = assert_python_ok(self.script, '-r', data_path) rc, out, err = assert_python_ok(self.script, '-r', data_path)
self.assertEqual(out, b'') self.assertEqual(out, b'')
self.assertEqual(err, b'') self.assertEqual(err, b'')
with open(backup) as f: with open(backup, encoding='utf-8') as f:
self.assertEqual(f.read(), broken) self.assertEqual(f.read(), broken)
with open(data_path) as f: with open(data_path, encoding='utf-8') as f:
indented = f.read() indented = f.read()
compile(indented, '_test.py', 'exec') compile(indented, '_test.py', 'exec')
self.assertEqual(self.pindent(broken, '-r'), indented) self.assertEqual(self.pindent(broken, '-r'), indented)

View File

@ -24,7 +24,7 @@ trailer = """\
""" """
def add_escapes(filename): def add_escapes(filename):
with open(filename) as fp: with open(filename, encoding="utf-8") as fp:
for line in fp: for line in fp:
yield html.escape(line) yield html.escape(line)
@ -79,7 +79,7 @@ def main():
filename = sys.argv[1] filename = sys.argv[1]
outputfilename = filename + ".html" outputfilename = filename + ".html"
input = add_escapes(filename) input = add_escapes(filename)
with open(outputfilename, "w") as output: with open(outputfilename, "w", encoding="utf-8") as output:
gprof2html(input, output, filename) gprof2html(input, output, filename)
webbrowser.open("file:" + os.path.abspath(outputfilename)) webbrowser.open("file:" + os.path.abspath(outputfilename))