Skip to content

Commit 0ebbf8f

Browse files
authored
fixed some consider-using-with pylint warnings (#6903)
1 parent 8ba27f1 commit 0ebbf8f

17 files changed

+86
-94
lines changed

Diff for: addons/test/util.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ def find_cppcheck_binary():
2121
def dump_create(fpath, *argv):
2222
cppcheck_binary = find_cppcheck_binary()
2323
cmd = [cppcheck_binary, "--dump", "-DDUMMY", "--quiet", fpath] + list(argv)
24-
p = subprocess.Popen(cmd)
25-
p.communicate()
26-
if p.returncode != 0:
27-
raise OSError("cppcheck returns error code: %d" % p.returncode)
28-
p = subprocess.Popen(["sync"])
29-
p.communicate()
24+
with subprocess.Popen(cmd) as p:
25+
p.communicate()
26+
if p.returncode != 0:
27+
raise OSError("cppcheck returns error code: %d" % p.returncode)
28+
with subprocess.Popen(["sync"]) as p:
29+
p.communicate()
3030

3131

3232
def dump_remove(fpath):
33-
p = subprocess.Popen(["rm", "-f", fpath + ".dump"])
34-
p.communicate()
33+
with subprocess.Popen(["rm", "-f", fpath + ".dump"]) as p:
34+
p.communicate()
3535

3636

3737
def convert_json_output(raw_json_strings):

Diff for: test/cli/testutils.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ def create_gui_project_file(project_file, root_path=None, import_project=None, p
4040
cppcheck_xml += ' </addons>\n'
4141
cppcheck_xml += '</project>\n'
4242

43-
f = open(project_file, 'wt')
44-
f.write(cppcheck_xml)
45-
f.close()
43+
with open(project_file, 'wt') as f:
44+
f.write(cppcheck_xml)
4645

4746

4847
def __lookup_cppcheck_exe():

Diff for: test/signal/test-signalhandler.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ def __call_process(arg):
2323
exe = __lookup_cppcheck_exe('test-signalhandler')
2424
if exe is None:
2525
raise Exception('executable not found')
26-
p = subprocess.Popen([exe, arg], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
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
26+
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
3131

3232

3333
def test_assert():

Diff for: test/signal/test-stacktrace.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ def __call_process():
2222
exe = __lookup_cppcheck_exe('test-stacktrace')
2323
if exe is None:
2424
raise Exception('executable not found')
25-
p = subprocess.Popen([exe], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
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
25+
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
3030

3131

3232
def test_stack():

Diff for: tools/bisect/bisect_hang.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ def run(cppcheck_path, options, elapsed_time=None):
1212
cmd = options.split()
1313
cmd.insert(0, cppcheck_path)
1414
print('running {}'.format(cppcheck_path))
15-
p = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
16-
try:
17-
p.communicate(timeout=timeout)
18-
if p.returncode != 0:
19-
print('error')
20-
return None
21-
print('done')
22-
except subprocess.TimeoutExpired:
23-
print('timeout')
24-
p.kill()
25-
p.communicate()
26-
return False
15+
with subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) as p:
16+
try:
17+
p.communicate(timeout=timeout)
18+
if p.returncode != 0:
19+
print('error')
20+
return None
21+
print('done')
22+
except subprocess.TimeoutExpired:
23+
print('timeout')
24+
p.kill()
25+
p.communicate()
26+
return False
2727

2828
return True
2929

Diff for: tools/bisect/bisect_res.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ def run(cppcheck_path, options):
88
cmd = options.split()
99
cmd.insert(0, cppcheck_path)
1010
print('running {}'.format(cppcheck_path))
11-
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
12-
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')
11+
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) as p:
12+
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')
2022
return p.returncode, stderr, stdout
21-
print('done')
22-
return p.returncode, stderr, stdout
2323

2424

2525
# TODO: check arguments

Diff for: tools/ci.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ def gitpush():
3939

4040

4141
def iconv(filename):
42-
p = subprocess.Popen(['file', '-i', filename],
43-
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
44-
comm = p.communicate()
42+
with subprocess.Popen(['file', '-i', filename],
43+
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as p:
44+
comm = p.communicate()
4545
if 'charset=iso-8859-1' in comm[0]:
4646
subprocess.call(
4747
["iconv", filename, "--from=ISO-8859-1", "--to=UTF-8", "-o", filename])

Diff for: tools/compare_ast_symdb.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
def run_cppcheck(cppcheck_parameters:str, clang:str):
1515
cmd = '{} {} {} --debug --verbose'.format(CPPCHECK, cppcheck_parameters, clang)
1616
#print(cmd)
17-
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
18-
comm = p.communicate()
17+
with subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
18+
comm = p.communicate()
1919
return comm[0].decode('utf-8', 'ignore')
2020

2121

Diff for: tools/daca2-download.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ def getpackages():
4141
if not wget('ls-lR.gz'):
4242
return []
4343
subprocess.call(['nice', 'gunzip', 'ls-lR.gz'])
44-
f = open('ls-lR', 'rt')
45-
lines = f.readlines()
46-
f.close()
44+
with open('ls-lR', 'rt') as f:
45+
lines = f.readlines()
4746
subprocess.call(['rm', 'ls-lR'])
4847

4948
path = None

Diff for: tools/daca2-getpackages.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ def getpackages():
4444
subprocess.call(['nice', 'gunzip', 'ls-lR.gz'])
4545
if not os.path.isfile('ls-lR'):
4646
sys.exit(1)
47-
f = open('ls-lR', 'rt')
48-
lines = f.readlines()
49-
f.close()
47+
with open('ls-lR', 'rt') as f:
48+
lines = f.readlines()
5049
subprocess.call(['rm', 'ls-lR'])
5150

5251
# Example content in ls-lR:

Diff for: tools/donate-cpu.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,12 @@
249249

250250
def get_client_version_head(path):
251251
cmd = 'python3' + ' ' + os.path.join(path, 'tools', 'donate-cpu.py') + ' ' + '--version'
252-
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, universal_newlines=True)
253-
try:
254-
comm = p.communicate()
255-
return comm[0].strip()
256-
except:
257-
return None
252+
with subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, universal_newlines=True) as p:
253+
try:
254+
comm = p.communicate()
255+
return comm[0].strip()
256+
except:
257+
return None
258258

259259
client_version_head = get_client_version_head(tree_path)
260260
c, errout, info, t, cppcheck_options, timing_info = lib.scan_package(tree_path, source_path, libraries, capture_callstack)

Diff for: tools/donate_cpu_lib.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/
1717
# Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic
1818
# changes)
19-
CLIENT_VERSION = "1.3.64"
19+
CLIENT_VERSION = "1.3.65"
2020

2121
# Timeout for analysis with Cppcheck in seconds
2222
CPPCHECK_TIMEOUT = 30 * 60

Diff for: tools/matchcompiler.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,8 @@ def _replaceCStrings(self, line):
679679
def convertFile(self, srcname, destname, line_directive):
680680
self._reset()
681681

682-
fin = io.open(srcname, "rt", encoding="utf-8")
683-
srclines = fin.readlines()
684-
fin.close()
682+
with io.open(srcname, "rt", encoding="utf-8") as fin:
683+
srclines = fin.readlines()
685684

686685
code = ''
687686

@@ -723,13 +722,12 @@ def convertFile(self, srcname, destname, line_directive):
723722
header += '#include "errorlogger.h"\n'
724723
header += '#include "token.h"\n'
725724

726-
fout = io.open(destname, 'wt', encoding="utf-8")
727-
if modified or len(self._rawMatchFunctions):
728-
fout.write(header)
729-
fout.write(strFunctions)
730-
fout.write(lineno)
731-
fout.write(code)
732-
fout.close()
725+
with io.open(destname, 'wt', encoding="utf-8") as fout:
726+
if modified or len(self._rawMatchFunctions):
727+
fout.write(header)
728+
fout.write(strFunctions)
729+
fout.write(lineno)
730+
fout.write(code)
733731

734732

735733
def main():

Diff for: tools/parse-glibc.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,14 @@ def checknonnull(cfg, functionName, nonnull):
3333

3434

3535
def parseheader(cppcheckpath, filename):
36-
f = open(filename, 'rt')
37-
data = f.read()
38-
f.close()
36+
with open(filename, 'rt') as f:
37+
data = f.read()
3938

40-
f = open(cppcheckpath + '/cfg/std.cfg', 'rt')
41-
stdcfg = f.read()
42-
f.close()
39+
with open(cppcheckpath + '/cfg/std.cfg', 'rt') as f:
40+
stdcfg = f.read()
4341

44-
f = open(cppcheckpath + '/cfg/posix.cfg', 'rt')
45-
posixcfg = f.read()
46-
f.close()
42+
with open(cppcheckpath + '/cfg/posix.cfg', 'rt') as f:
43+
posixcfg = f.read()
4744

4845
while '/*' in data:
4946
pos1 = data.find('/*')

Diff for: tools/reduce.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,9 @@ def runtool(self, filedata=None):
8888
return False
8989

9090
def __writefile(self, filename, filedata):
91-
f = open(filename, 'wt')
92-
for line in filedata:
93-
f.write(line)
94-
f.close()
91+
with open(filename, 'wt') as f:
92+
for line in filedata:
93+
f.write(line)
9594

9695
def replaceandrun(self, what, filedata, i, line):
9796
print(what + ' ' + str(i + 1) + '/' + str(len(filedata)) + '..')

Diff for: tools/trac-keywords.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
def readdb():
99
cmds = ['sqlite3', TRACDB, 'SELECT id,keywords FROM ticket WHERE status<>"closed";']
10-
p = subprocess.Popen(cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
11-
comm = p.communicate()
10+
with subprocess.Popen(cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
11+
comm = p.communicate()
1212
data = comm[0]
1313
ret = {}
1414
for line in data.splitlines():

Diff for: tools/triage_py/triage_version.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828

2929
def sort_commit_hashes(commits):
3030
git_cmd = 'git rev-list --abbrev-commit --topo-order --no-walk=sorted --reverse ' + ' '.join(commits)
31-
p = subprocess.Popen(git_cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=git_repo, universal_newlines=True)
32-
stdout, stderr = p.communicate()
33-
if p.returncode != 0:
34-
print('error: sorting commit hashes failed')
35-
print(stderr)
36-
sys.exit(1)
31+
with subprocess.Popen(git_cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=git_repo, universal_newlines=True) as p:
32+
stdout, stderr = p.communicate()
33+
if p.returncode != 0:
34+
print('error: sorting commit hashes failed')
35+
print(stderr)
36+
sys.exit(1)
3737
return stdout.splitlines()
3838

3939
verbose = args.verbose
@@ -131,7 +131,8 @@ def sort_commit_hashes(commits):
131131
else:
132132
# get version string
133133
version_cmd = exe + ' ' + '--version'
134-
version = subprocess.Popen(version_cmd.split(), stdout=subprocess.PIPE, universal_newlines=True).stdout.read().strip()
134+
with subprocess.Popen(version_cmd.split(), stdout=subprocess.PIPE, universal_newlines=True) as p:
135+
version = p.stdout.read().strip()
135136
# sanitize version
136137
version = version.replace('Cppcheck ', '').replace(' dev', '')
137138

0 commit comments

Comments
 (0)