Skip to content

Commit 78a5359

Browse files
committed
gh-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 005555a commit 78a5359

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
@@ -170,6 +170,22 @@ def _have_socket_vsock():
170170
return (cid is not None)
171171

172172

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

248264
HAVE_SOCKET_VSOCK = _have_socket_vsock()
249265

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

255268
HAVE_SOCKET_BLUETOOTH = _have_socket_bluetooth()
256269

0 commit comments

Comments
 (0)