Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
35 changes: 34 additions & 1 deletion Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,36 @@ def _have_socket_hyperv():
s.close()
return True

def _query_available_service(expected_protocols, services_file = '/etc/services'):
if not os.path.exists(services_file):
return None
services_found = dict()
with open(services_file, 'r') as f:
for line in f:
line = line.strip()
if line.startswith('#'):
# Skip comment line.
continue
tokens = line.split()
if len(tokens) < 2:
continue
if '/' not in tokens[1]:
continue
try:
_, entry_protocol = tokens[1].split('/')
except:
continue
entry_name = tokens[0]
if entry_name not in services_found:
services_found[entry_name] = [entry_protocol]
else:
services_found[entry_name].append(entry_protocol)
for protocol in expected_protocols:
if protocol not in services_found[entry_name]:
break
else:
return entry_name
return None

@contextlib.contextmanager
def socket_setdefaulttimeout(timeout):
Expand Down Expand Up @@ -1276,7 +1306,10 @@ def testGetServBy(self):
except OSError:
pass
else:
raise OSError
service = _query_available_service(('tcp'))
if service is None:
self.skipTest('No available 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