Skip to content

Commit

Permalink
Ruff rules for comprehensions, performance, and simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Nov 29, 2024
1 parent 06cebfc commit 21b4a43
Show file tree
Hide file tree
Showing 23 changed files with 77 additions and 159 deletions.
18 changes: 7 additions & 11 deletions tools/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def side_module_external_deps(external_symbols):
sym = demangle_c_symbol_name(sym)
if sym in external_symbols:
deps = deps.union(external_symbols[sym])
return sorted(list(deps))
return sorted(deps)


def create_stub_object(external_symbols):
Expand Down Expand Up @@ -184,12 +184,11 @@ def lld_flags_for_executable(external_symbols):
c_exports += side_module_external_deps(external_symbols)
for export in c_exports:
if settings.ERROR_ON_UNDEFINED_SYMBOLS:
cmd.append('--export=' + export)
cmd.append(f'--export={export}')
else:
cmd.append('--export-if-defined=' + export)
cmd.append(f'--export-if-defined={export}')

for e in settings.EXPORT_IF_DEFINED:
cmd.append('--export-if-defined=' + e)
cmd.extend(f'--export-if-defined={e}' for e in settings.EXPORT_IF_DEFINED)

if settings.RELOCATABLE:
cmd.append('--experimental-pic')
Expand Down Expand Up @@ -426,10 +425,7 @@ def eval_ctors(js_file, wasm_file, debug_info):
if kept_ctors:
args += ['--kept-exports=' + ','.join(kept_ctors)]
else:
if settings.EXPECT_MAIN:
ctor = '_start'
else:
ctor = '_initialize'
ctor = '_start' if settings.EXPECT_MAIN else '_initialize'
args = ['--ctors=' + ctor, '--kept-exports=' + ctor]
if settings.EVAL_CTORS == 2:
args += ['--ignore-external-input']
Expand Down Expand Up @@ -749,10 +745,10 @@ def minify_wasm_js(js_file, wasm_file, expensive_optimizations, debug_info):


def is_internal_global(name):
internal_start_stop_symbols = set(['__start_em_asm', '__stop_em_asm',
internal_start_stop_symbols = {'__start_em_asm', '__stop_em_asm',
'__start_em_js', '__stop_em_js',
'__start_em_lib_deps', '__stop_em_lib_deps',
'__em_lib_deps'])
'__em_lib_deps'}
internal_prefixes = ('__em_js__', '__em_lib_deps')
return name in internal_start_stop_symbols or any(name.startswith(p) for p in internal_prefixes)

Expand Down
4 changes: 1 addition & 3 deletions tools/colored_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ def add_coloring_to_emit_ansi(fn):
# add methods we need to the class
def new(*args):
levelno = args[1].levelno
if levelno >= 50:
color = '\x1b[31m' # red
elif levelno >= 40:
if levelno >= 50 or levelno >= 40:
color = '\x1b[31m' # red
elif levelno >= 30:
color = '\x1b[33m' # yellow
Expand Down
2 changes: 1 addition & 1 deletion tools/determinism_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def run():
def write(data, subdir):
if not os.path.exists(subdir):
os.mkdir(subdir)
for relevant_file in data.keys():
for relevant_file in data:
Path(os.path.join(subdir, relevant_file)).write_text(data[relevant_file])


Expand Down
5 changes: 2 additions & 3 deletions tools/emcoverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from glob import glob

import coverage.cmdline # type: ignore
import contextlib

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))

Expand All @@ -62,10 +63,8 @@ def main():
if sys.argv[1] in ('html', 'report', 'xml'):
old_argv = sys.argv
sys.argv = ['coverage', 'combine'] + glob(os.path.join(store, '*'))
try:
with contextlib.suppress(SystemExit):
coverage.cmdline.main()
except SystemExit:
pass
sys.argv = old_argv + ['-i']
return coverage.cmdline.main()

Expand Down
8 changes: 4 additions & 4 deletions tools/emdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ def merge_entry_to_existing(existing_data, new_entry, total_source_set_size):
name = new_entry['unminified_name']
if name in existing_data:
ex = existing_data[name]
num_times_occurs_1 = ex['num_times_occurs'] if 'num_times_occurs' in ex else 1
num_times_occurs_2 = new_entry['num_times_occurs'] if 'num_times_occurs' in new_entry else 1
num_times_occurs_1 = ex.get('num_times_occurs', 1)
num_times_occurs_2 = new_entry.get('num_times_occurs', 1)
existing_data[name] = {
'lines': ex['lines'] + new_entry['lines'],
'bytes': ex['bytes'] + new_entry['bytes'],
Expand Down Expand Up @@ -212,7 +212,7 @@ def analyze_javascript_file_contents(filename, file_contents, total_source_set_s
if asm_start >= 0:
asm_start_brace = file_contents.rfind('{', 0, asm_start)
if asm_start_brace >= 0:
asm_end_brace = brace_map[asm_start_brace] if asm_start_brace in brace_map else file_len
asm_end_brace = brace_map.get(asm_start_brace, file_len)

func_pos = -1
var_pos = -1
Expand Down Expand Up @@ -516,7 +516,7 @@ def print_symbol_info(data, total_source_set_size):
continue
if options.only_common and (not e['in_set_1'] or not e['in_set_2']):
continue
prev_bytes = e['prev_bytes'] if 'prev_bytes' in e else 0
prev_bytes = e.get('prev_bytes', 0)
if max(e['bytes'], prev_bytes) < options.filter_size:
continue
if e['bytes'] == prev_bytes and options.only_changes:
Expand Down
39 changes: 11 additions & 28 deletions tools/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def set_memory(static_bump):

def report_missing_exports_wasm_only(metadata):
if diagnostics.is_enabled('undefined'):
defined_symbols = set(asmjs_mangle(e) for e in metadata.all_exports)
defined_symbols = {asmjs_mangle(e) for e in metadata.all_exports}
missing = set(settings.USER_EXPORTS) - defined_symbols
for symbol in sorted(missing):
diagnostics.warning('undefined', f'undefined exported symbol: "{symbol}"')
Expand All @@ -235,7 +235,7 @@ def report_missing_exports(js_symbols):
if diagnostics.is_enabled('undefined'):
# Report any symbol that was explicitly exported but is present neither
# as a native function nor as a JS library function.
defined_symbols = set(asmjs_mangle(e) for e in settings.WASM_EXPORTS).union(js_symbols)
defined_symbols = {asmjs_mangle(e) for e in settings.WASM_EXPORTS}.union(js_symbols)
missing = set(settings.USER_EXPORTS) - defined_symbols
for symbol in sorted(missing):
diagnostics.warning('undefined', f'undefined exported symbol: "{symbol}"')
Expand Down Expand Up @@ -346,10 +346,7 @@ def emscript(in_wasm, out_wasm, outfile_js, js_syms, finalize=True, base_metadat

for em_js_func, raw in metadata.em_js_funcs.items():
c_sig = raw.split('<::>')[0].strip('()')
if not c_sig or c_sig == 'void':
c_sig = []
else:
c_sig = c_sig.split(',')
c_sig = [] if not c_sig or c_sig == 'void' else c_sig.split(',')
if em_js_func in import_map:
imp = import_map[em_js_func]
assert imp.kind == webassembly.ExternType.FUNC
Expand Down Expand Up @@ -646,11 +643,8 @@ def create_tsd_exported_runtime_methods(metadata):
tsc_output_file = in_temp('jsdoc.d.ts')
utils.write_file(js_doc_file, js_doc)
tsc = shutil.which('tsc')
if tsc:
# Use the full path from the which command so windows can find tsc.
tsc = [tsc]
else:
tsc = shared.get_npm_cmd('tsc')
# Use the full path from the which command so windows can find tsc.
tsc = [tsc] if tsc else shared.get_npm_cmd('tsc')
cmd = tsc + ['--outFile', tsc_output_file, '--declaration', '--emitDeclarationOnly', '--allowJs', js_doc_file]
shared.check_call(cmd, cwd=path_from_root())
return utils.read_file(tsc_output_file)
Expand Down Expand Up @@ -699,15 +693,12 @@ def create_asm_consts(metadata):
asm_consts = {}
for addr, const in metadata.em_asm_consts.items():
body = trim_asm_const_body(const)
args = []
max_arity = 16
arity = 0
for i in range(max_arity):
if ('$' + str(i)) in const:
if f'${i}' in const:
arity = i + 1
for i in range(arity):
args.append('$' + str(i))
args = ', '.join(args)
args = ', '.join(f"${i}" for i in range(arity))
if 'arguments' in body:
# arrow functions don't bind `arguments` so we have to use
# the old function syntax in this case
Expand All @@ -717,8 +708,7 @@ def create_asm_consts(metadata):
if settings.RELOCATABLE:
addr += settings.GLOBAL_BASE
asm_consts[addr] = func
asm_consts = [(key, value) for key, value in asm_consts.items()]
asm_consts.sort()
asm_consts = sorted(asm_consts.items())
return asm_consts


Expand Down Expand Up @@ -762,10 +752,7 @@ def create_em_js(metadata):
assert separator in raw
args, body = raw.split(separator, 1)
args = args[1:-1]
if args == 'void':
args = []
else:
args = args.split(',')
args = [] if args == 'void' else args.split(',')
arg_names = [arg.split()[-1].replace('*', '') for arg in args if arg]
args = ','.join(arg_names)
func = f'function {name}({args}) {body}'
Expand Down Expand Up @@ -836,10 +823,8 @@ def add_standard_wasm_imports(send_items_map):

def create_sending(metadata, library_symbols):
# Map of wasm imports to mangled/external/JS names
send_items_map = {}
send_items_map = {name: name for name in metadata.imports}

for name in metadata.invoke_funcs:
send_items_map[name] = name
for name in metadata.imports:
if name in metadata.em_js_funcs:
send_items_map[name] = name
Expand Down Expand Up @@ -903,9 +888,7 @@ def install_wrapper(sym):
# pthread_self and _emscripten_proxy_execute_task_queue are currently called in some
# cases after the runtime has exited.
# TODO: Look into removing these, and improving our robustness around thread termination.
if sym in ('__trap', 'pthread_self', '_emscripten_proxy_execute_task_queue'):
return False
return True
return not sym in ('__trap', 'pthread_self', '_emscripten_proxy_execute_task_queue')

for name, types in function_exports.items():
nargs = len(types.params)
Expand Down
2 changes: 1 addition & 1 deletion tools/experimental/reproduceriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
window_location = sys.argv[4] if len(sys.argv) >= 5 else ''
on_idle = sys.argv[5] if len(sys.argv) >= 6 else ''

shell = not not window_location
shell = bool(window_location)

dirs_to_drop = 0 if not os.path.dirname(first_js) else len(os.path.dirname(first_js).split('/'))

Expand Down
8 changes: 4 additions & 4 deletions tools/feature_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,16 @@ def report_missing(setting_name):
setting_value = getattr(settings, setting_name)
logger.debug(f'cannot use {feature.name} because {setting_name} is too old: {setting_value}')

if settings.MIN_CHROME_VERSION < min_versions['chrome']:
if min_versions['chrome'] > settings.MIN_CHROME_VERSION:
report_missing('MIN_CHROME_VERSION')
return False
if settings.MIN_FIREFOX_VERSION < min_versions['firefox']:
if min_versions['firefox'] > settings.MIN_FIREFOX_VERSION:
report_missing('MIN_FIREFOX_VERSION')
return False
if settings.MIN_SAFARI_VERSION < min_versions['safari']:
if min_versions['safari'] > settings.MIN_SAFARI_VERSION:
report_missing('MIN_SAFARI_VERSION')
return False
if 'node' in min_versions and settings.MIN_NODE_VERSION < min_versions['node']:
if 'node' in min_versions and min_versions['node'] > settings.MIN_NODE_VERSION:
report_missing('MIN_NODE_VERSION')
return False
return True
Expand Down
5 changes: 1 addition & 4 deletions tools/file_packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ def should_ignore(fullname):
if has_hidden_attribute(fullname):
return True

for p in excluded_patterns:
if fnmatch.fnmatch(fullname, p):
return True
return False
return any(fnmatch.fnmatch(fullname, p) for p in excluded_patterns)


def add(mode, rootpathsrc, rootpathdst):
Expand Down
4 changes: 1 addition & 3 deletions tools/js_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,7 @@ def write_chunk(chunk, i):

with ToolchainProfiler.profile_block('sort_or_concat'):
# sort functions by size, to make diffing easier and to improve aot times
funcses = []
for out_file in filenames:
funcses.append(split_funcs(utils.read_file(out_file)))
funcses = [split_funcs(utils.read_file(out_file)) for out_file in filenames]
funcs = [item for sublist in funcses for item in sublist]
funcses = None
if not os.environ.get('EMCC_NO_OPT_SORT'):
Expand Down
16 changes: 7 additions & 9 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def will_metadce():
def setup_environment_settings():
# Environment setting based on user input
environments = settings.ENVIRONMENT.split(',')
if any([x for x in environments if x not in VALID_ENVIRONMENTS]):
if any(x for x in environments if x not in VALID_ENVIRONMENTS):
exit_with_error(f'Invalid environment specified in "ENVIRONMENT": {settings.ENVIRONMENT}. Should be one of: {",".join(VALID_ENVIRONMENTS)}')

settings.ENVIRONMENT_MAY_BE_WEB = not settings.ENVIRONMENT or 'web' in environments
Expand Down Expand Up @@ -216,8 +216,7 @@ def get_js_sym_info():
# and can contain full paths to temporary files.
skip_settings = {'PRE_JS_FILES', 'POST_JS_FILES'}
input_files = [json.dumps(settings.external_dict(skip_keys=skip_settings), sort_keys=True, indent=2)]
for jslib in sorted(glob.glob(utils.path_from_root('src') + '/library*.js')):
input_files.append(read_file(jslib))
input_files.extend(read_file(jslib) for jslib in sorted(glob.glob(utils.path_from_root('src') + '/library*.js')))
for jslib in settings.JS_LIBRARIES:
if not os.path.isabs(jslib):
jslib = utils.path_from_root('src', jslib)
Expand Down Expand Up @@ -626,7 +625,7 @@ def check_browser_versions():

if settings.LEGACY_VM_SUPPORT:
# Default all browser versions to zero
for key in min_version_settings.keys():
for key in min_version_settings:
default_setting(key, 0)

for key, oldest in min_version_settings.items():
Expand Down Expand Up @@ -967,9 +966,8 @@ def phase_linker_setup(options, state, newargs):
if settings.MINIMAL_RUNTIME_STREAMING_WASM_COMPILATION and settings.MINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION:
exit_with_error('MINIMAL_RUNTIME_STREAMING_WASM_COMPILATION and MINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION are mutually exclusive!')

if options.emrun:
if settings.MINIMAL_RUNTIME:
exit_with_error('--emrun is not compatible with MINIMAL_RUNTIME')
if options.emrun and settings.MINIMAL_RUNTIME:
exit_with_error('--emrun is not compatible with MINIMAL_RUNTIME')

if options.use_closure_compiler:
settings.USE_CLOSURE_COMPILER = 1
Expand Down Expand Up @@ -2943,7 +2941,7 @@ def process_dynamic_libs(dylibs, lib_dirs):
dylibs += extras
for dylib in dylibs:
exports = webassembly.get_exports(dylib)
exports = set(e.name for e in exports)
exports = {e.name for e in exports}
# EM_JS function are exports with a special prefix. We need to strip
# this prefix to get the actual symbol name. For the main module, this
# is handled by extract_metadata.py.
Expand All @@ -2957,7 +2955,7 @@ def process_dynamic_libs(dylibs, lib_dirs):
# TODO(sbc): Integrate with metadata.invoke_funcs that comes from the
# main module to avoid creating new invoke functions at runtime.
imports = set(imports)
imports = set(i for i in imports if not i.startswith('invoke_'))
imports = {i for i in imports if not i.startswith('invoke_')}
weak_imports = webassembly.get_weak_imports(dylib)
strong_imports = sorted(imports.difference(weak_imports))
logger.debug('Adding symbols requirements from `%s`: %s', dylib, imports)
Expand Down
2 changes: 1 addition & 1 deletion tools/maint/check_struct_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def check_structs(info):


def check_defines(info):
for define in info['defines'].keys():
for define in info['defines']:
key = r'cDefs\.' + define + r'\>'
# grep --quiet ruturns 0 when there is a match
if subprocess.run(['git', 'grep', '--quiet', key], check=False).returncode != 0:
Expand Down
17 changes: 7 additions & 10 deletions tools/maint/gen_sig_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ def ignore_symbol(s, cxx):
'stackSave', 'stackRestore', 'stackAlloc', 'getTempRet0', 'setTempRet0',
}:
return True
if cxx and s in ('__asctime_r') or s.startswith('__cxa_find_matching_catch'):
return True
return False
return bool(cxx and s in '__asctime_r' or s.startswith('__cxa_find_matching_catch'))


def create_c_file(filename, symbol_list, header):
Expand Down Expand Up @@ -263,7 +261,7 @@ def update_line(l):
def remove_sigs(sig_info):
print("removing __sig attributes ...")

to_remove = [f'{sym}__sig:' for sym in sig_info.keys()]
to_remove = [f'{sym}__sig:' for sym in sig_info]

def strip_line(l):
l = l.strip()
Expand Down Expand Up @@ -362,12 +360,11 @@ def extract_sig_info(sig_info, extra_settings=None, extra_cflags=None, cxx=False
assert sym in sig_info64
sig64 = sig_info64[sym]
sig_string = functype_to_str(sig32, sig64)
if sym in sig_info:
if sig_info[sym] != sig_string:
print(sym)
print(sig_string)
print(sig_info[sym])
assert sig_info[sym] == sig_string
if sym in sig_info and sig_info[sym] != sig_string:
print(sym)
print(sig_string)
print(sig_info[sym])
assert sig_info[sym] == sig_string
sig_info[sym] = sig_string


Expand Down
Loading

0 comments on commit 21b4a43

Please sign in to comment.