From b472b4e65da579ea00475f13be4daf5b28259c36 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Oliver=20St=C3=B6neberg?=
Date: Sun, 31 Jan 2021 14:27:11 +0100
Subject: [PATCH] some Python cleanups based on PyCharm inspections (#2999)
* some Python cleanups based on PyCharm inspections
* test-helloworld.py: adjusted assert in test_addon_relative_path()
---
addons/cert.py | 4 ++--
addons/misra.py | 10 ++++------
addons/misra_9.py | 9 +++++----
addons/test/test-misra.py | 1 -
htmlreport/cppcheck-htmlreport | 18 +++++++++---------
htmlreport/test_htmlreport.py | 2 +-
test/bug-hunting/cve.py | 2 --
test/bug-hunting/itc.py | 3 +--
test/cli/test-clang-import.py | 2 +-
test/cli/test-helloworld.py | 25 ++++++++++++-------------
test/cli/test-inline-suppress.py | 1 -
test/cli/test-proj2.py | 8 ++++----
test/cli/test-suppress-syntaxError.py | 2 --
test/cli/testutils.py | 3 +--
tools/compare_ast_symdb.py | 1 -
tools/daca2-getpackages.py | 5 -----
tools/donate-cpu.py | 8 --------
17 files changed, 40 insertions(+), 64 deletions(-)
diff --git a/addons/cert.py b/addons/cert.py
index 21c172114..9bdbf0b45 100755
--- a/addons/cert.py
+++ b/addons/cert.py
@@ -314,7 +314,7 @@ def str05(data):
# STR07-C
# Use the bounds-checking interfaces for string manipulation
def str07(data):
- if(data.standards.c=='c89' or data.standards.c=='c99'):
+ if data.standards.c=='c89' or data.standards.c=='c99':
return
for token in data.tokenlist:
if not isFunctionCall(token, ('strcpy', 'strcat')):
@@ -324,7 +324,7 @@ def str07(data):
continue
if args[1].isString:
continue
- reportError(token, 'style', 'Use the bounds-checking interfaces %s_s()' % (token.str), 'STR07-C')
+ reportError(token, 'style', 'Use the bounds-checking interfaces %s_s()' % token.str, 'STR07-C')
# STR11-C
# Do not specify the bound of a character array initialized with a string literal
diff --git a/addons/misra.py b/addons/misra.py
index 5894e1306..5d27c700a 100755
--- a/addons/misra.py
+++ b/addons/misra.py
@@ -23,7 +23,6 @@ import os
import argparse
import codecs
import string
-from collections import defaultdict
try:
from itertools import izip as zip
@@ -1119,7 +1118,7 @@ class MisraChecker:
def misra_2_7(self, data):
for func in data.functions:
# Skip function with no parameter
- if (len(func.argument) == 0):
+ if len(func.argument) == 0:
continue
# Setup list of function parameters
func_param_list = list()
@@ -1130,11 +1129,11 @@ class MisraChecker:
if (scope.type == "Function") and (scope.function == func):
# Search function body: remove referenced function parameter from list
token = scope.bodyStart
- while (token.next != None and token != scope.bodyEnd and len(func_param_list) > 0):
- if (token.variable != None and token.variable in func_param_list):
+ while token.next is not None and token != scope.bodyEnd and len(func_param_list) > 0:
+ if token.variable is not None and token.variable in func_param_list:
func_param_list.remove(token.variable)
token = token.next
- if (len(func_param_list) > 0):
+ if len(func_param_list) > 0:
# At least one parameter has not been referenced in function body
self.reportError(func.tokenDef, 2, 7)
@@ -3074,7 +3073,6 @@ class MisraChecker:
check_function(*args)
def parseDump(self, dumpfile):
- filename = '.'.join(dumpfile.split('.')[:-1])
data = cppcheckdata.parsedump(dumpfile)
typeBits['CHAR'] = data.platform.char_bit
diff --git a/addons/misra_9.py b/addons/misra_9.py
index 6217e9288..5f0b9d624 100644
--- a/addons/misra_9.py
+++ b/addons/misra_9.py
@@ -13,6 +13,7 @@ class ElementDef:
self.numInits = 0
self.childIndex = -1
+ self.flexibleToken = None
self.isFlexible = False
self.structureViolationToken = None
@@ -24,7 +25,7 @@ class ElementDef:
inits += 'D'
if not (self.isPositional or self.isDesignated) and self.numInits == 0:
inits += '_'
- if (self.numInits > 1):
+ if self.numInits > 1:
inits += str(self.numInits)
attrs = ["childIndex", "elementType", "valueType"]
@@ -59,7 +60,7 @@ class ElementDef:
t.append('D')
if self.numInits == 0:
t.append('_')
- if (self.numInits > 1):
+ if self.numInits > 1:
t.append(str(self.numInits))
myDump = "".join(t)
@@ -165,7 +166,7 @@ class ElementDef:
self.structureViolationToken = token
def markAsFlexibleArray(self, token):
- self.flexibleToken = token;
+ self.flexibleToken = token
self.isFlexible = True
def markAsCurrent(self):
@@ -371,7 +372,7 @@ class InitializerParser:
self.root = self.root.parent
- if self.token.astParent == None:
+ if self.token.astParent is None:
self.token = None
break
diff --git a/addons/test/test-misra.py b/addons/test/test-misra.py
index a4d01b36e..cc300f8ab 100644
--- a/addons/test/test-misra.py
+++ b/addons/test/test-misra.py
@@ -10,7 +10,6 @@
import pytest
import re
import sys
-import subprocess
from .util import dump_create, dump_remove, convert_json_output
diff --git a/htmlreport/cppcheck-htmlreport b/htmlreport/cppcheck-htmlreport
index 97a3af50d..7c36b297f 100755
--- a/htmlreport/cppcheck-htmlreport
+++ b/htmlreport/cppcheck-htmlreport
@@ -491,7 +491,7 @@ if __name__ == '__main__':
with io.open(source_filename, 'r', encoding=options.source_encoding) as input_file:
content = input_file.read()
except IOError:
- if (error['id'] == 'unmatchedSuppression'):
+ if error['id'] == 'unmatchedSuppression':
continue # file not found, bail out
else:
sys.stderr.write("ERROR: Source file '%s' not found.\n" %
@@ -592,13 +592,13 @@ if __name__ == '__main__':
output_file.write('\n Line | Id | CWE | Severity | Message |
')
for filename, data in sorted(files.items()):
if filename in decode_errors: # don't print a link but a note
- output_file.write("\n %s |
" % (filename))
+ output_file.write("\n %s |
" % filename)
output_file.write("\n Could not generated due to UnicodeDecodeError |
")
else:
if filename.endswith('*'): # assume unmatched suppression
output_file.write(
"\n %s |
" %
- (filename))
+ filename)
else:
output_file.write(
"\n %s |
" %
@@ -640,7 +640,7 @@ if __name__ == '__main__':
output_file.write('\n ')
output_file.write(HTML_FOOTER % contentHandler.versionCppcheck)
- if (decode_errors):
+ if decode_errors:
sys.stderr.write("\nGenerating html failed for the following files: " + ' '.join(decode_errors))
sys.stderr.write("\nConsider changing source-encoding (for example: \"htmlreport ... --source-encoding=\"iso8859-1\"\"\n")
@@ -653,7 +653,7 @@ if __name__ == '__main__':
stats_countlist = {}
for filename, data in sorted(files.items()):
- if (filename == ''):
+ if filename == '':
continue
stats_tmplist = []
for error in sorted(data['errors'], key=lambda k: k['line']):
@@ -679,11 +679,11 @@ if __name__ == '__main__':
for filename in stats_countlist:
try: # also bail out if we have a file with no sev-results
_sum += stats_countlist[filename][sev]
- stats_templist[filename] = (int)(stats_countlist[filename][sev]) # file : amount,
+ stats_templist[filename] = int(stats_countlist[filename][sev]) # file : amount,
except KeyError:
continue
# don't print "0 style" etc, if no style warnings were found
- if (_sum == 0):
+ if _sum == 0:
continue
except KeyError:
continue
@@ -697,12 +697,12 @@ if __name__ == '__main__':
for i in stats_list_sorted: # printing loop
# for aesthetics: if it's the first iteration of the loop, get
# the max length of the number string
- if (it == 0):
+ if it == 0:
LENGTH = len(str(i[1])) # <- length of longest number, now get the difference and try to make other numbers align to it
stats_file.write(" " * 3 + str(i[1]) + " " * (1 + LENGTH - len(str(i[1]))) + " " + i[0] + "
\n")
it += 1
- if (it == 10): # print only the top 10
+ if it == 10: # print only the top 10
break
stats_file.write("
\n")
diff --git a/htmlreport/test_htmlreport.py b/htmlreport/test_htmlreport.py
index f2da6a75f..ffca77f29 100755
--- a/htmlreport/test_htmlreport.py
+++ b/htmlreport/test_htmlreport.py
@@ -106,7 +106,7 @@ def runCheck(source_filename=None, xml_version='1', xml_filename=None):
with open(os.path.join(output_directory, 'index.html')) as index_file:
index_contents = index_file.read()
- yield (index_contents, output_directory)
+ yield index_contents, output_directory
shutil.rmtree(output_directory)
diff --git a/test/bug-hunting/cve.py b/test/bug-hunting/cve.py
index b2fa0f032..b81ec546c 100644
--- a/test/bug-hunting/cve.py
+++ b/test/bug-hunting/cve.py
@@ -3,8 +3,6 @@
import glob
import logging
import os
-import re
-import shutil
import sys
import subprocess
diff --git a/test/bug-hunting/itc.py b/test/bug-hunting/itc.py
index 55f312372..c4c3f8567 100644
--- a/test/bug-hunting/itc.py
+++ b/test/bug-hunting/itc.py
@@ -3,7 +3,6 @@
# https://github.com/regehr/itc-benchmarks
-import glob
import os
import re
import shutil
@@ -82,7 +81,7 @@ for testfile in TESTFILES:
missing = []
for w in wanted:
if w not in actual:
- missing.append(w);
+ missing.append(w)
if len(missing) > 0:
print('wanted:' + str(wanted))
print('actual:' + str(actual))
diff --git a/test/cli/test-clang-import.py b/test/cli/test-clang-import.py
index 7a9ca8484..7e3a392ff 100644
--- a/test/cli/test-clang-import.py
+++ b/test/cli/test-clang-import.py
@@ -4,7 +4,7 @@
import os
import re
import subprocess
-from testutils import create_gui_project_file, cppcheck
+from testutils import cppcheck
def get_debug_section(title, stdout):
diff --git a/test/cli/test-helloworld.py b/test/cli/test-helloworld.py
index f3fbaa47d..e027d774f 100644
--- a/test/cli/test-helloworld.py
+++ b/test/cli/test-helloworld.py
@@ -11,7 +11,7 @@ def cppcheck_local(args):
os.chdir('helloworld')
ret, stdout, stderr = cppcheck(args)
os.chdir(cwd)
- return (ret, stdout, stderr)
+ return ret, stdout, stderr
def getRelativeProjectPath():
return 'helloworld'
@@ -24,7 +24,7 @@ def getAbsoluteProjectPath():
def getVsConfigs(stdout, filename):
ret = []
for line in stdout.split('\n'):
- if not line.startswith('Checking %s ' % (filename)):
+ if not line.startswith('Checking %s ' % filename):
continue
if not line.endswith('...'):
continue
@@ -38,7 +38,7 @@ def test_relative_path():
ret, stdout, stderr = cppcheck(['--template=cppcheck1', 'helloworld'])
filename = os.path.join('helloworld', 'main.c')
assert ret == 0
- assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
+ assert stderr == '[%s:5]: (error) Division by zero.\n' % filename
def test_local_path():
@@ -51,7 +51,7 @@ def test_absolute_path():
ret, stdout, stderr = cppcheck(['--template=cppcheck1', prjpath])
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
- assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
+ assert stderr == '[%s:5]: (error) Division by zero.\n' % filename
def test_addon_local_path():
ret, stdout, stderr = cppcheck_local(['--addon=misra', '--template=cppcheck1', '.'])
@@ -72,31 +72,30 @@ def test_addon_relative_path():
ret, stdout, stderr = cppcheck(['--addon=misra', '--template=cppcheck1', prjpath])
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
- assert stdout == 'Checking %s ...\n' % (filename)
+ assert stdout == ('Checking %s ...\n'
+ 'Checking %s: SOME_CONFIG...\n' % (filename, filename))
assert stderr == ('[%s:5]: (error) Division by zero.\n'
'[%s:1]: (style) misra violation (use --rule-texts= to get proper output)\n' % (filename, filename))
-def test_addon_relative_path():
+def test_addon_with_gui_project():
project_file = 'helloworld/test.cppcheck'
create_gui_project_file(project_file, paths=['.'], addon='misra')
ret, stdout, stderr = cppcheck(['--template=cppcheck1', '--project=' + project_file])
filename = os.path.join('helloworld', 'main.c')
assert ret == 0
- assert stdout == 'Checking %s ...\n' % (filename)
+ assert stdout == 'Checking %s ...\n' % filename
assert stderr == ('[%s:5]: (error) Division by zero.\n'
'[%s:1]: (style) misra violation (use --rule-texts= to get proper output)\n' % (filename, filename))
def test_basepath_relative_path():
prjpath = getRelativeProjectPath()
ret, stdout, stderr = cppcheck([prjpath, '--template=cppcheck1', '-rp=' + prjpath])
- filename = os.path.join(prjpath, 'main.c')
assert ret == 0
assert stderr == '[main.c:5]: (error) Division by zero.\n'
def test_basepath_absolute_path():
prjpath = getAbsoluteProjectPath()
ret, stdout, stderr = cppcheck(['--template=cppcheck1', prjpath, '-rp=' + prjpath])
- filename = os.path.join(prjpath, 'main.c')
assert ret == 0
assert stderr == '[main.c:5]: (error) Division by zero.\n'
@@ -112,7 +111,7 @@ def test_vs_project_relative_path():
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
- assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
+ assert stderr == '[%s:5]: (error) Division by zero.\n' % filename
def test_vs_project_absolute_path():
prjpath = getAbsoluteProjectPath()
@@ -120,7 +119,7 @@ def test_vs_project_absolute_path():
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
- assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
+ assert stderr == '[%s:5]: (error) Division by zero.\n' % filename
def test_cppcheck_project_local_path():
ret, stdout, stderr = cppcheck_local(['--template=cppcheck1', '--platform=win64', '--project=helloworld.cppcheck'])
@@ -134,7 +133,7 @@ def test_cppcheck_project_relative_path():
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
assert getVsConfigs(stdout, filename) == 'Debug|x64'
- assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
+ assert stderr == '[%s:5]: (error) Division by zero.\n' % filename
def test_cppcheck_project_absolute_path():
prjpath = getAbsoluteProjectPath()
@@ -142,7 +141,7 @@ def test_cppcheck_project_absolute_path():
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
assert getVsConfigs(stdout, filename) == 'Debug|x64'
- assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
+ assert stderr == '[%s:5]: (error) Division by zero.\n' % filename
def test_suppress_command_line():
prjpath = getRelativeProjectPath()
diff --git a/test/cli/test-inline-suppress.py b/test/cli/test-inline-suppress.py
index 5c4fef176..bfdd2654f 100644
--- a/test/cli/test-inline-suppress.py
+++ b/test/cli/test-inline-suppress.py
@@ -3,7 +3,6 @@
import json
import os
-import re
from testutils import cppcheck
def create_unused_function_compile_commands():
diff --git a/test/cli/test-proj2.py b/test/cli/test-proj2.py
index 63495690d..749d6407c 100644
--- a/test/cli/test-proj2.py
+++ b/test/cli/test-proj2.py
@@ -29,7 +29,7 @@ def cppcheck_local(args):
os.chdir('proj2')
ret, stdout, stderr = cppcheck(args)
os.chdir(cwd)
- return (ret, stdout, stderr)
+ return ret, stdout, stderr
def test_file_filter():
ret, stdout, stderr = cppcheck(['proj2/','--file-filter=proj2/a/*'])
@@ -138,17 +138,17 @@ def test_gui_project_loads_absolute_vs_solution():
assert stdout.find('Checking %s Release|Win32...' % file2) >= 0
assert stdout.find('Checking %s Release|x64...' % file2) >= 0
-def test_gui_project_loads_relative_vs_solution():
+def test_gui_project_loads_relative_vs_solution_2():
create_gui_project_file('test.cppcheck', root_path='proj2', import_project='proj2/proj2.sln')
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
assert stderr == ERR_A + ERR_B
-def test_gui_project_loads_relative_vs_solution():
+def test_gui_project_loads_relative_vs_solution_with_exclude():
create_gui_project_file('test.cppcheck', root_path='proj2', import_project='proj2/proj2.sln', exclude_paths=['b'])
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
assert stderr == ERR_A
-def test_gui_project_loads_absolute_vs_solution():
+def test_gui_project_loads_absolute_vs_solution_2():
create_gui_project_file('test.cppcheck',
root_path=realpath('proj2'),
import_project=realpath('proj2/proj2.sln'))
diff --git a/test/cli/test-suppress-syntaxError.py b/test/cli/test-suppress-syntaxError.py
index 0276b1eaa..00d383eab 100644
--- a/test/cli/test-suppress-syntaxError.py
+++ b/test/cli/test-suppress-syntaxError.py
@@ -1,8 +1,6 @@
# python -m pytest test-suppress-syntaxError.py
-import os
-import re
from testutils import cppcheck
def test_j2():
diff --git a/test/cli/testutils.py b/test/cli/testutils.py
index 6e8d50c8e..2ced12b31 100644
--- a/test/cli/testutils.py
+++ b/test/cli/testutils.py
@@ -1,7 +1,6 @@
import logging
import os
-import shutil
import subprocess
# Create Cppcheck project file
@@ -32,7 +31,7 @@ def create_gui_project_file(project_file, root_path=None, import_project=None, p
cppcheck_xml += ' \n'
if addon:
cppcheck_xml += ' \n'
- cppcheck_xml += ' %s\n' % (addon)
+ cppcheck_xml += ' %s\n' % addon
cppcheck_xml += ' \n'
cppcheck_xml += '\n'
diff --git a/tools/compare_ast_symdb.py b/tools/compare_ast_symdb.py
index c6da632cb..83d998bce 100644
--- a/tools/compare_ast_symdb.py
+++ b/tools/compare_ast_symdb.py
@@ -4,7 +4,6 @@
# check all files in a compile_commands.json:
# grep '"file":' compile_commands.json | grep -v test | sed 's|.*/curl/|"--project=compile_commands.json --file-filter=*|' | xargs python3 ~/cppcheck/tools/compare_ast.py
-import glob
import os
import re
import sys
diff --git a/tools/daca2-getpackages.py b/tools/daca2-getpackages.py
index 6cfc5b197..3d0d8341b 100755
--- a/tools/daca2-getpackages.py
+++ b/tools/daca2-getpackages.py
@@ -9,15 +9,10 @@
#
-import argparse
-import logging
import subprocess
import sys
-import shutil
-import glob
import os
import re
-import datetime
import time
import natsort
diff --git a/tools/donate-cpu.py b/tools/donate-cpu.py
index d313f2adc..9b7066084 100755
--- a/tools/donate-cpu.py
+++ b/tools/donate-cpu.py
@@ -28,14 +28,6 @@
#
# Quick start: just run this script without any arguments
-import shutil
-import os
-import subprocess
-import sys
-import socket
-import time
-import re
-import tarfile
import platform
from donate_cpu_lib import *