According to PEP8[1]:
> A bare `except:` clause will catch SystemExit and KeyboardInterrupt
> exceptions, making it harder to interrupt a program with Control-C,
> and can disguise other problems.
Use more specific exceptions or `contextlib.suppress()` in case of
`try: ... except: pass`.
[1]: https://peps.python.org/pep-0008/#programming-recommendations
---
gitsrht/blueprints/api/__init__.py | 2 +-
gitsrht/blueprints/api/info.py | 2 +-
gitsrht/blueprints/manage.py | 2 +-
gitsrht/blueprints/repo.py | 6 +++---
gitsrht/editorconfig.py | 4 ++--
gitsrht/git.py | 2 +-
gitsrht/repos.py | 6 ++----
scripts/symlink-update-hook.py | 9 +++------
8 files changed, 14 insertions(+), 19 deletions(-)
diff --git a/gitsrht/blueprints/api/__init__.py b/gitsrht/blueprints/api/__init__.py
index 2217d67..f0f901b 100644
--- a/gitsrht/blueprints/api/__init__.py
+++ b/gitsrht/blueprints/api/__init__.py
@@ -37,7 +37,7 @@ def register_api(app):
try:
dist = pkg_resources.get_distribution("gitsrht")
return { "version": dist.version }
- except:
+ except Exception:
return { "version": "unknown" }
@app.route("/api/user/<username>")
diff --git a/gitsrht/blueprints/api/info.py b/gitsrht/blueprints/api/info.py
index afc4573..e17cfd0 100644
--- a/gitsrht/blueprints/api/info.py
+++ b/gitsrht/blueprints/api/info.py
@@ -172,7 +172,7 @@ def repos_by_name_readme_PUT(reponame):
readme = None
try:
readme = request.data.decode("utf-8")
- except:
+ except ValueError:
return valid.error("README files must be UTF-8 encoded", field="body")
resp = exec_gql(current_app.site, """
diff --git a/gitsrht/blueprints/manage.py b/gitsrht/blueprints/manage.py
index 0c2af54..eb9a96a 100644
--- a/gitsrht/blueprints/manage.py
+++ b/gitsrht/blueprints/manage.py
@@ -201,7 +201,7 @@ def settings_access_POST(owner_name, repo_name):
username = username[1:]
try:
user = current_app.oauth_service.lookup_user(username)
- except:
+ except Exception:
user = None
valid.expect(user, "User not found.", field="user")
valid.expect(not user or user.id != current_user.id,
diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py
index 1bc86c2..11418c3 100644
--- a/gitsrht/blueprints/repo.py
+++ b/gitsrht/blueprints/repo.py
@@ -281,7 +281,7 @@ def tree(owner, repo, ref, path):
if not blob.is_binary:
try:
data = blob.data.decode()
- except:
+ except ValueError:
data = '[unable to decode]'
md = not blob.is_binary and entry.name.endswith(".md")
if md:
@@ -427,7 +427,7 @@ def blame(owner, repo, ref, path):
path="/".join(path)))
try:
data = blob.data.decode()
- except:
+ except ValueError:
return redirect(url_for("repo.log",
owner=repo.owner.canonical_name, repo=repo.name, ref=ref,
path="/".join(path)))
@@ -669,7 +669,7 @@ def refs(owner, repo):
try:
page = int(page) - 1
tags = tags[page*results_per_page:page*results_per_page+results_per_page]
- except:
+ except Exception:
page = 0
else:
page = 0
diff --git a/gitsrht/editorconfig.py b/gitsrht/editorconfig.py
index 272033f..3c848f6 100644
--- a/gitsrht/editorconfig.py
+++ b/gitsrht/editorconfig.py
@@ -8,7 +8,7 @@ class EditorConfig:
self.repo = repo
self.tree = tree
self._config = self._config_for(path)
- except:
+ except Exception:
self._config = None
def _config_for(self, path):
@@ -41,7 +41,7 @@ class EditorConfig:
# gross
config.read_string("[__root__]\n" + blob.data.decode())
break
- except:
+ except Exception:
config = None
if not config:
return None
diff --git a/gitsrht/git.py b/gitsrht/git.py
index 3c67241..a84ef0c 100644
--- a/gitsrht/git.py
+++ b/gitsrht/git.py
@@ -28,7 +28,7 @@ def signature_time(signature):
tzaware = datetime.fromtimestamp(float(signature.time), tzinfo)
diff = datetime.now(timezone.utc) - tzaware
return datetime.utcnow() - diff
- except:
+ except Exception:
return datetime.utcnow()
def commit_time(commit):
diff --git a/gitsrht/repos.py b/gitsrht/repos.py
index ef79728..44d0ab8 100644
--- a/gitsrht/repos.py
+++ b/gitsrht/repos.py
@@ -1,3 +1,4 @@
+import contextlib
import hashlib
import os.path
import pygit2
@@ -54,11 +55,8 @@ def upload_artifact(valid, repo, commit, f, filename):
secret_key=s3_secret_key, secure=s3_secure)
prefix = os.path.join(s3_prefix, "artifacts",
repo.owner.canonical_name, repo.name)
- try:
+ with contextlib.suppress(S3Error):
minio.make_bucket(s3_bucket)
- except:
- # Thanks for not giving us more specific exceptions, minio
- pass
sha = hashlib.sha256()
buf = f.read(1024)
while len(buf) > 0:
diff --git a/scripts/symlink-update-hook.py b/scripts/symlink-update-hook.py
index 6c8413d..a3a8438 100755
--- a/scripts/symlink-update-hook.py
+++ b/scripts/symlink-update-hook.py
@@ -4,6 +4,7 @@ from srht.database import DbSession
db = DbSession(cfg("git.sr.ht", "connection-string"))
from gitsrht.types import Repository
db.init()
+import contextlib
import os
post_update = cfg("git.sr.ht", "post-update-script")
@@ -12,14 +13,10 @@ def migrate(path, link):
if not os.path.exists(path) \
or not os.path.islink(path) \
or os.readlink(path) != link:
- try:
+ with contextlib.suppress(Exception):
os.remove(path)
- except:
- pass
- try:
+ with contextlib.suppress(Exception):
os.symlink(link, path)
- except:
- pass
return True
return False
--
2.43.0