Skip to content

Commit 4663e5f

Browse files
authored
bpo-43651: PEP 597: Fix EncodingWarning in some tests (GH-25190)
* Fix test_lzma * Fix test_mailbox * Fix test_mimetypes * Fix test_posix
1 parent fb78692 commit 4663e5f

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

Lib/test/test_lzma.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,14 +1250,14 @@ def test_binary_modes(self):
12501250
def test_text_modes(self):
12511251
uncompressed = INPUT.decode("ascii")
12521252
uncompressed_raw = uncompressed.replace("\n", os.linesep)
1253-
with lzma.open(BytesIO(COMPRESSED_XZ), "rt") as f:
1253+
with lzma.open(BytesIO(COMPRESSED_XZ), "rt", encoding="ascii") as f:
12541254
self.assertEqual(f.read(), uncompressed)
12551255
with BytesIO() as bio:
1256-
with lzma.open(bio, "wt") as f:
1256+
with lzma.open(bio, "wt", encoding="ascii") as f:
12571257
f.write(uncompressed)
12581258
file_data = lzma.decompress(bio.getvalue()).decode("ascii")
12591259
self.assertEqual(file_data, uncompressed_raw)
1260-
with lzma.open(bio, "at") as f:
1260+
with lzma.open(bio, "at", encoding="ascii") as f:
12611261
f.write(uncompressed)
12621262
file_data = lzma.decompress(bio.getvalue()).decode("ascii")
12631263
self.assertEqual(file_data, uncompressed_raw * 2)
@@ -1334,17 +1334,18 @@ def test_newline(self):
13341334
# Test with explicit newline (universal newline mode disabled).
13351335
text = INPUT.decode("ascii")
13361336
with BytesIO() as bio:
1337-
with lzma.open(bio, "wt", newline="\n") as f:
1337+
with lzma.open(bio, "wt", encoding="ascii", newline="\n") as f:
13381338
f.write(text)
13391339
bio.seek(0)
1340-
with lzma.open(bio, "rt", newline="\r") as f:
1340+
with lzma.open(bio, "rt", encoding="ascii", newline="\r") as f:
13411341
self.assertEqual(f.readlines(), [text])
13421342

13431343
def test_x_mode(self):
13441344
self.addCleanup(unlink, TESTFN)
13451345
for mode in ("x", "xb", "xt"):
13461346
unlink(TESTFN)
1347-
with lzma.open(TESTFN, mode):
1347+
encoding = "ascii" if "t" in mode else None
1348+
with lzma.open(TESTFN, mode, encoding=encoding):
13481349
pass
13491350
with self.assertRaises(FileExistsError):
13501351
with lzma.open(TESTFN, mode):

Lib/test/test_mailbox.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_add(self):
7777
self.assertEqual(len(self._box), 6)
7878
with self.assertWarns(DeprecationWarning):
7979
keys.append(self._box.add(
80-
io.TextIOWrapper(io.BytesIO(_bytes_sample_message))))
80+
io.TextIOWrapper(io.BytesIO(_bytes_sample_message), encoding="utf-8")))
8181
self.assertEqual(len(self._box), 7)
8282
self.assertEqual(self._box.get_string(keys[0]), self._template % 0)
8383
for i in (1, 2, 3, 4, 5, 6):
@@ -160,7 +160,7 @@ def test_add_binary_nonascii_file(self):
160160
self._non_latin_bin_msg.split(b'\n'))
161161

162162
def test_add_text_file_warns(self):
163-
with tempfile.TemporaryFile('w+') as f:
163+
with tempfile.TemporaryFile('w+', encoding='utf-8') as f:
164164
f.write(_sample_message)
165165
f.seek(0)
166166
with self.assertWarns(DeprecationWarning):
@@ -724,9 +724,9 @@ def test_clean(self):
724724
# Remove old files from 'tmp'
725725
foo_path = os.path.join(self._path, 'tmp', 'foo')
726726
bar_path = os.path.join(self._path, 'tmp', 'bar')
727-
with open(foo_path, 'w') as f:
727+
with open(foo_path, 'w', encoding='utf-8') as f:
728728
f.write("@")
729-
with open(bar_path, 'w') as f:
729+
with open(bar_path, 'w', encoding='utf-8') as f:
730730
f.write("@")
731731
self._box.clean()
732732
self.assertTrue(os.path.exists(foo_path))
@@ -984,7 +984,7 @@ def tearDown(self):
984984
os_helper.unlink(lock_remnant)
985985

986986
def assertMailboxEmpty(self):
987-
with open(self._path) as f:
987+
with open(self._path, 'rb') as f:
988988
self.assertEqual(f.readlines(), [])
989989

990990
def test_get_bytes_from(self):
@@ -1150,12 +1150,12 @@ def test_terminating_newline(self):
11501150
def test_message_separator(self):
11511151
# Check there's always a single blank line after each message
11521152
self._box.add('From: foo\n\n0') # No newline at the end
1153-
with open(self._path) as f:
1153+
with open(self._path, encoding='utf-8') as f:
11541154
data = f.read()
11551155
self.assertEqual(data[-3:], '0\n\n')
11561156

11571157
self._box.add('From: foo\n\n0\n') # Newline at the end
1158-
with open(self._path) as f:
1158+
with open(self._path, encoding='utf-8') as f:
11591159
data = f.read()
11601160
self.assertEqual(data[-3:], '0\n\n')
11611161

@@ -1305,7 +1305,7 @@ class TestBabyl(_TestSingleFile, unittest.TestCase):
13051305
_factory = lambda self, path, factory=None: mailbox.Babyl(path, factory)
13061306

13071307
def assertMailboxEmpty(self):
1308-
with open(self._path) as f:
1308+
with open(self._path, 'rb') as f:
13091309
self.assertEqual(f.readlines(), [])
13101310

13111311
def tearDown(self):
@@ -1390,7 +1390,7 @@ def test_initialize_with_string(self):
13901390

13911391
def test_initialize_with_file(self):
13921392
# Initialize based on contents of file
1393-
with open(self._path, 'w+') as f:
1393+
with open(self._path, 'w+', encoding='utf-8') as f:
13941394
f.write(_sample_message)
13951395
f.seek(0)
13961396
msg = self._factory(f)
@@ -2158,7 +2158,7 @@ def createMessage(self, dir, mbox=False):
21582158
filename = ".".join((str(t), str(pid), "myhostname", "mydomain"))
21592159
tmpname = os.path.join(self._dir, "tmp", filename)
21602160
newname = os.path.join(self._dir, dir, filename)
2161-
with open(tmpname, "w") as fp:
2161+
with open(tmpname, "w", encoding="utf-8") as fp:
21622162
self._msgfiles.append(tmpname)
21632163
if mbox:
21642164
fp.write(FROM_)

Lib/test/test_mimetypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_read_mime_types(self):
6464
with os_helper.temp_dir() as directory:
6565
data = "x-application/x-unittest pyunit\n"
6666
file = pathlib.Path(directory, "sample.mimetype")
67-
file.write_text(data)
67+
file.write_text(data, encoding="utf-8")
6868
mime_dict = mimetypes.read_mime_types(file)
6969
eq(mime_dict[".pyunit"], "x-application/x-unittest")
7070

Lib/test/test_posix.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class PosixTester(unittest.TestCase):
4545

4646
def setUp(self):
4747
# create empty file
48-
fp = open(os_helper.TESTFN, 'w+')
49-
fp.close()
48+
with open(os_helper.TESTFN, "wb"):
49+
pass
5050
self.teardown_files = [ os_helper.TESTFN ]
5151
self._warnings_manager = warnings_helper.check_warnings()
5252
self._warnings_manager.__enter__()
@@ -1579,7 +1579,7 @@ def test_returns_pid(self):
15791579
args = self.python_args('-c', script)
15801580
pid = self.spawn_func(args[0], args, os.environ)
15811581
support.wait_process(pid, exitcode=0)
1582-
with open(pidfile) as f:
1582+
with open(pidfile, encoding="utf-8") as f:
15831583
self.assertEqual(f.read(), str(pid))
15841584

15851585
def test_no_such_executable(self):
@@ -1602,14 +1602,14 @@ def test_specify_environment(self):
16021602
self.addCleanup(os_helper.unlink, envfile)
16031603
script = f"""if 1:
16041604
import os
1605-
with open({envfile!r}, "w") as envfile:
1605+
with open({envfile!r}, "w", encoding="utf-8") as envfile:
16061606
envfile.write(os.environ['foo'])
16071607
"""
16081608
args = self.python_args('-c', script)
16091609
pid = self.spawn_func(args[0], args,
16101610
{**os.environ, 'foo': 'bar'})
16111611
support.wait_process(pid, exitcode=0)
1612-
with open(envfile) as f:
1612+
with open(envfile, encoding="utf-8") as f:
16131613
self.assertEqual(f.read(), 'bar')
16141614

16151615
def test_none_file_actions(self):
@@ -1861,7 +1861,7 @@ def test_open_file(self):
18611861
file_actions=file_actions)
18621862

18631863
support.wait_process(pid, exitcode=0)
1864-
with open(outfile) as f:
1864+
with open(outfile, encoding="utf-8") as f:
18651865
self.assertEqual(f.read(), 'hello')
18661866

18671867
def test_close_file(self):
@@ -1872,15 +1872,15 @@ def test_close_file(self):
18721872
try:
18731873
os.fstat(0)
18741874
except OSError as e:
1875-
with open({closefile!r}, 'w') as closefile:
1875+
with open({closefile!r}, 'w', encoding='utf-8') as closefile:
18761876
closefile.write('is closed %d' % e.errno)
18771877
"""
18781878
args = self.python_args('-c', script)
18791879
pid = self.spawn_func(args[0], args, os.environ,
18801880
file_actions=[(os.POSIX_SPAWN_CLOSE, 0)])
18811881

18821882
support.wait_process(pid, exitcode=0)
1883-
with open(closefile) as f:
1883+
with open(closefile, encoding="utf-8") as f:
18841884
self.assertEqual(f.read(), 'is closed %d' % errno.EBADF)
18851885

18861886
def test_dup2(self):
@@ -1898,7 +1898,7 @@ def test_dup2(self):
18981898
pid = self.spawn_func(args[0], args, os.environ,
18991899
file_actions=file_actions)
19001900
support.wait_process(pid, exitcode=0)
1901-
with open(dupfile) as f:
1901+
with open(dupfile, encoding="utf-8") as f:
19021902
self.assertEqual(f.read(), 'hello')
19031903

19041904

0 commit comments

Comments
 (0)