[Python-checkins] bpo-43651: Fix EncodingWarning in `os.fdopen()` and test_os (GH-25654)
methane
webhook-mailer at python.org
Wed Apr 28 22:35:46 EDT 2021
https://github.com/python/cpython/commit/a69256527f93d2aa32e76658deab829e324d97b6
commit: a69256527f93d2aa32e76658deab829e324d97b6
branch: master
author: Inada Naoki <songofacandy at gmail.com>
committer: methane <songofacandy at gmail.com>
date: 2021-04-29T11:35:36+09:00
summary:
bpo-43651: Fix EncodingWarning in `os.fdopen()` and test_os (GH-25654)
files:
M Lib/os.py
M Lib/test/test_os.py
diff --git a/Lib/os.py b/Lib/os.py
index ea09e8c7e1048..d26cfc99939f3 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -983,16 +983,16 @@ def popen(cmd, mode="r", buffering=-1):
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:
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 96fddc7d05766..8b3d1feb78fe3 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -269,7 +269,7 @@ def test_write_windows_console(self):
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 @@ def test_replace(self):
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 @@ def test_exist_ok_s_isgid_directory(self):
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 @@ def test_chmod(self):
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 @@ def helper(self):
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 @@ def _test_link(self, file1, file2):
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 @@ def create_args(self, *, with_env=False, use_bytes=False):
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 @@ def _test_invalid_env(self, spawn):
# 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')
More information about the Python-checkins
mailing list