Skip to content

Commit a692565

Browse files
authored
bpo-43651: Fix EncodingWarning in os.fdopen() and test_os (GH-25654)
1 parent fa51c0c commit a692565

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

Lib/os.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -983,16 +983,16 @@ def popen(cmd, mode="r", buffering=-1):
983983
import subprocess, io
984984
if mode == "r":
985985
proc = subprocess.Popen(cmd,
986-
shell=True,
986+
shell=True, text=True,
987987
stdout=subprocess.PIPE,
988988
bufsize=buffering)
989-
return _wrap_close(io.TextIOWrapper(proc.stdout), proc)
989+
return _wrap_close(proc.stdout, proc)
990990
else:
991991
proc = subprocess.Popen(cmd,
992-
shell=True,
992+
shell=True, text=True,
993993
stdin=subprocess.PIPE,
994994
bufsize=buffering)
995-
return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
995+
return _wrap_close(proc.stdin, proc)
996996

997997
# Helper for popen() -- a proxy for a file whose close waits for the process
998998
class _wrap_close:

Lib/test/test_os.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def test_write_windows_console(self):
269269

270270
def fdopen_helper(self, *args):
271271
fd = os.open(os_helper.TESTFN, os.O_RDONLY)
272-
f = os.fdopen(fd, *args)
272+
f = os.fdopen(fd, *args, encoding="utf-8")
273273
f.close()
274274

275275
def test_fdopen(self):
@@ -290,7 +290,7 @@ def test_replace(self):
290290

291291
os.replace(os_helper.TESTFN, TESTFN2)
292292
self.assertRaises(FileNotFoundError, os.stat, os_helper.TESTFN)
293-
with open(TESTFN2, 'r') as f:
293+
with open(TESTFN2, 'r', encoding='utf-8') as f:
294294
self.assertEqual(f.read(), "1")
295295

296296
def test_open_keywords(self):
@@ -1627,7 +1627,7 @@ def test_exist_ok_s_isgid_directory(self):
16271627
def test_exist_ok_existing_regular_file(self):
16281628
base = os_helper.TESTFN
16291629
path = os.path.join(os_helper.TESTFN, 'dir1')
1630-
with open(path, 'w') as f:
1630+
with open(path, 'w', encoding='utf-8') as f:
16311631
f.write('abc')
16321632
self.assertRaises(OSError, os.makedirs, path)
16331633
self.assertRaises(OSError, os.makedirs, path, exist_ok=False)
@@ -2094,7 +2094,7 @@ def test_chmod(self):
20942094

20952095

20962096
class TestInvalidFD(unittest.TestCase):
2097-
singles = ["fchdir", "dup", "fdopen", "fdatasync", "fstat",
2097+
singles = ["fchdir", "dup", "fdatasync", "fstat",
20982098
"fstatvfs", "fsync", "tcgetpgrp", "ttyname"]
20992099
#singles.append("close")
21002100
#We omit close because it doesn't raise an exception on some platforms
@@ -2106,15 +2106,18 @@ def helper(self):
21062106
for f in singles:
21072107
locals()["test_"+f] = get_single(f)
21082108

2109-
def check(self, f, *args):
2109+
def check(self, f, *args, **kwargs):
21102110
try:
2111-
f(os_helper.make_bad_fd(), *args)
2111+
f(os_helper.make_bad_fd(), *args, **kwargs)
21122112
except OSError as e:
21132113
self.assertEqual(e.errno, errno.EBADF)
21142114
else:
21152115
self.fail("%r didn't raise an OSError with a bad file descriptor"
21162116
% f)
21172117

2118+
def test_fdopen(self):
2119+
self.check(os.fdopen, encoding="utf-8")
2120+
21182121
@unittest.skipUnless(hasattr(os, 'isatty'), 'test needs os.isatty()')
21192122
def test_isatty(self):
21202123
self.assertEqual(os.isatty(os_helper.make_bad_fd()), False)
@@ -2210,7 +2213,7 @@ def _test_link(self, file1, file2):
22102213
os.link(file1, file2)
22112214
except PermissionError as e:
22122215
self.skipTest('os.link(): %s' % e)
2213-
with open(file1, "r") as f1, open(file2, "r") as f2:
2216+
with open(file1, "rb") as f1, open(file2, "rb") as f2:
22142217
self.assertTrue(os.path.sameopenfile(f1.fileno(), f2.fileno()))
22152218

22162219
def test_link(self):
@@ -3009,7 +3012,7 @@ def create_args(self, *, with_env=False, use_bytes=False):
30093012
code = ('import sys, os; magic = os.environ[%r]; sys.exit(%s)'
30103013
% (self.key, self.exitcode))
30113014

3012-
with open(filename, "w") as fp:
3015+
with open(filename, "w", encoding="utf-8") as fp:
30133016
fp.write(code)
30143017

30153018
args = [sys.executable, filename]
@@ -3149,7 +3152,7 @@ def _test_invalid_env(self, spawn):
31493152
# equal character in the environment variable value
31503153
filename = os_helper.TESTFN
31513154
self.addCleanup(os_helper.unlink, filename)
3152-
with open(filename, "w") as fp:
3155+
with open(filename, "w", encoding="utf-8") as fp:
31533156
fp.write('import sys, os\n'
31543157
'if os.getenv("FRUIT") != "orange=lemon":\n'
31553158
' raise AssertionError')

0 commit comments

Comments
 (0)