From bc6e7686275d6d331b4d66b882b75fd5d8888400 Mon Sep 17 00:00:00 2001 From: Carey Metcalfe Date: Fri, 13 Nov 2020 14:33:36 -0500 Subject: [PATCH] Fix exception handling Bare excepts will catch `GeneratorExit` exceptions which are raised whenever a generator stops. This was causing issues when transcoding and caching the results. All instances of bare excepts have been replaced with scoped versions. --- supysonic/api/__init__.py | 2 +- supysonic/api/media.py | 3 ++- supysonic/cache.py | 5 +++-- supysonic/jukebox.py | 4 ++-- supysonic/schema/migration/postgres/20180317.py | 2 +- supysonic/schema/migration/sqlite/20180317.py | 2 +- 6 files changed, 10 insertions(+), 8 deletions(-) diff --git a/supysonic/api/__init__.py b/supysonic/api/__init__.py index ac74d96..175e69e 100644 --- a/supysonic/api/__init__.py +++ b/supysonic/api/__init__.py @@ -41,7 +41,7 @@ def decode_password(password): try: return binascii.unhexlify(password[4:].encode("utf-8")).decode("utf-8") - except: + except ValueError: return password diff --git a/supysonic/api/media.py b/supysonic/api/media.py index cc29aab..6e5c5c2 100644 --- a/supysonic/api/media.py +++ b/supysonic/api/media.py @@ -164,7 +164,8 @@ def stream_media(): if not data: break yield data - except: # pragma: nocover + except BaseException: + # Make sure child processes are always killed if dec_proc != None: dec_proc.kill() proc.kill() diff --git a/supysonic/cache.py b/supysonic/cache.py index 7584a0d..8bcef43 100644 --- a/supysonic/cache.py +++ b/supysonic/cache.py @@ -159,9 +159,10 @@ class Cache(object): self._make_space(size, key=key) os.replace(f.name, self._filepath(key)) self._record_file(key, size) - except: + except Exception: f.close() - os.remove(f.name) + with contextlib.suppress(OSError): + os.remove(f.name) raise def set(self, key, value): diff --git a/supysonic/jukebox.py b/supysonic/jukebox.py index 0d4f0d2..2b8c49c 100644 --- a/supysonic/jukebox.py +++ b/supysonic/jukebox.py @@ -157,6 +157,6 @@ class Jukebox(object): logger.debug("Start playing with command %s", args) try: return Popen(args, stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL) - except: - logger.exception("Failed running play command") + except Exception: + logger.exception("Failed to run play command") return None diff --git a/supysonic/schema/migration/postgres/20180317.py b/supysonic/schema/migration/postgres/20180317.py index a9c3dfe..416aa97 100644 --- a/supysonic/schema/migration/postgres/20180317.py +++ b/supysonic/schema/migration/postgres/20180317.py @@ -5,7 +5,7 @@ import uuid try: bytes = buffer -except: +except NameError: pass parser = argparse.ArgumentParser() diff --git a/supysonic/schema/migration/sqlite/20180317.py b/supysonic/schema/migration/sqlite/20180317.py index 08c09ee..df97ac2 100644 --- a/supysonic/schema/migration/sqlite/20180317.py +++ b/supysonic/schema/migration/sqlite/20180317.py @@ -5,7 +5,7 @@ import uuid try: bytes = buffer -except: +except NameError: pass parser = argparse.ArgumentParser()