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()
This commit is contained in:
Oliver Stöneberg 2021-01-31 14:27:11 +01:00 committed by GitHub
parent 3ec6e85af0
commit b472b4e65d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 40 additions and 64 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -10,7 +10,6 @@
import pytest
import re
import sys
import subprocess
from .util import dump_create, dump_remove, convert_json_output

View File

@ -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 <tr><th>Line</th><th>Id</th><th>CWE</th><th>Severity</th><th>Message</th></tr>')
for filename, data in sorted(files.items()):
if filename in decode_errors: # don't print a link but a note
output_file.write("\n <tr><td colspan=\"5\">%s</td></tr>" % (filename))
output_file.write("\n <tr><td colspan=\"5\">%s</td></tr>" % filename)
output_file.write("\n <tr><td colspan=\"5\"> Could not generated due to UnicodeDecodeError</td></tr>")
else:
if filename.endswith('*'): # assume unmatched suppression
output_file.write(
"\n <tr><td colspan=\"5\">%s</td></tr>" %
(filename))
filename)
else:
output_file.write(
"\n <tr><td colspan=\"5\"><a href=\"%s\">%s</a></td></tr>" %
@ -640,7 +640,7 @@ if __name__ == '__main__':
output_file.write('\n </table>')
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("&#160;" * 3 + str(i[1]) + "&#160;" * (1 + LENGTH - len(str(i[1]))) + "<a href=\"" + files[i[0]]['htmlfile'] + "\"> " + i[0] + "</a><br>\n")
it += 1
if (it == 10): # print only the top 10
if it == 10: # print only the top 10
break
stats_file.write("</p>\n")

View File

@ -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)

View File

@ -3,8 +3,6 @@
import glob
import logging
import os
import re
import shutil
import sys
import subprocess

View File

@ -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))

View File

@ -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):

View File

@ -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=<file> 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=<file> 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()

View File

@ -3,7 +3,6 @@
import json
import os
import re
from testutils import cppcheck
def create_unused_function_compile_commands():

View File

@ -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'))

View File

@ -1,8 +1,6 @@
# python -m pytest test-suppress-syntaxError.py
import os
import re
from testutils import cppcheck
def test_j2():

View File

@ -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 += ' </suppressions>\n'
if addon:
cppcheck_xml += ' <addons>\n'
cppcheck_xml += ' <addon>%s</addon>\n' % (addon)
cppcheck_xml += ' <addon>%s</addon>\n' % addon
cppcheck_xml += ' </addons>\n'
cppcheck_xml += '</project>\n'

View File

@ -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

View File

@ -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

View File

@ -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 *