bpo-43651: Fix EncodingWarning in `os.fdopen()` and test_os (GH-25654)

This commit is contained in:
Inada Naoki 2021-04-29 11:35:36 +09:00 committed by GitHub
parent fa51c0c448
commit a69256527f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 13 deletions

View File

@ -983,16 +983,16 @@ if sys.platform != 'vxworks':
import subprocess, io
if mode == "r":
proc = subprocess.Popen(cmd,
shell=True,
shell=True, text=True,
stdout=subprocess.PIPE,
bufsize=buffering)
return _wrap_close(io.TextIOWrapper(proc.stdout), proc)
return _wrap_close(proc.stdout, proc)
else:
proc = subprocess.Popen(cmd,
shell=True,
shell=True, text=True,
stdin=subprocess.PIPE,
bufsize=buffering)
return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
return _wrap_close(proc.stdin, proc)
# Helper for popen() -- a proxy for a file whose close waits for the process
class _wrap_close:

View File

@ -269,7 +269,7 @@ class FileTests(unittest.TestCase):
def fdopen_helper(self, *args):
fd = os.open(os_helper.TESTFN, os.O_RDONLY)
f = os.fdopen(fd, *args)
f = os.fdopen(fd, *args, encoding="utf-8")
f.close()
def test_fdopen(self):
@ -290,7 +290,7 @@ class FileTests(unittest.TestCase):
os.replace(os_helper.TESTFN, TESTFN2)
self.assertRaises(FileNotFoundError, os.stat, os_helper.TESTFN)
with open(TESTFN2, 'r') as f:
with open(TESTFN2, 'r', encoding='utf-8') as f:
self.assertEqual(f.read(), "1")
def test_open_keywords(self):
@ -1627,7 +1627,7 @@ class MakedirTests(unittest.TestCase):
def test_exist_ok_existing_regular_file(self):
base = os_helper.TESTFN
path = os.path.join(os_helper.TESTFN, 'dir1')
with open(path, 'w') as f:
with open(path, 'w', encoding='utf-8') as f:
f.write('abc')
self.assertRaises(OSError, os.makedirs, path)
self.assertRaises(OSError, os.makedirs, path, exist_ok=False)
@ -2094,7 +2094,7 @@ class Win32ErrorTests(unittest.TestCase):
class TestInvalidFD(unittest.TestCase):
singles = ["fchdir", "dup", "fdopen", "fdatasync", "fstat",
singles = ["fchdir", "dup", "fdatasync", "fstat",
"fstatvfs", "fsync", "tcgetpgrp", "ttyname"]
#singles.append("close")
#We omit close because it doesn't raise an exception on some platforms
@ -2106,15 +2106,18 @@ class TestInvalidFD(unittest.TestCase):
for f in singles:
locals()["test_"+f] = get_single(f)
def check(self, f, *args):
def check(self, f, *args, **kwargs):
try:
f(os_helper.make_bad_fd(), *args)
f(os_helper.make_bad_fd(), *args, **kwargs)
except OSError as e:
self.assertEqual(e.errno, errno.EBADF)
else:
self.fail("%r didn't raise an OSError with a bad file descriptor"
% f)
def test_fdopen(self):
self.check(os.fdopen, encoding="utf-8")
@unittest.skipUnless(hasattr(os, 'isatty'), 'test needs os.isatty()')
def test_isatty(self):
self.assertEqual(os.isatty(os_helper.make_bad_fd()), False)
@ -2210,7 +2213,7 @@ class LinkTests(unittest.TestCase):
os.link(file1, file2)
except PermissionError as e:
self.skipTest('os.link(): %s' % e)
with open(file1, "r") as f1, open(file2, "r") as f2:
with open(file1, "rb") as f1, open(file2, "rb") as f2:
self.assertTrue(os.path.sameopenfile(f1.fileno(), f2.fileno()))
def test_link(self):
@ -3009,7 +3012,7 @@ class SpawnTests(unittest.TestCase):
code = ('import sys, os; magic = os.environ[%r]; sys.exit(%s)'
% (self.key, self.exitcode))
with open(filename, "w") as fp:
with open(filename, "w", encoding="utf-8") as fp:
fp.write(code)
args = [sys.executable, filename]
@ -3149,7 +3152,7 @@ class SpawnTests(unittest.TestCase):
# equal character in the environment variable value
filename = os_helper.TESTFN
self.addCleanup(os_helper.unlink, filename)
with open(filename, "w") as fp:
with open(filename, "w", encoding="utf-8") as fp:
fp.write('import sys, os\n'
'if os.getenv("FRUIT") != "orange=lemon":\n'
' raise AssertionError')