Skip to content

Commit

Permalink
Install flake8-bugbear plugin and fix resulting warnings. NFC (#17173)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Jun 19, 2022
1 parent 3ebf9f1 commit 4343cbe
Show file tree
Hide file tree
Showing 20 changed files with 185 additions and 164 deletions.
2 changes: 1 addition & 1 deletion .flake8
@@ -1,5 +1,5 @@
[flake8]
ignore = E111,E114,E501,E261,E266,E121,E402,E241,W504,E741
ignore = E111,E114,E501,E261,E266,E121,E402,E241,W504,E741,B011
exclude =
./third_party/, # third-party code
./tools/filelock.py, # third-party code
Expand Down
4 changes: 2 additions & 2 deletions emsymbolizer.py
Expand Up @@ -87,7 +87,7 @@ def decodeVLQ(string):
result = []
shift = 0
value = 0
for i, c in enumerate(string):
for c in string:
try:
integer = vlq_map[c]
except ValueError:
Expand All @@ -107,7 +107,7 @@ def decodeVLQ(string):
line = 1
col = 1
name = 0
for i, segment in enumerate(source_map_json['mappings'].split(',')):
for segment in source_map_json['mappings'].split(','):
data = decodeVLQ(segment)
info = []

Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Expand Up @@ -5,6 +5,7 @@
# Install with `pip3 install -r requirements-dev.txt`

flake8==3.9.0
flake8-bugbear==22.4.25
flake8-unused-arguments==0.0.6
coverage==5.5

Expand Down
2 changes: 1 addition & 1 deletion site/source/get_wiki.py
Expand Up @@ -115,7 +115,7 @@ def ConvertFilesToRst():
length = len(title)
# print length
headerbar = ''
for number in range(length):
for _ in range(length):
headerbar += '='
page_reference = filenamestripped
page_reference_link_text = '.. _%s:\n\n' % page_reference
Expand Down
56 changes: 33 additions & 23 deletions tests/common.py
Expand Up @@ -643,7 +643,7 @@ def verify_es5(self, filename):
self.fail('es-check failed to verify ES5 output compliance')

# Build JavaScript code from source code
def build(self, filename, libraries=[], includes=[], force_c=False, js_outfile=True, emcc_args=[], output_basename=None):
def build(self, filename, libraries=None, includes=None, force_c=False, js_outfile=True, emcc_args=None, output_basename=None):
suffix = '.js' if js_outfile else '.wasm'
compiler = [compiler_for(filename, force_c)]
if compiler[0] == EMCC:
Expand All @@ -661,11 +661,16 @@ def build(self, filename, libraries=[], includes=[], force_c=False, js_outfile=T
else:
basename = os.path.basename(filename)
output = shared.unsuffixed(basename) + suffix
cmd = compiler + [filename, '-o', output] + self.get_emcc_args(main_file=True) + emcc_args + libraries
cmd = compiler + [filename, '-o', output] + self.get_emcc_args(main_file=True)
if emcc_args:
cmd += emcc_args
if libraries:
cmd += libraries
if shared.suffix(filename) not in ('.i', '.ii'):
# Add the location of the test file to include path.
cmd += ['-I.']
cmd += ['-I' + str(include) for include in includes]
if includes:
cmd += ['-I' + str(include) for include in includes]

self.run_process(cmd, stderr=self.stderr_redirect if not DEBUG else None)
self.assertExists(output)
Expand Down Expand Up @@ -729,7 +734,7 @@ def measure_wasm_code_lines(self, wasm):
non_data_lines = [line for line in wat_lines if '(data ' not in line]
return len(non_data_lines)

def run_js(self, filename, engine=None, args=[],
def run_js(self, filename, engine=None, args=None,
output_nicerizer=None,
assert_returncode=0,
interleaved_output=True):
Expand Down Expand Up @@ -897,9 +902,11 @@ def get_build_dir(self):
ensure_dir(ret)
return ret

def get_library(self, name, generated_libs, configure=['sh', './configure'],
configure_args=[], make=['make'], make_args=None,
def get_library(self, name, generated_libs, configure=['sh', './configure'], # noqa
configure_args=None, make=None, make_args=None,
env_init=None, cache_name_extra='', native=False):
if make is None:
make = ['make']
if env_init is None:
env_init = {}
if make_args is None:
Expand Down Expand Up @@ -929,9 +936,10 @@ def get_library(self, name, generated_libs, configure=['sh', './configure'],
return generated_libs

print(f'<building and saving {cache_name} into cache>', file=sys.stderr)
if configure is not None:
# Avoid += so we don't mutate the default arg
configure = configure + configure_args
if configure and configure_args:
# Make to copy to avoid mutating default param
configure = list(configure)
configure += configure_args

cflags = ' '.join(emcc_args)
env_init.setdefault('CFLAGS', cflags)
Expand All @@ -958,7 +966,7 @@ def run_process(self, cmd, check=True, **args):
print(e.stderr)
self.fail(f'subprocess exited with non-zero return code({e.returncode}): `{shared.shlex_join(cmd)}`')

def emcc(self, filename, args=[], output_filename=None, **kwargs):
def emcc(self, filename, args=[], output_filename=None, **kwargs): # noqa
cmd = [compiler_for(filename), filename] + self.get_emcc_args(ldflags='-c' not in args) + args
if output_filename:
cmd += ['-o', output_filename]
Expand Down Expand Up @@ -1047,9 +1055,10 @@ def _test_dylink_dso_needed(self, do_run):

so = '.wasm' if self.is_wasm() else '.js'

def ccshared(src, linkto=[]):
def ccshared(src, linkto=None):
cmdv = [EMCC, src, '-o', shared.unsuffixed(src) + so, '-sSIDE_MODULE'] + self.get_emcc_args()
cmdv += linkto
if linkto:
cmdv += linkto
self.run_process(cmdv)

ccshared('liba.cpp')
Expand All @@ -1076,8 +1085,9 @@ def ccshared(src, linkto=[]):
''',
'a: loaded\na: b (prev: (null))\na: c (prev: b)\n', emcc_args=extra_args)

extra_args = []
for libname in ['liba', 'libb', 'libc']:
self.emcc_args += ['--embed-file', libname + so]
extra_args += ['--embed-file', libname + so]
do_run(r'''
#include <assert.h>
#include <dlfcn.h>
Expand All @@ -1103,7 +1113,7 @@ def ccshared(src, linkto=[]):
return 0;
}
''' % locals(),
'a: loaded\na: b (prev: (null))\na: c (prev: b)\n')
'a: loaded\na: b (prev: (null))\na: c (prev: b)\n', emcc_args=extra_args)

def filtered_js_engines(self):
for engine in self.js_engines:
Expand Down Expand Up @@ -1140,12 +1150,12 @@ def do_run_in_out_file_test(self, *path, **kwargs):
return self._build_and_run(srcfile, expected, **kwargs)

## Does a complete test - builds, runs, checks output, etc.
def _build_and_run(self, filename, expected_output, args=[], output_nicerizer=None,
def _build_and_run(self, filename, expected_output, args=None, output_nicerizer=None,
no_build=False,
libraries=[],
includes=[],
libraries=None,
includes=None,
assert_returncode=0, assert_identical=False, assert_all=False,
check_for_error=True, force_c=False, emcc_args=[],
check_for_error=True, force_c=False, emcc_args=None,
interleaved_output=True,
regex=False,
output_basename=None):
Expand Down Expand Up @@ -1745,11 +1755,11 @@ def build_library(name,
generated_libs,
configure,
make,
make_args=[],
cache=None,
cache_name=None,
env_init={},
native=False):
make_args,
cache,
cache_name,
env_init,
native):
"""Build a library and cache the result. We build the library file
once and cache it for all our tests. (We cache in memory since the test
directory is destroyed and recreated for each test. Note that we cache
Expand Down
2 changes: 1 addition & 1 deletion tests/gen_large_switchcase.py
Expand Up @@ -10,7 +10,7 @@
cases = ''
i = 1
incr = 1
for x in range(0, num_cases):
for _ in range(0, num_cases):
cases += ' case ' + str(i) + ': return "' + str(i) + str(i) + str(i) + '";\n'
i += incr
incr = (incr % 5) + 1
Expand Down
5 changes: 3 additions & 2 deletions tests/jsrun.py
Expand Up @@ -16,7 +16,8 @@
DEFAULT_TIMEOUT = 5 * 60


def make_command(filename, engine, args=[]):
def make_command(filename, engine, args=None):
args = args or []
# if no engine is needed, indicated by None, then there is a native executable
# provided which we can just run
if engine[0] is None:
Expand Down Expand Up @@ -85,7 +86,7 @@ def require_engine(engine):
sys.exit(1)


def run_js(filename, engine, args=[],
def run_js(filename, engine, args=None,
stdin=None, stdout=PIPE, stderr=None, cwd=None,
full_output=False, assert_returncode=0, skip_check=False,
timeout=DEFAULT_TIMEOUT):
Expand Down
2 changes: 1 addition & 1 deletion tests/runner.py
Expand Up @@ -128,7 +128,7 @@ def get_all_tests(modules):
def tests_with_expanded_wildcards(args, all_tests):
# Process wildcards, e.g. "browser.test_pthread_*" should expand to list all pthread tests
new_args = []
for i, arg in enumerate(args):
for arg in args:
if '*' in arg:
if arg.startswith('skip:'):
arg = arg[5:]
Expand Down
36 changes: 21 additions & 15 deletions tests/test_benchmark.py
Expand Up @@ -65,7 +65,7 @@ def prepare(self):
def bench(self, args, output_parser=None, reps=TEST_REPS, expected_output=None):
self.times = []
self.reps = reps
for i in range(reps):
for _ in range(reps):
start = time.time()
output = self.run(args)
if expected_output is not None and expected_output not in output:
Expand Down Expand Up @@ -125,13 +125,15 @@ def get_size_text(self):


class NativeBenchmarker(Benchmarker):
def __init__(self, name, cc, cxx, args=[OPTIMIZATIONS]):
def __init__(self, name, cc, cxx, args=None):
self.name = name
self.cc = cc
self.cxx = cxx
self.args = args.copy()
self.args = args or [OPTIMIZATIONS]

def build(self, parent, filename, args, shared_args, emcc_args, native_args, native_exec, lib_builder, has_output_parser):
native_args = native_args or []
shared_args = shared_args or []
self.parent = parent
if lib_builder:
env = {'CC': self.cc, 'CXX': self.cxx, 'CXXFLAGS': "-Wno-c++11-narrowing"}
Expand Down Expand Up @@ -173,15 +175,17 @@ def run_binaryen_opts(filename, opts):


class EmscriptenBenchmarker(Benchmarker):
def __init__(self, name, engine, extra_args=[], env={}, binaryen_opts=[]):
def __init__(self, name, engine, extra_args=None, env=None, binaryen_opts=None):
self.name = name
self.engine = engine
self.extra_args = extra_args.copy()
self.extra_args = extra_args or []
self.env = os.environ.copy()
self.env.update(env)
self.binaryen_opts = binaryen_opts.copy()
if env:
self.env.update(env)
self.binaryen_opts = binaryen_opts or []

def build(self, parent, filename, args, shared_args, emcc_args, native_args, native_exec, lib_builder, has_output_parser):
emcc_args = emcc_args or []
self.filename = filename
llvm_root = self.env.get('LLVM') or config.LLVM_ROOT
if lib_builder:
Expand All @@ -190,7 +194,7 @@ def build(self, parent, filename, args, shared_args, emcc_args, native_args, nat
# systems (like zlib) if they see a CFLAGS it will override all their
# default flags, including optimizations.
env_init['CFLAGS'] = ' '.join(LLVM_FEATURE_FLAGS + [OPTIMIZATIONS] + self.extra_args)
emcc_args = emcc_args + lib_builder('js_' + llvm_root, native=False, env_init=env_init)
emcc_args += lib_builder('js_' + llvm_root, native=False, env_init=env_init)
final = os.path.dirname(filename) + os.path.sep + self.name + ('_' if self.name else '') + os.path.basename(filename) + '.js'
final = final.replace('.cpp', '')
try_delete(final)
Expand All @@ -201,7 +205,9 @@ def build(self, parent, filename, args, shared_args, emcc_args, native_args, nat
'-sENVIRONMENT=node,shell',
'-sBENCHMARK=%d' % (1 if IGNORE_COMPILATION and not has_output_parser else 0),
'-o', final
] + shared_args + LLVM_FEATURE_FLAGS
] + LLVM_FEATURE_FLAGS
if shared_args:
cmd += shared_args
if common.EMTEST_FORCE64:
cmd += ['--profiling']
else:
Expand Down Expand Up @@ -281,11 +287,11 @@ def get_output_files(self):


class CheerpBenchmarker(Benchmarker):
def __init__(self, name, engine, args=[OPTIMIZATIONS], binaryen_opts=[]):
def __init__(self, name, engine, args=None, binaryen_opts=None):
self.name = name
self.engine = engine
self.args = args.copy()
self.binaryen_opts = binaryen_opts.copy()
self.args = args or [OPTIMIZATIONS]
self.binaryen_opts = binaryen_opts or []

def build(self, parent, filename, args, shared_args, emcc_args, native_args, native_exec, lib_builder, has_output_parser):
cheerp_args = [
Expand Down Expand Up @@ -435,8 +441,8 @@ def hardcode_arguments(self, code):
''' % DEFAULT_ARG
return code

def do_benchmark(self, name, src, expected_output='FAIL', args=[],
emcc_args=[], native_args=[], shared_args=[],
def do_benchmark(self, name, src, expected_output='FAIL', args=None,
emcc_args=None, native_args=None, shared_args=None,
force_c=False, reps=TEST_REPS, native_exec=None,
output_parser=None, args_processor=None, lib_builder=None,
skip_native=False):
Expand Down Expand Up @@ -809,7 +815,7 @@ def zzz_test_corrections64(self):
'''
self.do_benchmark('corrections64', src, 'final:')

def fasta(self, name, double_rep, emcc_args=[]):
def fasta(self, name, double_rep):
src = read_file(test_file('fasta.cpp')).replace('double', double_rep)
src = src.replace(' const size_t n = ( argc > 1 ) ? atoi( argv[1] ) : 512;', '''
int n;
Expand Down

0 comments on commit 4343cbe

Please sign in to comment.