bpo-43651: PEP 597: Fix `socket.makefile()` (GH-25645)

This commit is contained in:
Inada Naoki 2021-04-27 13:16:28 +09:00 committed by GitHub
parent 743e2bae10
commit cfe523b492
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 3 deletions

View File

@ -337,6 +337,7 @@ def makefile(self, mode="r", buffering=None, *,
buffer = io.BufferedWriter(raw, buffering) buffer = io.BufferedWriter(raw, buffering)
if binary: if binary:
return buffer return buffer
encoding = io.text_encoding(encoding)
text = io.TextIOWrapper(buffer, encoding, errors, newline) text = io.TextIOWrapper(buffer, encoding, errors, newline)
text.mode = mode text.mode = mode
return text return text

View File

@ -1678,7 +1678,8 @@ def test_makefile_mode(self):
for mode in 'r', 'rb', 'rw', 'w', 'wb': for mode in 'r', 'rb', 'rw', 'w', 'wb':
with self.subTest(mode=mode): with self.subTest(mode=mode):
with socket.socket() as sock: with socket.socket() as sock:
with sock.makefile(mode) as fp: encoding = None if "b" in mode else "utf-8"
with sock.makefile(mode, encoding=encoding) as fp:
self.assertEqual(fp.mode, mode) self.assertEqual(fp.mode, mode)
def test_makefile_invalid_mode(self): def test_makefile_invalid_mode(self):
@ -5625,7 +5626,7 @@ def isTipcAvailable():
if not hasattr(socket, "AF_TIPC"): if not hasattr(socket, "AF_TIPC"):
return False return False
try: try:
f = open("/proc/modules") f = open("/proc/modules", encoding="utf-8")
except (FileNotFoundError, IsADirectoryError, PermissionError): except (FileNotFoundError, IsADirectoryError, PermissionError):
# It's ok if the file does not exist, is a directory or if we # It's ok if the file does not exist, is a directory or if we
# have not the permission to read it. # have not the permission to read it.
@ -6222,7 +6223,7 @@ def test_errors(self):
meth = self.meth_from_sock(s) meth = self.meth_from_sock(s)
self.assertRaisesRegex( self.assertRaisesRegex(
ValueError, "SOCK_STREAM", meth, file) ValueError, "SOCK_STREAM", meth, file)
with open(os_helper.TESTFN, 'rt') as file: with open(os_helper.TESTFN, encoding="utf-8") as file:
with socket.socket() as s: with socket.socket() as s:
meth = self.meth_from_sock(s) meth = self.meth_from_sock(s)
self.assertRaisesRegex( self.assertRaisesRegex(