Skip to content

Fix all flake8 issues on python 3 #9209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[flake8]
ignore = E111,E114,E501,E261,E266,E121,E402,E241
ignore = E111,E114,E501,E261,E266,E121,E402,E241,W504
exclude =
./third_party/, # third-party code
./tools/filelock.py, # third-party code
./tools/scons/, # third-party code
./tests/freetype/src/, # third-party code
./tests/poppler/poppler/, # third-party code
./site/source/conf.py,
./tools/debug/,
./tools/emdump.py,
Expand Down
2 changes: 1 addition & 1 deletion emar.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def run():
newargs[j] = full_newname
to_delete.append(full_newname)
contents.add(newname)
except:
except Exception:
# it is ok to fail here, we just don't get hashing
contents.add(basename)
pass
Expand Down
35 changes: 18 additions & 17 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ def run(args):
if '--version' in args:
try:
revision = run_process(['git', 'show'], stdout=PIPE, stderr=PIPE, cwd=shared.path_from_root()).stdout.splitlines()[0]
except:
except Exception:
revision = '(unknown revision)'
print('''emcc (Emscripten gcc/clang-like replacement) %s (%s)
Copyright (C) 2014 the Emscripten authors (see AUTHORS.txt)
Expand Down Expand Up @@ -631,7 +631,7 @@ def is_minus_s_for_emcc(args, i):
src = open(arg).read()
if debug_configure:
open(tempout, 'a').write('============= ' + arg + '\n' + src + '\n=============\n\n')
except:
except IOError:
pass
elif arg.endswith('.s'):
if debug_configure:
Expand Down Expand Up @@ -721,7 +721,7 @@ def filter_emscripten_options(argv):
open(target, 'w').write('#!' + full_node + '\n' + src) # add shebang
try:
os.chmod(target, stat.S_IMODE(os.stat(target).st_mode) | stat.S_IXUSR) # make executable
except:
except OSError:
pass # can fail if e.g. writing the executable to /dev/null
return ret

Expand Down Expand Up @@ -862,7 +862,7 @@ def optimizing(opts):
newargs[i] = newargs[i + 1] = ''
if key == 'WASM_BACKEND=1':
exit_with_error('do not set -s WASM_BACKEND, instead set EMCC_WASM_BACKEND=1 in the environment')
newargs = [arg for arg in newargs if arg is not '']
newargs = [arg for arg in newargs if arg != '']

settings_key_changes = set()
for s in settings_changes:
Expand Down Expand Up @@ -929,14 +929,14 @@ def optimizing(opts):
input_files.append((i, arg))
elif file_suffix.endswith(STATICLIB_ENDINGS + DYNAMICLIB_ENDINGS):
# if it's not, and it's a library, just add it to libs to find later
l = unsuffixed_basename(arg)
libname = unsuffixed_basename(arg)
for prefix in LIB_PREFIXES:
if not prefix:
continue
if l.startswith(prefix):
l = l[len(prefix):]
if libname.startswith(prefix):
libname = libname[len(prefix):]
break
libs.append((i, l))
libs.append((i, libname))
newargs[i] = ''
else:
logger.warning(arg + ' is not a valid input file')
Expand Down Expand Up @@ -982,7 +982,7 @@ def optimizing(opts):

original_input_files = input_files[:]

newargs = [a for a in newargs if a is not '']
newargs = [a for a in newargs if a != '']

has_dash_c = '-c' in newargs
has_dash_S = '-S' in newargs
Expand Down Expand Up @@ -3122,7 +3122,7 @@ def generate_minimal_runtime_html(target, options, js_target, target_basename,
memfile, optimizer):
logger.debug('generating HTML for minimal runtime')
shell = read_and_preprocess(options.shell_path)
if re.search('{{{\s*SCRIPT\s*}}}', shell):
if re.search(r'{{{\s*SCRIPT\s*}}}', shell):
exit_with_error('--shell-file "' + options.shell_path + '": MINIMAL_RUNTIME uses a different kind of HTML page shell file than the traditional runtime! Please see $EMSCRIPTEN/src/shell_minimal_runtime.html for a template to use as a basis.')

shell = shell.replace('{{{ TARGET_BASENAME }}}', target_basename)
Expand Down Expand Up @@ -3528,7 +3528,7 @@ def parse_string_list(text):
# simpler syntaxes
try:
return json.loads(text)
except:
except ValueError:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, how can we be sure this is the only type of exception that can arise here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From documentation:

If the data being deserialized is not a valid JSON document, a JSONDecodeError [subclass of ValueError] will be raised.

This is the only kind of error we want to recover from.

return parse_string_list(text)

try:
Expand All @@ -3540,12 +3540,13 @@ def parse_string_list(text):
def validate_arg_level(level_string, max_level, err_msg, clamp=False):
try:
level = int(level_string)
if clamp:
if level > max_level:
logger.warning("optimization level '-O" + level_string + "' is not supported; using '-O" + str(max_level) + "' instead")
level = max_level
assert 0 <= level <= max_level
except:
except ValueError:
raise Exception(err_msg)
if clamp:
if level > max_level:
logger.warning("optimization level '-O" + level_string + "' is not supported; using '-O" + str(max_level) + "' instead")
level = max_level
if not 0 <= level <= max_level:
raise Exception(err_msg)
return level

Expand Down
2 changes: 1 addition & 1 deletion emlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
def run():
try:
me, main, side, out = sys.argv[:4]
except:
except ValueError:
print('usage: emlink.py [main module] [side module] [output name]', file=sys.stderr)
sys.exit(1)

Expand Down
34 changes: 17 additions & 17 deletions emrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,10 @@ def kill_browser_process():
else:
try:
subprocess.call(['pkill', processname_killed_atexit])
except OSError as e:
except OSError:
try:
subprocess.call(['killall', processname_killed_atexit])
except OSError as e:
except OSError:
loge('Both commands pkill and killall failed to clean up the spawned browser process. Perhaps neither of these utilities is available on your system?')
delete_emrun_safe_firefox_profile()
# Clear the process name to represent that the browser is now dead.
Expand Down Expand Up @@ -567,7 +567,7 @@ def send_head(self):

def log_request(self, code):
# Filter out 200 OK messages to remove noise.
if code is not 200:
if code != 200:
SimpleHTTPRequestHandler.log_request(self, code)

def log_message(self, format, *args):
Expand All @@ -588,7 +588,7 @@ def do_POST(self):
dump_out_directory = 'dump_out'
try:
os.mkdir(dump_out_directory)
except:
except OSError:
pass
filename = os.path.join(dump_out_directory, os.path.normpath(filename))
open(filename, 'wb').write(data)
Expand All @@ -598,7 +598,7 @@ def do_POST(self):
system_info = json.loads(get_system_info(format_json=True))
try:
browser_info = json.loads(get_browser_info(browser_exe, format_json=True))
except:
except ValueError:
browser_info = ''
data = {'system': system_info, 'browser': browser_info}
self.send_response(200)
Expand Down Expand Up @@ -630,7 +630,7 @@ def do_POST(self):
i = data.index('^', 5)
seq_num = int(data[5:i])
data = data[i + 1:]
except:
except ValueError:
pass

is_exit = data.startswith('^exit^')
Expand Down Expand Up @@ -735,7 +735,7 @@ def find_gpu_model(model):

try:
import_win32api_modules()
except:
except Exception:
return []

for i in range(0, 16):
Expand All @@ -760,13 +760,13 @@ def find_gpu_model(model):
try:
driverVersion = winreg.QueryValueEx(hVideoCardReg, 'DriverVersion')[0]
VideoCardDescription += ', driver version ' + driverVersion
except:
except WindowsError:
pass

try:
driverDate = winreg.QueryValueEx(hVideoCardReg, 'DriverDate')[0]
VideoCardDescription += ' (' + driverDate + ')'
except:
except WindowsError:
pass

VideoCardMemorySize = winreg.QueryValueEx(hVideoCardReg, 'HardwareInformation.MemorySize')[0]
Expand Down Expand Up @@ -831,7 +831,7 @@ def macos_get_gpu_info():
memory = int(re.search("VRAM (.*?): (.*) MB", gpu).group(2).strip())
gpus += [{'model': model_name + ' (' + bus + ')', 'ram': memory * 1024 * 1024}]
return gpus
except:
except Exception:
pass


Expand Down Expand Up @@ -941,7 +941,7 @@ def win_get_file_properties(fname):
strInfo[propName] = win32api.GetFileVersionInfo(fname, strInfoPath)

props['StringFileInfo'] = strInfo
except:
except Exception:
pass

return props
Expand All @@ -954,7 +954,7 @@ def get_computer_model():
with open(os.path.join(os.getenv("HOME"), '.emrun.hwmodel.cached'), 'r') as f:
model = f.read()
return model
except:
except IOError:
pass

try:
Expand All @@ -969,7 +969,7 @@ def get_computer_model():
model = model.group(1).strip()
open(os.path.join(os.getenv("HOME"), '.emrun.hwmodel.cached'), 'w').write(model) # Cache the hardware model to disk
return model
except:
except Exception:
hwmodel = check_output(['sysctl', 'hw.model'])
hwmodel = re.search('hw.model: (.*)', hwmodel).group(1).strip()
return hwmodel
Expand Down Expand Up @@ -1006,15 +1006,15 @@ def get_os_version():
version = ''
try:
version = ' ' + check_output(['wmic', 'os', 'get', 'version']).split('\n')[1].strip()
except:
except Exception:
pass
return productName[0] + version + bitness
elif MACOS:
return 'macOS ' + platform.mac_ver()[0] + bitness
elif LINUX:
kernel_version = check_output(['uname', '-r']).strip()
return ' '.join(platform.linux_distribution()) + ', linux kernel ' + kernel_version + ' ' + platform.architecture()[0] + bitness
except:
except Exception:
return 'Unknown OS'


Expand All @@ -1037,7 +1037,7 @@ def get_system_memory():
return win32api.GlobalMemoryStatusEx()['TotalPhys']
elif MACOS:
return int(check_output(['sysctl', '-n', 'hw.memsize']).strip())
except:
except Exception:
return -1


Expand Down Expand Up @@ -1292,7 +1292,7 @@ def get_system_info(format_json):
else:
try:
unique_system_id = open(os.path.expanduser('~/.emrun.generated.guid'), 'r').read().strip()
except:
except Exception:
import uuid
unique_system_id = str(uuid.uuid4())
try:
Expand Down
10 changes: 5 additions & 5 deletions emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def parse_fastcomp_output(backend_output, DEBUG):

try:
metadata = json.loads(metadata_raw, object_pairs_hook=OrderedDict)
except:
except ValueError:
logger.error('emscript: failure to parse metadata output from compiler backend. raw output is: \n' + metadata_raw)
raise

Expand Down Expand Up @@ -344,7 +344,7 @@ def move_preasm(m):
# calculate globals
try:
del forwarded_json['Variables']['globals']['_llvm_global_ctors'] # not a true variable
except:
except KeyError:
pass
if not shared.Settings.RELOCATABLE:
global_vars = metadata['externs']
Expand Down Expand Up @@ -450,7 +450,7 @@ def collapse_redundant_vars(code):
old_code = ''
while code != old_code: # Repeated vars overlap, so can't run in one regex pass. Runs in O(log(N)) time
old_code = code
code = re.sub('(var [^;]*);\s*var ', r'\1,\n ', code)
code = re.sub(r'(var [^;]*);\s*var ', r'\1,\n ', code)
return code


Expand Down Expand Up @@ -631,7 +631,7 @@ def is_int(x):
try:
int(x)
return True
except:
except ValueError:
return False


Expand Down Expand Up @@ -2211,7 +2211,7 @@ def emscript_wasm_backend(infile, outfile, memfile, libraries, compiler_engine,

try:
del forwarded_json['Variables']['globals']['_llvm_global_ctors'] # not a true variable
except:
except KeyError:
pass

sending = create_sending_wasm(invoke_funcs, forwarded_json, metadata)
Expand Down
2 changes: 1 addition & 1 deletion site/source/get_api_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def addapiitems(matchobj):
api_item = api_item.split('(')[0]
try:
api_item = api_item.split(' ')[1]
except:
except IndexError:
pass

# print lang
Expand Down
4 changes: 2 additions & 2 deletions site/source/get_wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def errorhandler(func, path, exc_info):
try:
shutil.rmtree(output_dir, ignore_errors=False, onerror=errorhandler)
print('Old wiki clone removed')
except:
except IOError:
print('No directory to clean found')


Expand All @@ -72,7 +72,7 @@ def CloneWiki():
try:
os.makedirs(output_dir)
print('Created directory')
except:
except OSError:
pass

# Clone
Expand Down
2 changes: 1 addition & 1 deletion tests/fuzz/creduce_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def try_js(args):
try:
try_js(args)
break
except Exception as e:
except Exception:
pass
else:
sys.exit(0)
Expand Down
2 changes: 1 addition & 1 deletion tests/fuzz/csmith_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
shared.try_delete(filename)
try:
shared.run_process([COMP, '-m32', opts, fullname, '-o', filename + '1'] + CSMITH_CFLAGS + ['-w']) # + shared.EMSDK_OPTS
except CalledProcessError as e:
except CalledProcessError:
print('Failed to compile natively using clang')
notes['invalid'] += 1
continue
Expand Down
6 changes: 3 additions & 3 deletions tests/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ def build(self, src, dirname, filename, main_file=None,
try:
# Make sure we notice if compilation steps failed
os.remove(f + '.o')
except:
except OSError:
pass
args = [PYTHON, EMCC] + self.get_emcc_args(main_file=True) + \
['-I' + dirname, '-I' + os.path.join(dirname, 'include')] + \
Expand Down Expand Up @@ -1660,7 +1660,7 @@ def build_library(name,
stderr = None
try:
Building.configure(configure + configure_args, env=env, stdout=stdout, stderr=stderr)
except subprocess.CalledProcessError as e:
except subprocess.CalledProcessError:
pass # Ignore exit code != 0

def open_make_out(mode='r'):
Expand Down Expand Up @@ -1765,7 +1765,7 @@ def skip_requested_tests(args, modules):
suite = getattr(m, suite_name)
setattr(suite, test_name, lambda s: s.skipTest("requested to be skipped"))
break
except:
except AttributeError:
pass
args[i] = None
return [a for a in args if a is not None]
Expand Down
Loading