Skip to content

Commit 8bbfeb3

Browse files
authored
bpo-43651: PEP 597: Fix EncodingWarning in some tests (GH-25142)
* test__xxsubinterpreters * test_builtin * test_doctest * test_exceptions * test_opcodes * test_support * test_argparse * test_baseexception * test_bdb * test_bool * test_asdl_parser
1 parent bef7b26 commit 8bbfeb3

11 files changed

+36
-31
lines changed

Lib/test/test__xxsubinterpreters.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ def _captured_script(script):
2525
indented = script.replace('\n', '\n ')
2626
wrapped = dedent(f"""
2727
import contextlib
28-
with open({w}, 'w') as spipe:
28+
with open({w}, 'w', encoding="utf-8") as spipe:
2929
with contextlib.redirect_stdout(spipe):
3030
{indented}
3131
""")
32-
return wrapped, open(r)
32+
return wrapped, open(r, encoding="utf-8")
3333

3434

3535
def _run_output(interp, request, shared=None):
@@ -45,7 +45,7 @@ def _running(interp):
4545
def run():
4646
interpreters.run_string(interp, dedent(f"""
4747
# wait for "signal"
48-
with open({r}) as rpipe:
48+
with open({r}, encoding="utf-8") as rpipe:
4949
rpipe.read()
5050
"""))
5151

@@ -54,7 +54,7 @@ def run():
5454

5555
yield
5656

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

@@ -806,7 +806,7 @@ def f():
806806
@unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
807807
def test_fork(self):
808808
import tempfile
809-
with tempfile.NamedTemporaryFile('w+') as file:
809+
with tempfile.NamedTemporaryFile('w+', encoding="utf-8") as file:
810810
file.write('')
811811
file.flush()
812812

@@ -816,7 +816,7 @@ def test_fork(self):
816816
try:
817817
os.fork()
818818
except RuntimeError:
819-
with open('{file.name}', 'w') as out:
819+
with open('{file.name}', 'w', encoding='utf-8') as out:
820820
out.write('{expected}')
821821
""")
822822
interpreters.run_string(self.id, script)

Lib/test/test_argparse.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def tearDown(self):
4545

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

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

14741474
parser_signature = Sig(fromfile_prefix_chars='@')
@@ -1498,7 +1498,7 @@ def setUp(self):
14981498
('hello', 'hello world!\n'),
14991499
]
15001500
for path, text in file_texts:
1501-
with open(path, 'w') as file:
1501+
with open(path, 'w', encoding="utf-8") as file:
15021502
file.write(text)
15031503

15041504
class FromFileConverterArgumentParser(ErrorRaisingArgumentParser):
@@ -1580,7 +1580,8 @@ class TestFileTypeR(TempDirMixin, ParserTestCase):
15801580
def setUp(self):
15811581
super(TestFileTypeR, self).setUp()
15821582
for file_name in ['foo', 'bar']:
1583-
with open(os.path.join(self.temp_dir, file_name), 'w') as file:
1583+
with open(os.path.join(self.temp_dir, file_name),
1584+
'w', encoding="utf-8") as file:
15841585
file.write(file_name)
15851586
self.create_readonly_file('readonly')
15861587

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

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

16261628
argument_signatures = [

Lib/test/test_baseexception.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ def test_inheritance(self):
2828
except TypeError:
2929
pass
3030

31-
inheritance_tree = open(os.path.join(os.path.split(__file__)[0],
32-
'exception_hierarchy.txt'))
31+
inheritance_tree = open(
32+
os.path.join(os.path.split(__file__)[0], 'exception_hierarchy.txt'),
33+
encoding="utf-8")
3334
try:
3435
superclass_name = inheritance_tree.readline().rstrip()
3536
try:

Lib/test/test_bdb.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def create_modules(modules):
539539
try:
540540
for m in modules:
541541
fname = m + '.py'
542-
with open(fname, 'w') as f:
542+
with open(fname, 'w', encoding="utf-8") as f:
543543
f.write(textwrap.dedent(modules[m]))
544544
linecache.checkcache(fname)
545545
importlib.invalidate_caches()

Lib/test/test_bool.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def test_boolean(self):
235235

236236
def test_fileclosed(self):
237237
try:
238-
with open(os_helper.TESTFN, "w") as f:
238+
with open(os_helper.TESTFN, "w", encoding="utf-8") as f:
239239
self.assertIs(f.closed, False)
240240
self.assertIs(f.closed, True)
241241
finally:

Lib/test/test_builtin.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ def test_oct(self):
11591159

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

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

11981198
self.write_testfile()
11991199
current_locale_encoding = locale.getpreferredencoding(False)
1200-
fp = open(TESTFN, 'w')
1200+
with warnings.catch_warnings():
1201+
warnings.simplefilter("ignore", EncodingWarning)
1202+
fp = open(TESTFN, 'w')
12011203
with fp:
12021204
self.assertEqual(fp.encoding, current_locale_encoding)
12031205
finally:
12041206
os.environ.clear()
12051207
os.environ.update(old_environ)
12061208

12071209
def test_open_non_inheritable(self):
1208-
fileobj = open(__file__)
1210+
fileobj = open(__file__, encoding="utf-8")
12091211
with fileobj:
12101212
self.assertFalse(os.get_inheritable(fileobj.fileno()))
12111213

@@ -1300,7 +1302,7 @@ def test_pow(self):
13001302

13011303
def test_input(self):
13021304
self.write_testfile()
1303-
fp = open(TESTFN, 'r')
1305+
fp = open(TESTFN, encoding="utf-8")
13041306
savestdin = sys.stdin
13051307
savestdout = sys.stdout # Eats the echo
13061308
try:
@@ -2022,7 +2024,7 @@ def _run_child(self, child, terminal_input):
20222024
os.write(fd, terminal_input)
20232025

20242026
# Get results from the pipe
2025-
with open(r, "r") as rpipe:
2027+
with open(r, encoding="utf-8") as rpipe:
20262028
lines = []
20272029
while True:
20282030
line = rpipe.readline().strip()

Lib/test/test_doctest.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2846,7 +2846,7 @@ def test_CLI(): r"""
28462846
>>> from test.support.os_helper import temp_dir
28472847
>>> with temp_dir() as tmpdir:
28482848
... fn = os.path.join(tmpdir, 'myfile.doc')
2849-
... with open(fn, 'w') as f:
2849+
... with open(fn, 'w', encoding='utf-8') as f:
28502850
... _ = f.write('This is a very simple test file.\n')
28512851
... _ = f.write(' >>> 1 + 1\n')
28522852
... _ = f.write(' 2\n')
@@ -2898,7 +2898,7 @@ def test_CLI(): r"""
28982898
>>> from test.support.os_helper import temp_dir
28992899
>>> with temp_dir() as tmpdir:
29002900
... fn = os.path.join(tmpdir, 'myfile.doc')
2901-
... with open(fn, 'w') as f:
2901+
... with open(fn, 'w', encoding="utf-8") as f:
29022902
... _ = f.write('This is another simple test file.\n')
29032903
... _ = f.write(' >>> 1 + 1\n')
29042904
... _ = f.write(' 2\n')
@@ -2909,7 +2909,7 @@ def test_CLI(): r"""
29092909
... _ = f.write('\n')
29102910
... _ = f.write('And that is it.\n')
29112911
... fn2 = os.path.join(tmpdir, 'myfile2.py')
2912-
... with open(fn2, 'w') as f:
2912+
... with open(fn2, 'w', encoding='utf-8') as f:
29132913
... _ = f.write('def test_func():\n')
29142914
... _ = f.write(' \"\"\"\n')
29152915
... _ = f.write(' This is simple python test function.\n')

Lib/test/test_exceptions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ def testRaising(self):
5454
self.assertRaises(AttributeError, getattr, sys, "undefined_attribute")
5555

5656
self.raise_catch(EOFError, "EOFError")
57-
fp = open(TESTFN, 'w')
57+
fp = open(TESTFN, 'w', encoding="utf-8")
5858
fp.close()
59-
fp = open(TESTFN, 'r')
59+
fp = open(TESTFN, 'r', encoding="utf-8")
6060
savestdin = sys.stdin
6161
try:
6262
try:

Lib/test/test_opcodes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_try_inside_for_loop(self):
2424
def test_setup_annotations_line(self):
2525
# check that SETUP_ANNOTATIONS does not create spurious line numbers
2626
try:
27-
with open(ann_module.__file__) as f:
27+
with open(ann_module.__file__, encoding="utf-8") as f:
2828
txt = f.read()
2929
co = compile(txt, ann_module.__file__, 'exec')
3030
self.assertEqual(co.co_firstlineno, 1)

Lib/test/test_support.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_unload(self):
4747
self.assertNotIn("sched", sys.modules)
4848

4949
def test_unlink(self):
50-
with open(TESTFN, "w") as f:
50+
with open(TESTFN, "w", encoding="utf-8") as f:
5151
pass
5252
os_helper.unlink(TESTFN)
5353
self.assertFalse(os.path.exists(TESTFN))
@@ -79,7 +79,7 @@ def test_rmtree(self):
7979

8080
def test_forget(self):
8181
mod_filename = TESTFN + '.py'
82-
with open(mod_filename, 'w') as f:
82+
with open(mod_filename, 'w', encoding="utf-8") as f:
8383
print('foo = 1', file=f)
8484
sys.path.insert(0, os.curdir)
8585
importlib.invalidate_caches()

Parser/asdl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def check(mod):
204204

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

0 commit comments

Comments
 (0)