Skip to content

Commit 3bbc118

Browse files
committed
pythongh-149078: test_socket: verify runtime kernel support for UDPLite
The test suite previously only checked for the existence of the socket.IPPROTO_UDPLITE constant. This caused test failures on systems where the constant is defined in headers (and thus present in the interpreter) but the protocol is not supported or enabled in the running kernel (e.g., restricted build environments). This change adds a _have_socket_udplite() helper that performs a runtime check by attempting to create a UDPLite socket, aligning its behavior with other optional protocols like RDS or AF_ALG.
1 parent 22d53a9 commit 3bbc118

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

Lib/test/test_socket.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,22 @@ def _have_socket_vsock():
168168
return (cid is not None)
169169

170170

171+
def _have_socket_udplite():
172+
"""Check whether UDPLITE sockets are supported on this host."""
173+
if not hasattr(socket, "IPPROTO_UDPLITE"):
174+
return False
175+
# Older Android versions block UDPLITE with SELinux.
176+
if support.is_android and platform.android_ver().api_level < 29:
177+
return False
178+
try:
179+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDPLITE)
180+
except (AttributeError, OSError):
181+
return False
182+
else:
183+
s.close()
184+
return True
185+
186+
171187
def _have_socket_bluetooth():
172188
"""Check whether AF_BLUETOOTH sockets are supported on this host."""
173189
try:
@@ -245,10 +261,7 @@ def downgrade_malformed_data_warning():
245261

246262
HAVE_SOCKET_VSOCK = _have_socket_vsock()
247263

248-
# Older Android versions block UDPLITE with SELinux.
249-
HAVE_SOCKET_UDPLITE = (
250-
hasattr(socket, "IPPROTO_UDPLITE")
251-
and not (support.is_android and platform.android_ver().api_level < 29))
264+
HAVE_SOCKET_UDPLITE = _have_socket_udplite()
252265

253266
HAVE_SOCKET_BLUETOOTH = _have_socket_bluetooth()
254267

0 commit comments

Comments
 (0)