Skip to content

Commit 54b964b

Browse files
committed
Add first_line() function
1 parent f60c855 commit 54b964b

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

‎Lib/test/pythoninfo.py‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ def normalize_text(text):
2222
return text.strip()
2323

2424

25+
def first_line(text):
26+
# Get the first line. Return text unchanged if it's empty.
27+
lines = text.splitlines()
28+
if lines:
29+
return lines[0]
30+
else:
31+
# text is an empty string
32+
return text
33+
34+
2535
def read_first_line(filename):
2636
# Get the first line of a text file and strip trailing spaces
2737
try:
@@ -481,12 +491,9 @@ def run_command(cmd, check=True, **kwargs):
481491

482492
def collect_gdb(info_add):
483493
version = run_command(["gdb", "-nx", "--version"])
484-
if not version:
485-
return
486-
487-
# Only keep the first line
488-
version = version.splitlines()[0]
489-
info_add('gdb_version', version)
494+
if version:
495+
# Only keep the first line
496+
info_add('gdb_version', first_line(version))
490497

491498

492499
def collect_tkinter(info_add):
@@ -904,7 +911,7 @@ def collect_cc(info_add):
904911
# CC --version failed: ignore error
905912
return
906913

907-
text = stdout.splitlines()[0]
914+
text = first_line(stdout)
908915
text = normalize_text(text)
909916
info_add('CC.version', text)
910917

@@ -1027,9 +1034,7 @@ def collect_windows(info_add):
10271034
# "ver" output starts with an empty line: remove it
10281035
output = output.strip()
10291036
if output:
1030-
first_line = output.splitlines()[0]
1031-
if first_line:
1032-
info_add('windows.ver', first_line)
1037+
info_add('windows.ver', first_line(output))
10331038

10341039
# windows.developer_mode: get AllowDevelopmentWithoutDevLicense registry
10351040
value = winreg_query(r"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows"

0 commit comments

Comments
 (0)