[Python-checkins] bpo-43651: PEP 597: Fix `socket.makefile()` (GH-25645)

methane webhook-mailer at python.org
Tue Apr 27 00:16:32 EDT 2021


https://github.com/python/cpython/commit/cfe523b49280cdc8c239c807121ad3f33552f638
commit: cfe523b49280cdc8c239c807121ad3f33552f638
branch: master
author: Inada Naoki <songofacandy at gmail.com>
committer: methane <songofacandy at gmail.com>
date: 2021-04-27T13:16:28+09:00
summary:

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

files:
M Lib/socket.py
M Lib/test/test_socket.py

diff --git a/Lib/socket.py b/Lib/socket.py
index 5276cc8ba3d61..fc11eb783c3dd 100755
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -337,6 +337,7 @@ def makefile(self, mode="r", buffering=None, *,
             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
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 43a1d5bdcf536..3c45278748a94 100755
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -1678,7 +1678,8 @@ def test_makefile_mode(self):
         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 @@ def test_errors(self):
                 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(



More information about the Python-checkins mailing list