Uses contextlib.suppress to ignore exceptions

Shorter, yet more meaningful way to ignore exceptions.
Also note it can prevent race conditions in the `if exists()` case.
This commit is contained in:
Andre Miras 2020-05-06 22:38:25 +02:00
parent efd47e60bc
commit f082919a90

View file

@ -21,7 +21,7 @@ import shutil
import fnmatch import fnmatch
import tempfile import tempfile
import time import time
from contextlib import contextmanager from contextlib import contextmanager, suppress
from datetime import datetime from datetime import datetime
from pprint import pformat from pprint import pformat
import logging import logging
@ -360,9 +360,6 @@ class Context:
# path to some tools # path to some tools
self.ccache = sh.which("ccache") self.ccache = sh.which("ccache")
if not self.ccache:
# ccache is missing, the build will not be optimized
pass
for cython_fn in ("cython-2.7", "cython"): for cython_fn in ("cython-2.7", "cython"):
cython = sh.which(cython_fn) cython = sh.which(cython_fn)
if cython: if cython:
@ -462,7 +459,7 @@ class Recipe:
if cwd: if cwd:
filename = join(cwd, filename) filename = join(cwd, filename)
if exists(filename): with suppress(FileNotFoundError):
unlink(filename) unlink(filename)
# Clean up temporary files just in case before downloading. # Clean up temporary files just in case before downloading.
@ -578,10 +575,8 @@ class Recipe:
""" """
Delete a specific marker Delete a specific marker
""" """
try: with suppress(FileNotFoundError):
unlink(join(self.build_dir, ".{}".format(marker))) unlink(join(self.build_dir, ".{}".format(marker)))
except Exception:
pass
def get_include_dir(self): def get_include_dir(self):
""" """
@ -1063,11 +1058,9 @@ class CythonRecipe(PythonRecipe):
build_env = self.get_recipe_env(arch) build_env = self.get_recipe_env(arch)
hostpython = sh.Command(self.ctx.hostpython) hostpython = sh.Command(self.ctx.hostpython)
if self.pre_build_ext: if self.pre_build_ext:
try: with suppress(Exception):
shprint(hostpython, "setup.py", "build_ext", "-g", shprint(hostpython, "setup.py", "build_ext", "-g",
_env=build_env) _env=build_env)
except Exception:
pass
self.cythonize_build() self.cythonize_build()
shprint(hostpython, "setup.py", "build_ext", "-g", shprint(hostpython, "setup.py", "build_ext", "-g",
_env=build_env) _env=build_env)
@ -1302,13 +1295,10 @@ pip Install a pip dependency into the distribution
else: else:
ctx = Context() ctx = Context()
for name in Recipe.list_recipes(): for name in Recipe.list_recipes():
try: with suppress(Exception):
recipe = Recipe.get_recipe(name, ctx) recipe = Recipe.get_recipe(name, ctx)
print("{recipe.name:<12} {recipe.version:<8}".format(recipe=recipe)) print("{recipe.name:<12} {recipe.version:<8}".format(recipe=recipe))
except Exception:
pass
def clean(self): def clean(self):
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Clean the build") description="Clean the build")