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

Skip to content

Commit 162307f

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.
1 parent 7929a1d commit 162307f

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
@@ -570,9 +570,15 @@ def bind_port(sock, host=HOST):
570570
raise TestFailed("tests should never set the SO_REUSEADDR " \
571571
"socket option on TCP/IP sockets!")
572572
if hasattr(socket, 'SO_REUSEPORT'):
573-
if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 1:
574-
raise TestFailed("tests should never set the SO_REUSEPORT " \
575-
"socket option on TCP/IP sockets!")
573+
try:
574+
if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 1:
575+
raise TestFailed("tests should never set the SO_REUSEPORT " \
576+
"socket option on TCP/IP sockets!")
577+
except OSError:
578+
# Python's socket module was compiled using modern headers
579+
# thus defining SO_REUSEPORT but this process is running
580+
# under an older kernel that does not support SO_REUSEPORT.
581+
pass
576582
if hasattr(socket, 'SO_EXCLUSIVEADDRUSE'):
577583
sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
578584

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- Fix test.support.bind_port() to not cause an error when Python was compiled
17+
on a system with SO_REUSEPORT defined in the headers but run on a system
18+
with an OS kernel that does not support that reasonably new socket option.
19+
1620
- Fix compilation error under gcc of the ctypes module bundled libffi for arm.
1721

1822
- Issue #19523: Closed FileHandler leak which occurred when delay was set.

0 commit comments

Comments
 (0)