Skip to content

Commit 6c1c7ad

Browse files
ggqlqpicnixz
andauthored
Update Lib/test/test_socket.py
Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 4cac906 commit 6c1c7ad

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

Lib/test/test_socket.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -206,32 +206,21 @@ def _find_service(expected_protocols,
206206
services_file = '/etc/services'):
207207
if not os.path.exists(services_file):
208208
return None
209-
services_found = dict()
209+
expected_protocols = set(expected_protocols)
210+
services = collections.defaultdict(set)
210211
with open(services_file, 'r') as f:
211-
for line in f:
212-
line = line.strip()
212+
for line in map(str.strip, f):
213213
if line.startswith('#'):
214-
# Skip comment line.
215214
continue
216215
tokens = line.split()
217-
if len(tokens) < 2:
218-
continue
219-
if '/' not in tokens[1]:
220-
continue
221-
try:
222-
_, entry_protocol = tokens[1].split('/')
223-
except:
216+
if len(tokens) < 2 or '/' not in tokens[1]:
224217
continue
225-
entry_name = tokens[0]
226-
if entry_name not in services_found:
227-
services_found[entry_name] = [entry_protocol]
228-
else:
229-
services_found[entry_name].append(entry_protocol)
230-
for protocol in expected_protocols:
231-
if protocol not in services_found[entry_name]:
232-
break
233-
else:
234-
return entry_name
218+
service_name = tokens[0]
219+
_, service_protocol = tokens[1].split('/', maxsplit=1)
220+
service_protocols = services[service_name]
221+
service_protocols.add(service_protocol)
222+
if service_protocols <= expected_protocols:
223+
return service_name
235224
return None
236225

237226
@contextlib.contextmanager

0 commit comments

Comments
 (0)