Skip to content

Commit

Permalink
fixed some consider-using-with pylint warnings (#6903)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Oct 26, 2024
1 parent 8ba27f1 commit 0ebbf8f
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 94 deletions.
16 changes: 8 additions & 8 deletions addons/test/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ def find_cppcheck_binary():
def dump_create(fpath, *argv):
cppcheck_binary = find_cppcheck_binary()
cmd = [cppcheck_binary, "--dump", "-DDUMMY", "--quiet", fpath] + list(argv)
p = subprocess.Popen(cmd)
p.communicate()
if p.returncode != 0:
raise OSError("cppcheck returns error code: %d" % p.returncode)
p = subprocess.Popen(["sync"])
p.communicate()
with subprocess.Popen(cmd) as p:
p.communicate()
if p.returncode != 0:
raise OSError("cppcheck returns error code: %d" % p.returncode)
with subprocess.Popen(["sync"]) as p:
p.communicate()


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


def convert_json_output(raw_json_strings):
Expand Down
5 changes: 2 additions & 3 deletions test/cli/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ def create_gui_project_file(project_file, root_path=None, import_project=None, p
cppcheck_xml += ' </addons>\n'
cppcheck_xml += '</project>\n'

f = open(project_file, 'wt')
f.write(cppcheck_xml)
f.close()
with open(project_file, 'wt') as f:
f.write(cppcheck_xml)


def __lookup_cppcheck_exe():
Expand Down
10 changes: 5 additions & 5 deletions test/signal/test-signalhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def __call_process(arg):
exe = __lookup_cppcheck_exe('test-signalhandler')
if exe is None:
raise Exception('executable not found')
p = subprocess.Popen([exe, arg], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
comm = p.communicate()
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
return p.returncode, stdout, stderr
with subprocess.Popen([exe, arg], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
comm = p.communicate()
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
return p.returncode, stdout, stderr


def test_assert():
Expand Down
10 changes: 5 additions & 5 deletions test/signal/test-stacktrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ def __call_process():
exe = __lookup_cppcheck_exe('test-stacktrace')
if exe is None:
raise Exception('executable not found')
p = subprocess.Popen([exe], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
comm = p.communicate()
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
return p.returncode, stdout, stderr
with subprocess.Popen([exe], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
comm = p.communicate()
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
return p.returncode, stdout, stderr


def test_stack():
Expand Down
24 changes: 12 additions & 12 deletions tools/bisect/bisect_hang.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ def run(cppcheck_path, options, elapsed_time=None):
cmd = options.split()
cmd.insert(0, cppcheck_path)
print('running {}'.format(cppcheck_path))
p = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
try:
p.communicate(timeout=timeout)
if p.returncode != 0:
print('error')
return None
print('done')
except subprocess.TimeoutExpired:
print('timeout')
p.kill()
p.communicate()
return False
with subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) as p:
try:
p.communicate(timeout=timeout)
if p.returncode != 0:
print('error')
return None
print('done')
except subprocess.TimeoutExpired:
print('timeout')
p.kill()
p.communicate()
return False

return True

Expand Down
22 changes: 11 additions & 11 deletions tools/bisect/bisect_res.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ def run(cppcheck_path, options):
cmd = options.split()
cmd.insert(0, cppcheck_path)
print('running {}'.format(cppcheck_path))
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
stdout, stderr = p.communicate()
# only 0 and 1 are well-defined in this case
if p.returncode > 1:
print('error')
return None, None, None
# signals are reported as negative exitcode (e.g. SIGSEGV -> -11)
if p.returncode < 0:
print('crash')
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) as p:
stdout, stderr = p.communicate()
# only 0 and 1 are well-defined in this case
if p.returncode > 1:
print('error')
return None, None, None
# signals are reported as negative exitcode (e.g. SIGSEGV -> -11)
if p.returncode < 0:
print('crash')
return p.returncode, stderr, stdout
print('done')
return p.returncode, stderr, stdout
print('done')
return p.returncode, stderr, stdout


# TODO: check arguments
Expand Down
6 changes: 3 additions & 3 deletions tools/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def gitpush():


def iconv(filename):
p = subprocess.Popen(['file', '-i', filename],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
comm = p.communicate()
with subprocess.Popen(['file', '-i', filename],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as p:
comm = p.communicate()
if 'charset=iso-8859-1' in comm[0]:
subprocess.call(
["iconv", filename, "--from=ISO-8859-1", "--to=UTF-8", "-o", filename])
Expand Down
4 changes: 2 additions & 2 deletions tools/compare_ast_symdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
def run_cppcheck(cppcheck_parameters:str, clang:str):
cmd = '{} {} {} --debug --verbose'.format(CPPCHECK, cppcheck_parameters, clang)
#print(cmd)
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
comm = p.communicate()
with subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
comm = p.communicate()
return comm[0].decode('utf-8', 'ignore')


Expand Down
5 changes: 2 additions & 3 deletions tools/daca2-download.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ def getpackages():
if not wget('ls-lR.gz'):
return []
subprocess.call(['nice', 'gunzip', 'ls-lR.gz'])
f = open('ls-lR', 'rt')
lines = f.readlines()
f.close()
with open('ls-lR', 'rt') as f:
lines = f.readlines()
subprocess.call(['rm', 'ls-lR'])

path = None
Expand Down
5 changes: 2 additions & 3 deletions tools/daca2-getpackages.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ def getpackages():
subprocess.call(['nice', 'gunzip', 'ls-lR.gz'])
if not os.path.isfile('ls-lR'):
sys.exit(1)
f = open('ls-lR', 'rt')
lines = f.readlines()
f.close()
with open('ls-lR', 'rt') as f:
lines = f.readlines()
subprocess.call(['rm', 'ls-lR'])

# Example content in ls-lR:
Expand Down
12 changes: 6 additions & 6 deletions tools/donate-cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@

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

client_version_head = get_client_version_head(tree_path)
c, errout, info, t, cppcheck_options, timing_info = lib.scan_package(tree_path, source_path, libraries, capture_callstack)
Expand Down
2 changes: 1 addition & 1 deletion tools/donate_cpu_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/
# Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic
# changes)
CLIENT_VERSION = "1.3.64"
CLIENT_VERSION = "1.3.65"

# Timeout for analysis with Cppcheck in seconds
CPPCHECK_TIMEOUT = 30 * 60
Expand Down
18 changes: 8 additions & 10 deletions tools/matchcompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,9 +679,8 @@ def _replaceCStrings(self, line):
def convertFile(self, srcname, destname, line_directive):
self._reset()

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

code = ''

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

fout = io.open(destname, 'wt', encoding="utf-8")
if modified or len(self._rawMatchFunctions):
fout.write(header)
fout.write(strFunctions)
fout.write(lineno)
fout.write(code)
fout.close()
with io.open(destname, 'wt', encoding="utf-8") as fout:
if modified or len(self._rawMatchFunctions):
fout.write(header)
fout.write(strFunctions)
fout.write(lineno)
fout.write(code)


def main():
Expand Down
15 changes: 6 additions & 9 deletions tools/parse-glibc.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,14 @@ def checknonnull(cfg, functionName, nonnull):


def parseheader(cppcheckpath, filename):
f = open(filename, 'rt')
data = f.read()
f.close()
with open(filename, 'rt') as f:
data = f.read()

f = open(cppcheckpath + '/cfg/std.cfg', 'rt')
stdcfg = f.read()
f.close()
with open(cppcheckpath + '/cfg/std.cfg', 'rt') as f:
stdcfg = f.read()

f = open(cppcheckpath + '/cfg/posix.cfg', 'rt')
posixcfg = f.read()
f.close()
with open(cppcheckpath + '/cfg/posix.cfg', 'rt') as f:
posixcfg = f.read()

while '/*' in data:
pos1 = data.find('/*')
Expand Down
7 changes: 3 additions & 4 deletions tools/reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ def runtool(self, filedata=None):
return False

def __writefile(self, filename, filedata):
f = open(filename, 'wt')
for line in filedata:
f.write(line)
f.close()
with open(filename, 'wt') as f:
for line in filedata:
f.write(line)

def replaceandrun(self, what, filedata, i, line):
print(what + ' ' + str(i + 1) + '/' + str(len(filedata)) + '..')
Expand Down
4 changes: 2 additions & 2 deletions tools/trac-keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

def readdb():
cmds = ['sqlite3', TRACDB, 'SELECT id,keywords FROM ticket WHERE status<>"closed";']
p = subprocess.Popen(cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
comm = p.communicate()
with subprocess.Popen(cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
comm = p.communicate()
data = comm[0]
ret = {}
for line in data.splitlines():
Expand Down
15 changes: 8 additions & 7 deletions tools/triage_py/triage_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@

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

verbose = args.verbose
Expand Down Expand Up @@ -131,7 +131,8 @@ def sort_commit_hashes(commits):
else:
# get version string
version_cmd = exe + ' ' + '--version'
version = subprocess.Popen(version_cmd.split(), stdout=subprocess.PIPE, universal_newlines=True).stdout.read().strip()
with subprocess.Popen(version_cmd.split(), stdout=subprocess.PIPE, universal_newlines=True) as p:
version = p.stdout.read().strip()
# sanitize version
version = version.replace('Cppcheck ', '').replace(' dev', '')

Expand Down

0 comments on commit 0ebbf8f

Please sign in to comment.