Skip to content

Commit ff7af22

Browse files
bpo-34932: Add socket.TCP_KEEPALIVE for macOS (GH-25079)
(cherry picked from commit d59d737) Co-authored-by: Shane Harvey <[email protected]>
1 parent 02e4c0c commit ff7af22

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

Doc/library/socket.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ Constants
381381

382382
.. versionchanged:: 3.10
383383
``IP_RECVTOS`` was added.
384+
Added ``TCP_KEEPALIVE``. On MacOS this constant can be used in the same
385+
way that ``TCP_KEEPIDLE`` is used on Linux.
384386

385387
.. data:: AF_CAN
386388
PF_CAN

Lib/test/test_socket.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6411,6 +6411,12 @@ def test_length_restriction(self):
64116411
sock.bind(("type", "n" * 64))
64126412

64136413

6414+
@unittest.skipUnless(sys.platform == 'darwin', 'macOS specific test')
6415+
class TestMacOSTCPFlags(unittest.TestCase):
6416+
def test_tcp_keepalive(self):
6417+
self.assertTrue(socket.TCP_KEEPALIVE)
6418+
6419+
64146420
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
64156421
class TestMSWindowsTCPFlags(unittest.TestCase):
64166422
knownTCPFlags = {
@@ -6669,6 +6675,7 @@ def test_main():
66696675
SendfileUsingSendfileTest,
66706676
])
66716677
tests.append(TestMSWindowsTCPFlags)
6678+
tests.append(TestMacOSTCPFlags)
66726679

66736680
thread_info = threading_helper.threading_setup()
66746681
support.run_unittest(*tests)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add socket.TCP_KEEPALIVE support for macOS. Patch by Shane Harvey.

Modules/socketmodule.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8158,6 +8158,10 @@ PyInit__socket(void)
81588158
#endif
81598159
#ifdef TCP_KEEPIDLE
81608160
PyModule_AddIntMacro(m, TCP_KEEPIDLE);
8161+
#endif
8162+
/* TCP_KEEPALIVE is OSX's TCP_KEEPIDLE equivalent */
8163+
#if defined(__APPLE__) && defined(TCP_KEEPALIVE)
8164+
PyModule_AddIntMacro(m, TCP_KEEPALIVE);
81618165
#endif
81628166
#ifdef TCP_KEEPINTVL
81638167
PyModule_AddIntMacro(m, TCP_KEEPINTVL);

0 commit comments

Comments
 (0)