[Python-checkins] bpo-34932: Add socket.TCP_KEEPALIVE for macOS (GH-25079)

miss-islington webhook-mailer at python.org
Wed Jul 14 19:15:40 EDT 2021


https://github.com/python/cpython/commit/ff7af2203c1f03d9300e93e3e06a47fb78cc2bef
commit: ff7af2203c1f03d9300e93e3e06a47fb78cc2bef
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-07-14T16:15:31-07:00
summary:

bpo-34932: Add socket.TCP_KEEPALIVE for macOS (GH-25079)

(cherry picked from commit d59d7374a364c4e5c2b9a83d8e4543ee494285b8)

Co-authored-by: Shane Harvey <shnhrv at gmail.com>

files:
A Misc/NEWS.d/next/macOS/2021-03-29-21-11-23.bpo-34932.f3Hdyd.rst
M Doc/library/socket.rst
M Lib/test/test_socket.py
M Modules/socketmodule.c

diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst
index ddda4d2de947a2..eb3090c83ef636 100755
--- a/Doc/library/socket.rst
+++ b/Doc/library/socket.rst
@@ -381,6 +381,8 @@ Constants
 
    .. versionchanged:: 3.10
       ``IP_RECVTOS`` was added.
+       Added ``TCP_KEEPALIVE``. On MacOS this constant can be used in the same
+       way that ``TCP_KEEPIDLE`` is used on Linux.
 
 .. data:: AF_CAN
           PF_CAN
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index a59afd414e73da..12af22fa3ca916 100755
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -6411,6 +6411,12 @@ def test_length_restriction(self):
             sock.bind(("type", "n" * 64))
 
 
+ at unittest.skipUnless(sys.platform == 'darwin', 'macOS specific test')
+class TestMacOSTCPFlags(unittest.TestCase):
+    def test_tcp_keepalive(self):
+        self.assertTrue(socket.TCP_KEEPALIVE)
+
+
 @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
 class TestMSWindowsTCPFlags(unittest.TestCase):
     knownTCPFlags = {
@@ -6669,6 +6675,7 @@ def test_main():
         SendfileUsingSendfileTest,
     ])
     tests.append(TestMSWindowsTCPFlags)
+    tests.append(TestMacOSTCPFlags)
 
     thread_info = threading_helper.threading_setup()
     support.run_unittest(*tests)
diff --git a/Misc/NEWS.d/next/macOS/2021-03-29-21-11-23.bpo-34932.f3Hdyd.rst b/Misc/NEWS.d/next/macOS/2021-03-29-21-11-23.bpo-34932.f3Hdyd.rst
new file mode 100644
index 00000000000000..d3a52c9fc37b5f
--- /dev/null
+++ b/Misc/NEWS.d/next/macOS/2021-03-29-21-11-23.bpo-34932.f3Hdyd.rst
@@ -0,0 +1 @@
+Add socket.TCP_KEEPALIVE support for macOS. Patch by Shane Harvey.
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 142cc7c8a7484e..9233430667c2fe 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -8158,6 +8158,10 @@ PyInit__socket(void)
 #endif
 #ifdef  TCP_KEEPIDLE
     PyModule_AddIntMacro(m, TCP_KEEPIDLE);
+#endif
+    /* TCP_KEEPALIVE is OSX's TCP_KEEPIDLE equivalent */
+#if defined(__APPLE__) && defined(TCP_KEEPALIVE)
+    PyModule_AddIntMacro(m, TCP_KEEPALIVE);
 #endif
 #ifdef  TCP_KEEPINTVL
     PyModule_AddIntMacro(m, TCP_KEEPINTVL);



More information about the Python-checkins mailing list