]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43651: PEP 597: Fix `socket.makefile()` (GH-25645)
authorInada Naoki <songofacandy@gmail.com>
Tue, 27 Apr 2021 04:16:28 +0000 (13:16 +0900)
committerGitHub <noreply@github.com>
Tue, 27 Apr 2021 04:16:28 +0000 (13:16 +0900)
Lib/socket.py
Lib/test/test_socket.py

index 5276cc8ba3d6197bb9b8849a1dcf2c944052a4c4..fc11eb783c3dd104d1ceaecc7d04c3cb301b7b83 100755 (executable)
@@ -337,6 +337,7 @@ class socket(_socket.socket):
             buffer = io.BufferedWriter(raw, buffering)
         if binary:
             return buffer
+        encoding = io.text_encoding(encoding)
         text = io.TextIOWrapper(buffer, encoding, errors, newline)
         text.mode = mode
         return text
index 43a1d5bdcf5361fc341d98fad934bb73631db3e5..3c45278748a94109cde04f40a11a7d5743b31e9c 100755 (executable)
@@ -1678,7 +1678,8 @@ class GeneralModuleTests(unittest.TestCase):
         for mode in 'r', 'rb', 'rw', 'w', 'wb':
             with self.subTest(mode=mode):
                 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)
 
     def test_makefile_invalid_mode(self):
@@ -5625,7 +5626,7 @@ def isTipcAvailable():
     if not hasattr(socket, "AF_TIPC"):
         return False
     try:
-        f = open("/proc/modules")
+        f = open("/proc/modules", encoding="utf-8")
     except (FileNotFoundError, IsADirectoryError, PermissionError):
         # It's ok if the file does not exist, is a directory or if we
         # have not the permission to read it.
@@ -6222,7 +6223,7 @@ class SendfileUsingSendTest(ThreadedTCPSocketTest):
                 meth = self.meth_from_sock(s)
                 self.assertRaisesRegex(
                     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:
                 meth = self.meth_from_sock(s)
                 self.assertRaisesRegex(