Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 12 additions & 9 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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')
Expand Down