Skip to content

Commit d59d737

Browse files
authored
bpo-34932: Add socket.TCP_KEEPALIVE for macOS (GH-25079)
1 parent 2b47af6 commit d59d737

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
@@ -6446,6 +6446,12 @@ def test_length_restriction(self):
64466446
sock.bind(("type", "n" * 64))
64476447

64486448

6449+
@unittest.skipUnless(sys.platform == 'darwin', 'macOS specific test')
6450+
class TestMacOSTCPFlags(unittest.TestCase):
6451+
def test_tcp_keepalive(self):
6452+
self.assertTrue(socket.TCP_KEEPALIVE)
6453+
6454+
64496455
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
64506456
class TestMSWindowsTCPFlags(unittest.TestCase):
64516457
knownTCPFlags = {
@@ -6704,6 +6710,7 @@ def test_main():
67046710
SendfileUsingSendfileTest,
67056711
])
67066712
tests.append(TestMSWindowsTCPFlags)
6713+
tests.append(TestMacOSTCPFlags)
67076714

67086715
thread_info = threading_helper.threading_setup()
67096716
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)