mirror of https://github.com/python/cpython.git
bpo-43651: Fix EncodingWarning in `os.fdopen()` and test_os (GH-25654)
This commit is contained in:
parent
fa51c0c448
commit
a69256527f
|
@ -983,16 +983,16 @@ def popen(cmd, mode="r", buffering=-1):
|
||||||
import subprocess, io
|
import subprocess, io
|
||||||
if mode == "r":
|
if mode == "r":
|
||||||
proc = subprocess.Popen(cmd,
|
proc = subprocess.Popen(cmd,
|
||||||
shell=True,
|
shell=True, text=True,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
bufsize=buffering)
|
bufsize=buffering)
|
||||||
return _wrap_close(io.TextIOWrapper(proc.stdout), proc)
|
return _wrap_close(proc.stdout, proc)
|
||||||
else:
|
else:
|
||||||
proc = subprocess.Popen(cmd,
|
proc = subprocess.Popen(cmd,
|
||||||
shell=True,
|
shell=True, text=True,
|
||||||
stdin=subprocess.PIPE,
|
stdin=subprocess.PIPE,
|
||||||
bufsize=buffering)
|
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
|
# Helper for popen() -- a proxy for a file whose close waits for the process
|
||||||
class _wrap_close:
|
class _wrap_close:
|
||||||
|
|
|
@ -269,7 +269,7 @@ def test_write_windows_console(self):
|
||||||
|
|
||||||
def fdopen_helper(self, *args):
|
def fdopen_helper(self, *args):
|
||||||
fd = os.open(os_helper.TESTFN, os.O_RDONLY)
|
fd = os.open(os_helper.TESTFN, os.O_RDONLY)
|
||||||
f = os.fdopen(fd, *args)
|
f = os.fdopen(fd, *args, encoding="utf-8")
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def test_fdopen(self):
|
def test_fdopen(self):
|
||||||
|
@ -290,7 +290,7 @@ def test_replace(self):
|
||||||
|
|
||||||
os.replace(os_helper.TESTFN, TESTFN2)
|
os.replace(os_helper.TESTFN, TESTFN2)
|
||||||
self.assertRaises(FileNotFoundError, os.stat, os_helper.TESTFN)
|
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")
|
self.assertEqual(f.read(), "1")
|
||||||
|
|
||||||
def test_open_keywords(self):
|
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):
|
def test_exist_ok_existing_regular_file(self):
|
||||||
base = os_helper.TESTFN
|
base = os_helper.TESTFN
|
||||||
path = os.path.join(os_helper.TESTFN, 'dir1')
|
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')
|
f.write('abc')
|
||||||
self.assertRaises(OSError, os.makedirs, path)
|
self.assertRaises(OSError, os.makedirs, path)
|
||||||
self.assertRaises(OSError, os.makedirs, path, exist_ok=False)
|
self.assertRaises(OSError, os.makedirs, path, exist_ok=False)
|
||||||
|
@ -2094,7 +2094,7 @@ def test_chmod(self):
|
||||||
|
|
||||||
|
|
||||||
class TestInvalidFD(unittest.TestCase):
|
class TestInvalidFD(unittest.TestCase):
|
||||||
singles = ["fchdir", "dup", "fdopen", "fdatasync", "fstat",
|
singles = ["fchdir", "dup", "fdatasync", "fstat",
|
||||||
"fstatvfs", "fsync", "tcgetpgrp", "ttyname"]
|
"fstatvfs", "fsync", "tcgetpgrp", "ttyname"]
|
||||||
#singles.append("close")
|
#singles.append("close")
|
||||||
#We omit close because it doesn't raise an exception on some platforms
|
#We omit close because it doesn't raise an exception on some platforms
|
||||||
|
@ -2106,15 +2106,18 @@ def helper(self):
|
||||||
for f in singles:
|
for f in singles:
|
||||||
locals()["test_"+f] = get_single(f)
|
locals()["test_"+f] = get_single(f)
|
||||||
|
|
||||||
def check(self, f, *args):
|
def check(self, f, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
f(os_helper.make_bad_fd(), *args)
|
f(os_helper.make_bad_fd(), *args, **kwargs)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
self.assertEqual(e.errno, errno.EBADF)
|
self.assertEqual(e.errno, errno.EBADF)
|
||||||
else:
|
else:
|
||||||
self.fail("%r didn't raise an OSError with a bad file descriptor"
|
self.fail("%r didn't raise an OSError with a bad file descriptor"
|
||||||
% f)
|
% f)
|
||||||
|
|
||||||
|
def test_fdopen(self):
|
||||||
|
self.check(os.fdopen, encoding="utf-8")
|
||||||
|
|
||||||
@unittest.skipUnless(hasattr(os, 'isatty'), 'test needs os.isatty()')
|
@unittest.skipUnless(hasattr(os, 'isatty'), 'test needs os.isatty()')
|
||||||
def test_isatty(self):
|
def test_isatty(self):
|
||||||
self.assertEqual(os.isatty(os_helper.make_bad_fd()), False)
|
self.assertEqual(os.isatty(os_helper.make_bad_fd()), False)
|
||||||
|
@ -2210,7 +2213,7 @@ def _test_link(self, file1, file2):
|
||||||
os.link(file1, file2)
|
os.link(file1, file2)
|
||||||
except PermissionError as e:
|
except PermissionError as e:
|
||||||
self.skipTest('os.link(): %s' % 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()))
|
self.assertTrue(os.path.sameopenfile(f1.fileno(), f2.fileno()))
|
||||||
|
|
||||||
def test_link(self):
|
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)'
|
code = ('import sys, os; magic = os.environ[%r]; sys.exit(%s)'
|
||||||
% (self.key, self.exitcode))
|
% (self.key, self.exitcode))
|
||||||
|
|
||||||
with open(filename, "w") as fp:
|
with open(filename, "w", encoding="utf-8") as fp:
|
||||||
fp.write(code)
|
fp.write(code)
|
||||||
|
|
||||||
args = [sys.executable, filename]
|
args = [sys.executable, filename]
|
||||||
|
@ -3149,7 +3152,7 @@ def _test_invalid_env(self, spawn):
|
||||||
# equal character in the environment variable value
|
# equal character in the environment variable value
|
||||||
filename = os_helper.TESTFN
|
filename = os_helper.TESTFN
|
||||||
self.addCleanup(os_helper.unlink, filename)
|
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'
|
fp.write('import sys, os\n'
|
||||||
'if os.getenv("FRUIT") != "orange=lemon":\n'
|
'if os.getenv("FRUIT") != "orange=lemon":\n'
|
||||||
' raise AssertionError')
|
' raise AssertionError')
|
||||||
|
|
Loading…
Reference in New Issue