From f082919a90270c225f6af4123a4d8e0116259e07 Mon Sep 17 00:00:00 2001 From: Andre Miras Date: Wed, 6 May 2020 22:38:25 +0200 Subject: [PATCH] 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. --- kivy_ios/toolchain.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/kivy_ios/toolchain.py b/kivy_ios/toolchain.py index b4807b7..888fcd2 100755 --- a/kivy_ios/toolchain.py +++ b/kivy_ios/toolchain.py @@ -21,7 +21,7 @@ import shutil import fnmatch import tempfile import time -from contextlib import contextmanager +from contextlib import contextmanager, suppress from datetime import datetime from pprint import pformat import logging @@ -360,9 +360,6 @@ class Context: # path to some tools 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"): cython = sh.which(cython_fn) if cython: @@ -462,7 +459,7 @@ class Recipe: if cwd: filename = join(cwd, filename) - if exists(filename): + with suppress(FileNotFoundError): unlink(filename) # Clean up temporary files just in case before downloading. @@ -578,10 +575,8 @@ class Recipe: """ Delete a specific marker """ - try: + with suppress(FileNotFoundError): unlink(join(self.build_dir, ".{}".format(marker))) - except Exception: - pass def get_include_dir(self): """ @@ -1063,11 +1058,9 @@ class CythonRecipe(PythonRecipe): build_env = self.get_recipe_env(arch) hostpython = sh.Command(self.ctx.hostpython) if self.pre_build_ext: - try: + with suppress(Exception): shprint(hostpython, "setup.py", "build_ext", "-g", _env=build_env) - except Exception: - pass self.cythonize_build() shprint(hostpython, "setup.py", "build_ext", "-g", _env=build_env) @@ -1302,13 +1295,10 @@ pip Install a pip dependency into the distribution else: ctx = Context() for name in Recipe.list_recipes(): - try: + with suppress(Exception): recipe = Recipe.get_recipe(name, ctx) print("{recipe.name:<12} {recipe.version:<8}".format(recipe=recipe)) - except Exception: - pass - def clean(self): parser = argparse.ArgumentParser( description="Clean the build")