Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from test.support.import_helper import ensure_lazy_imports
import _thread as thread
import array
import collections
import contextlib
import errno
import gc
Expand Down Expand Up @@ -88,6 +89,7 @@ def wrapper(*args, **kwds):

return decorator


def get_cid():
if fcntl is None:
return None
Expand All @@ -101,6 +103,7 @@ def get_cid():
else:
return struct.unpack("I", r)[0]


def _have_socket_can():
"""Check whether CAN sockets are supported on this host."""
try:
Expand All @@ -111,6 +114,7 @@ def _have_socket_can():
s.close()
return True


def _have_socket_can_isotp():
"""Check whether CAN ISOTP sockets are supported on this host."""
try:
Expand All @@ -121,6 +125,7 @@ def _have_socket_can_isotp():
s.close()
return True


def _have_socket_can_j1939():
"""Check whether CAN J1939 sockets are supported on this host."""
try:
Expand All @@ -131,6 +136,7 @@ def _have_socket_can_j1939():
s.close()
return True


def _have_socket_rds():
"""Check whether RDS sockets are supported on this host."""
try:
Expand All @@ -141,6 +147,7 @@ def _have_socket_rds():
s.close()
return True


def _have_socket_alg():
"""Check whether AF_ALG sockets are supported on this host."""
try:
Expand All @@ -151,6 +158,7 @@ def _have_socket_alg():
s.close()
return True


def _have_socket_qipcrtr():
"""Check whether AF_QIPCRTR sockets are supported on this host."""
try:
Expand All @@ -161,6 +169,7 @@ def _have_socket_qipcrtr():
s.close()
return True


def _have_socket_vsock():
"""Check whether AF_VSOCK sockets are supported on this host."""
cid = get_cid()
Expand Down Expand Up @@ -202,6 +211,28 @@ def _have_socket_hyperv():
return True


def _find_service(expected_protocols,
services_file='/etc/services'):
if not os.path.exists(services_file):
return None
expected_protocols = set(expected_protocols)
services = collections.defaultdict(set)
with open(services_file, 'r') as f:
for line in map(str.strip, f):
if line.startswith('#'):
continue
tokens = line.split()
if len(tokens) < 2 or '/' not in tokens[1]:
continue
service_name = tokens[0]
_, service_protocol = tokens[1].split('/', maxsplit=1)
service_protocols = services[service_name]
service_protocols.add(service_protocol)
if service_protocols <= expected_protocols:
return service_name
return None


@contextlib.contextmanager
def socket_setdefaulttimeout(timeout):
old_timeout = socket.getdefaulttimeout()
Expand Down Expand Up @@ -1276,7 +1307,10 @@ def testGetServBy(self):
except OSError:
pass
else:
raise OSError
service = _find_service(['tcp'])
if service is None:
self.skipTest('No available TCP service found.')
port = socket.getservbyname(service, 'tcp')
# Try same call with optional protocol omitted
# Issue gh-71123: this fails on Android before API level 23.
if not (support.is_android and platform.android_ver().api_level < 23):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``testGetServBy`` in ``test_socket.py`` on systems without certain entries in ``/etc/services``.
Loading