Skip to content

Commit 469c5e7

Browse files
authored
some subprocess.Popen() related Python cleanups / added TODOs about missing returncode usage (danmar#7065)
1 parent b194c6f commit 469c5e7

15 files changed

+81
-55
lines changed

addons/cppcheckdata.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,8 +1714,9 @@ def get_path_premium_addon():
17141714

17151715
def cmd_output(cmd):
17161716
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
1717-
comm = p.communicate()
1718-
out = comm[0]
1719-
if p.returncode == 1 and len(comm[1]) > 2:
1720-
out = comm[1]
1721-
return out.decode(encoding='utf-8', errors='ignore')
1717+
stdout, stderr = p.communicate()
1718+
rc = p.returncode
1719+
out = stdout
1720+
if rc == 1 and len(stderr) > 2:
1721+
out = stderr
1722+
return out.decode(encoding='utf-8', errors='ignore')

test/cli/clang-import_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from testutils import cppcheck, assert_cppcheck
1010

1111
try:
12+
# TODO: handle exitcode?
1213
subprocess.call(['clang', '--version'])
1314
except OSError:
1415
pytest.skip("'clang' does not exist", allow_module_level=True)

test/cli/testutils.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def __run_subprocess(args, env=None, cwd=None, timeout=None):
119119
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, cwd=cwd)
120120

121121
try:
122-
comm = p.communicate(timeout=timeout)
122+
stdout, stderr = p.communicate(timeout=timeout)
123123
return_code = p.returncode
124124
p = None
125125
except subprocess.TimeoutExpired:
@@ -141,10 +141,9 @@ def __run_subprocess(args, env=None, cwd=None, timeout=None):
141141
# sending the signal to the process groups causes the parent Python process to terminate as well
142142
#os.killpg(os.getpgid(p.pid), signal.SIGTERM) # Send the signal to all the process groups
143143
p.terminate()
144-
comm = p.communicate()
144+
stdout, stderr = p.communicate()
145+
p = None
145146

146-
stdout = comm[0]
147-
stderr = comm[1]
148147
return return_code, stdout, stderr
149148

150149

test/signal/test-signalhandler.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ def __call_process(arg):
2424
if exe is None:
2525
raise Exception('executable not found')
2626
with subprocess.Popen([exe, arg], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
27-
comm = p.communicate()
28-
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
29-
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
30-
return p.returncode, stdout, stderr
27+
stdout, stderr = p.communicate()
28+
rc = p.returncode
29+
stdout = stdout.decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
30+
stderr = stderr.decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
31+
return rc, stdout, stderr
3132

3233

3334
def test_assert():

test/signal/test-stacktrace.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ def __call_process():
2323
if exe is None:
2424
raise Exception('executable not found')
2525
with subprocess.Popen([exe], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
26-
comm = p.communicate()
27-
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
28-
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
29-
return p.returncode, stdout, stderr
26+
stdout, stderr = p.communicate()
27+
rc = p.returncode
28+
stdout = stdout.decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
29+
stderr = stderr.decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
30+
return rc, stdout, stderr
3031

3132

3233
def test_stack():

tools/bisect/bisect_res.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ def run(cppcheck_path, options):
1010
print('running {}'.format(cppcheck_path))
1111
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) as p:
1212
stdout, stderr = p.communicate()
13-
# only 0 and 1 are well-defined in this case
14-
if p.returncode > 1:
15-
print('error')
16-
return None, None, None
17-
# signals are reported as negative exitcode (e.g. SIGSEGV -> -11)
18-
if p.returncode < 0:
19-
print('crash')
20-
return p.returncode, stderr, stdout
21-
print('done')
22-
return p.returncode, stderr, stdout
13+
rc = p.returncode
14+
# only 0 and 1 are well-defined in this case
15+
if rc > 1:
16+
print('error')
17+
return None, None, None
18+
# signals are reported as negative exitcode (e.g. SIGSEGV -> -11)
19+
if rc < 0:
20+
print('crash')
21+
return rc, stderr, stdout
22+
print('done')
23+
return rc, stderr, stdout
2324

2425

2526
# TODO: check arguments

tools/ci.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ def gitpush():
4141
def iconv(filename):
4242
with subprocess.Popen(['file', '-i', filename],
4343
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as p:
44-
comm = p.communicate()
45-
if 'charset=iso-8859-1' in comm[0]:
44+
# TODO: handle p.returncode?
45+
stdout, _ = p.communicate()
46+
if 'charset=iso-8859-1' in stdout:
47+
# TODO: handle exitcode?
4648
subprocess.call(
4749
["iconv", filename, "--from=ISO-8859-1", "--to=UTF-8", "-o", filename])
4850

@@ -51,14 +53,19 @@ def iconv(filename):
5153
def generate_webreport():
5254
for filename in glob.glob('*/*.cpp'):
5355
iconv(filename)
56+
# TODO: handle exitcode?
5457
subprocess.call(
5558
["git", "commit", "-a", "-m", '"automatic conversion from iso-8859-1 formatting to utf-8"'])
5659
gitpush()
5760

61+
# TODO: handle exitcode?
5862
subprocess.call(["rm", "-rf", "devinfo"])
63+
# TODO: handle exitcode?
5964
subprocess.call(['nice', "./webreport.sh"])
6065
upload('-r devinfo', 'htdocs/')
66+
# TODO: handle exitcode?
6167
subprocess.call(["make", "clean"])
68+
# TODO: handle exitcode?
6269
subprocess.call(["rm", "-rf", "devinfo"])
6370

6471

tools/compare_ast_symdb.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ def run_cppcheck(cppcheck_parameters:str, clang:str):
1515
cmd = '{} {} {} --debug --verbose'.format(CPPCHECK, cppcheck_parameters, clang)
1616
#print(cmd)
1717
with subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
18-
comm = p.communicate()
19-
return comm[0].decode('utf-8', 'ignore')
18+
# TODO: handle p.returncode?
19+
stdout, _ = p.communicate()
20+
return stdout.decode('utf-8', 'ignore')
2021

2122

2223
def get_ast(debug):

tools/daca2-download.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def wget(filepath):
2323
if '/' in filepath:
2424
filename = filename[filename.rfind('/') + 1:]
2525
for d in DEBIAN:
26+
# TODO: handle exitcode?
2627
subprocess.call(
2728
['nice', 'wget', '--tries=10', '--timeout=300', '-O', filename, d + filepath])
2829
if os.path.isfile(filename):
@@ -40,9 +41,11 @@ def latestvername(names):
4041
def getpackages():
4142
if not wget('ls-lR.gz'):
4243
return []
44+
# TODO: handle exitcode?
4345
subprocess.call(['nice', 'gunzip', 'ls-lR.gz'])
4446
with open('ls-lR', 'rt') as f:
4547
lines = f.readlines()
48+
# TODO: handle exitcode?
4649
subprocess.call(['rm', 'ls-lR'])
4750

4851
path = None
@@ -141,10 +144,13 @@ def downloadpackage(filepath, outpath):
141144

142145
filename = filepath[filepath.rfind('/') + 1:]
143146
if filename[-3:] == '.gz':
147+
# TODO: handle exitcode?
144148
subprocess.call(['tar', 'xzvf', filename])
145149
elif filename[-3:] == '.xz':
150+
# TODO: handle exitcode?
146151
subprocess.call(['tar', 'xJvf', filename])
147152
elif filename[-4:] == '.bz2':
153+
# TODO: handle exitcode?
148154
subprocess.call(['tar', 'xjvf', filename])
149155
else:
150156
return
@@ -153,6 +159,7 @@ def downloadpackage(filepath, outpath):
153159

154160
for g in glob.glob('[#_A-Za-z0-9]*'):
155161
if os.path.isdir(g):
162+
# TODO: handle exitcode?
156163
subprocess.call(['tar', '-cJvf', outpath + filename[:filename.rfind('.')] + '.xz', g])
157164
break
158165

tools/daca2-getpackages.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def wget(filepath):
2424
if '/' in filepath:
2525
filename = filename[filename.rfind('/') + 1:]
2626
for d in DEBIAN:
27+
# TODO: handle exitcode?
2728
subprocess.call(
2829
['nice', 'wget', '--tries=10', '--timeout=300', '-O', filename, d + filepath])
2930
if os.path.isfile(filename):
@@ -41,11 +42,13 @@ def latestvername(names):
4142
def getpackages():
4243
if not wget('ls-lR.gz'):
4344
sys.exit(1)
45+
# TODO: handle exitcode?
4446
subprocess.call(['nice', 'gunzip', 'ls-lR.gz'])
4547
if not os.path.isfile('ls-lR'):
4648
sys.exit(1)
4749
with open('ls-lR', 'rt') as f:
4850
lines = f.readlines()
51+
# TODO: handle exitcode?
4952
subprocess.call(['rm', 'ls-lR'])
5053

5154
# Example content in ls-lR:

0 commit comments

Comments
 (0)