Do not use bare `except:`

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
This commit is contained in:
Naglis Jonaitis 2024-02-06 13:17:14 +02:00 committed by Simon Ser
parent 45812720f3
commit b3c993e21b
8 changed files with 14 additions and 19 deletions

View File

@ -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>")

View File

@ -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, """

View File

@ -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,

View File

@ -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

View File

@ -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

View File

@ -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):

View File

@ -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:

View File

@ -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