Skip to content

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

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 6 commits into from
Apr 2, 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
12 changes: 6 additions & 6 deletions Lib/test/test__xxsubinterpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ def _captured_script(script):
indented = script.replace('\n', '\n ')
wrapped = dedent(f"""
import contextlib
with open({w}, 'w') as spipe:
with open({w}, 'w', encoding="utf-8") as spipe:
with contextlib.redirect_stdout(spipe):
{indented}
""")
return wrapped, open(r)
return wrapped, open(r, encoding="utf-8")


def _run_output(interp, request, shared=None):
Expand All @@ -45,7 +45,7 @@ def _running(interp):
def run():
interpreters.run_string(interp, dedent(f"""
# wait for "signal"
with open({r}) as rpipe:
with open({r}, encoding="utf-8") as rpipe:
rpipe.read()
"""))

Expand All @@ -54,7 +54,7 @@ def run():

yield

with open(w, 'w') as spipe:
with open(w, 'w', encoding="utf-8") as spipe:
spipe.write('done')
t.join()

Expand Down Expand Up @@ -806,7 +806,7 @@ def f():
@unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
def test_fork(self):
import tempfile
with tempfile.NamedTemporaryFile('w+') as file:
with tempfile.NamedTemporaryFile('w+', encoding="utf-8") as file:
file.write('')
file.flush()

Expand All @@ -816,7 +816,7 @@ def test_fork(self):
try:
os.fork()
except RuntimeError:
with open('{file.name}', 'w') as out:
with open('{file.name}', 'w', encoding='utf-8') as out:
out.write('{expected}')
""")
interpreters.run_string(self.id, script)
Expand Down
14 changes: 8 additions & 6 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def tearDown(self):

def create_readonly_file(self, filename):
file_path = os.path.join(self.temp_dir, filename)
with open(file_path, 'w') as file:
with open(file_path, 'w', encoding="utf-8") as file:
file.write(filename)
os.chmod(file_path, stat.S_IREAD)

Expand Down Expand Up @@ -1468,7 +1468,7 @@ def setUp(self):
('invalid', '@no-such-path\n'),
]
for path, text in file_texts:
with open(path, 'w') as file:
with open(path, 'w', encoding="utf-8") as file:
file.write(text)

parser_signature = Sig(fromfile_prefix_chars='@')
Expand Down Expand Up @@ -1498,7 +1498,7 @@ def setUp(self):
('hello', 'hello world!\n'),
]
for path, text in file_texts:
with open(path, 'w') as file:
with open(path, 'w', encoding="utf-8") as file:
file.write(text)

class FromFileConverterArgumentParser(ErrorRaisingArgumentParser):
Expand Down Expand Up @@ -1580,7 +1580,8 @@ class TestFileTypeR(TempDirMixin, ParserTestCase):
def setUp(self):
super(TestFileTypeR, self).setUp()
for file_name in ['foo', 'bar']:
with open(os.path.join(self.temp_dir, file_name), 'w') as file:
with open(os.path.join(self.temp_dir, file_name),
'w', encoding="utf-8") as file:
file.write(file_name)
self.create_readonly_file('readonly')

Expand All @@ -1601,7 +1602,7 @@ class TestFileTypeDefaults(TempDirMixin, ParserTestCase):
"""Test that a file is not created unless the default is needed"""
def setUp(self):
super(TestFileTypeDefaults, self).setUp()
file = open(os.path.join(self.temp_dir, 'good'), 'w')
file = open(os.path.join(self.temp_dir, 'good'), 'w', encoding="utf-8")
file.write('good')
file.close()

Expand All @@ -1620,7 +1621,8 @@ class TestFileTypeRB(TempDirMixin, ParserTestCase):
def setUp(self):
super(TestFileTypeRB, self).setUp()
for file_name in ['foo', 'bar']:
with open(os.path.join(self.temp_dir, file_name), 'w') as file:
with open(os.path.join(self.temp_dir, file_name),
'w', encoding="utf-8") as file:
file.write(file_name)

argument_signatures = [
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_baseexception.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ def test_inheritance(self):
except TypeError:
pass

inheritance_tree = open(os.path.join(os.path.split(__file__)[0],
'exception_hierarchy.txt'))
inheritance_tree = open(
os.path.join(os.path.split(__file__)[0], 'exception_hierarchy.txt'),
encoding="utf-8")
try:
superclass_name = inheritance_tree.readline().rstrip()
try:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_bdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ def create_modules(modules):
try:
for m in modules:
fname = m + '.py'
with open(fname, 'w') as f:
with open(fname, 'w', encoding="utf-8") as f:
f.write(textwrap.dedent(modules[m]))
linecache.checkcache(fname)
importlib.invalidate_caches()
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def test_boolean(self):

def test_fileclosed(self):
try:
with open(os_helper.TESTFN, "w") as f:
with open(os_helper.TESTFN, "w", encoding="utf-8") as f:
self.assertIs(f.closed, False)
self.assertIs(f.closed, True)
finally:
Expand Down
14 changes: 8 additions & 6 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ def test_oct(self):

def write_testfile(self):
# NB the first 4 lines are also used to test input, below
fp = open(TESTFN, 'w')
fp = open(TESTFN, 'w', encoding="utf-8")
self.addCleanup(unlink, TESTFN)
with fp:
fp.write('1+1\n')
Expand All @@ -1171,7 +1171,7 @@ def write_testfile(self):

def test_open(self):
self.write_testfile()
fp = open(TESTFN, 'r')
fp = open(TESTFN, encoding="utf-8")
with fp:
self.assertEqual(fp.readline(4), '1+1\n')
self.assertEqual(fp.readline(), 'The quick brown fox jumps over the lazy dog.\n')
Expand All @@ -1197,15 +1197,17 @@ def test_open_default_encoding(self):

self.write_testfile()
current_locale_encoding = locale.getpreferredencoding(False)
fp = open(TESTFN, 'w')
with warnings.catch_warnings():
warnings.simplefilter("ignore", EncodingWarning)
fp = open(TESTFN, 'w')
with fp:
self.assertEqual(fp.encoding, current_locale_encoding)
finally:
os.environ.clear()
os.environ.update(old_environ)

def test_open_non_inheritable(self):
fileobj = open(__file__)
fileobj = open(__file__, encoding="utf-8")
with fileobj:
self.assertFalse(os.get_inheritable(fileobj.fileno()))

Expand Down Expand Up @@ -1300,7 +1302,7 @@ def test_pow(self):

def test_input(self):
self.write_testfile()
fp = open(TESTFN, 'r')
fp = open(TESTFN, encoding="utf-8")
savestdin = sys.stdin
savestdout = sys.stdout # Eats the echo
try:
Expand Down Expand Up @@ -2022,7 +2024,7 @@ def _run_child(self, child, terminal_input):
os.write(fd, terminal_input)

# Get results from the pipe
with open(r, "r") as rpipe:
with open(r, encoding="utf-8") as rpipe:
lines = []
while True:
line = rpipe.readline().strip()
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2846,7 +2846,7 @@ def test_CLI(): r"""
>>> from test.support.os_helper import temp_dir
>>> with temp_dir() as tmpdir:
... fn = os.path.join(tmpdir, 'myfile.doc')
... with open(fn, 'w') as f:
... with open(fn, 'w', encoding='utf-8') as f:
... _ = f.write('This is a very simple test file.\n')
... _ = f.write(' >>> 1 + 1\n')
... _ = f.write(' 2\n')
Expand Down Expand Up @@ -2898,7 +2898,7 @@ def test_CLI(): r"""
>>> from test.support.os_helper import temp_dir
>>> with temp_dir() as tmpdir:
... fn = os.path.join(tmpdir, 'myfile.doc')
... with open(fn, 'w') as f:
... with open(fn, 'w', encoding="utf-8") as f:
... _ = f.write('This is another simple test file.\n')
... _ = f.write(' >>> 1 + 1\n')
... _ = f.write(' 2\n')
Expand All @@ -2909,7 +2909,7 @@ def test_CLI(): r"""
... _ = f.write('\n')
... _ = f.write('And that is it.\n')
... fn2 = os.path.join(tmpdir, 'myfile2.py')
... with open(fn2, 'w') as f:
... with open(fn2, 'w', encoding='utf-8') as f:
... _ = f.write('def test_func():\n')
... _ = f.write(' \"\"\"\n')
... _ = f.write(' This is simple python test function.\n')
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def testRaising(self):
self.assertRaises(AttributeError, getattr, sys, "undefined_attribute")

self.raise_catch(EOFError, "EOFError")
fp = open(TESTFN, 'w')
fp = open(TESTFN, 'w', encoding="utf-8")
fp.close()
fp = open(TESTFN, 'r')
fp = open(TESTFN, 'r', encoding="utf-8")
savestdin = sys.stdin
try:
try:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_try_inside_for_loop(self):
def test_setup_annotations_line(self):
# check that SETUP_ANNOTATIONS does not create spurious line numbers
try:
with open(ann_module.__file__) as f:
with open(ann_module.__file__, encoding="utf-8") as f:
txt = f.read()
co = compile(txt, ann_module.__file__, 'exec')
self.assertEqual(co.co_firstlineno, 1)
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_unload(self):
self.assertNotIn("sched", sys.modules)

def test_unlink(self):
with open(TESTFN, "w") as f:
with open(TESTFN, "w", encoding="utf-8") as f:
pass
os_helper.unlink(TESTFN)
self.assertFalse(os.path.exists(TESTFN))
Expand Down Expand Up @@ -79,7 +79,7 @@ def test_rmtree(self):

def test_forget(self):
mod_filename = TESTFN + '.py'
with open(mod_filename, 'w') as f:
with open(mod_filename, 'w', encoding="utf-8") as f:
print('foo = 1', file=f)
sys.path.insert(0, os.curdir)
importlib.invalidate_caches()
Expand Down
2 changes: 1 addition & 1 deletion Parser/asdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def check(mod):

def parse(filename):
"""Parse ASDL from the given file and return a Module node describing it."""
with open(filename) as f:
with open(filename, encoding="utf-8") as f:
parser = ASDLParser()
return parser.parse(f.read())

Expand Down