Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6e622d

Browse files
committed
Fix test.support.bind_port() to not cause an error when Python was compiled
on a system with SO_REUSEPORT defined in the headers but run on a system with an OS kernel that does not support that reasonably new socket option.
2 parents 61b976f + 162307f commit b6e622d

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

Lib/test/support/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,15 @@ def bind_port(sock, host=HOST):
594594
raise TestFailed("tests should never set the SO_REUSEADDR " \
595595
"socket option on TCP/IP sockets!")
596596
if hasattr(socket, 'SO_REUSEPORT'):
597-
if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 1:
598-
raise TestFailed("tests should never set the SO_REUSEPORT " \
599-
"socket option on TCP/IP sockets!")
597+
try:
598+
if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 1:
599+
raise TestFailed("tests should never set the SO_REUSEPORT " \
600+
"socket option on TCP/IP sockets!")
601+
except OSError:
602+
# Python's socket module was compiled using modern headers
603+
# thus defining SO_REUSEPORT but this process is running
604+
# under an older kernel that does not support SO_REUSEPORT.
605+
pass
600606
if hasattr(socket, 'SO_EXCLUSIVEADDRUSE'):
601607
sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
602608

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ Core and Builtins
5050
Library
5151
-------
5252

53+
- Fix test.support.bind_port() to not cause an error when Python was compiled
54+
on a system with SO_REUSEPORT defined in the headers but run on a system
55+
with an OS kernel that does not support that reasonably new socket option.
56+
5357
- Fix compilation error under gcc of the ctypes module bundled libffi for arm.
5458

5559
- Issue #19448: Add private API to SSL module to lookup ASN.1 objects by OID,

0 commit comments

Comments
 (0)