Skip to content

Commit 08e43a5

Browse files
committed
Enable Ruff rule blind-except.
Note that two inline suppressions are used. I expect, provided that my [PR](astral-sh/ruff#19520) is merged in, that these suppressions will become superfluous in a later version of Ruff.
1 parent b7408cb commit 08e43a5

File tree

6 files changed

+28
-36
lines changed

6 files changed

+28
-36
lines changed

pelican/__init__.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
try:
3939
__version__ = importlib.metadata.version("pelican")
40-
except Exception:
40+
except importlib.metadata.PackageNotFoundError:
4141
__version__ = "unknown"
4242

4343
DEFAULT_CONFIG_NAME = "pelicanconf.py"
@@ -78,11 +78,10 @@ def init_plugins(self):
7878
try:
7979
plugin.register()
8080
self.plugins.append(plugin)
81-
except Exception as e:
82-
logger.error(
83-
"Cannot register plugin `%s`\n%s",
81+
except Exception:
82+
logger.exception(
83+
"Cannot register plugin `%s`",
8484
name,
85-
e,
8685
stacklevel=2,
8786
)
8887
if self.settings.get("DEBUG", False):
@@ -257,8 +256,8 @@ def __call__(self, parser, namespace, values, option_string):
257256

258257
try:
259258
instance, settings = get_instance(namespace)
260-
except Exception as e:
261-
logger.critical("%s: %s", e.__class__.__name__, e)
259+
except Exception as e: # noqa: BLE001 (blind-except)
260+
logger.critical("%s", e.__class__.__name__, exc_info=True)
262261
console.print_exception()
263262
sys.exit(getattr(e, "exitcode", 1))
264263

@@ -621,7 +620,8 @@ def listen(server, port, output, excqueue=None):
621620
except Exception as e:
622621
if excqueue is not None:
623622
excqueue.put(traceback.format_exception_only(type(e), e)[-1])
624-
return
623+
else:
624+
logging.exception("Listening aborted unexpectedly.")
625625

626626
except KeyboardInterrupt:
627627
httpd.socket.close()
@@ -679,8 +679,8 @@ def main(argv=None):
679679
pelican.run()
680680
except KeyboardInterrupt:
681681
logger.warning("Keyboard interrupt received. Exiting.")
682-
except Exception as e:
683-
logger.critical("%s: %s", e.__class__.__name__, e)
682+
except Exception as e: # noqa: BLE001 (blind-except)
683+
logger.critical("%s: %s", e.__class__.__name__, e, exc_info=True)
684684

685685
if args.verbosity == logging.DEBUG:
686686
console.print_exception()

pelican/generators.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -681,11 +681,10 @@ def generate_context(self):
681681
context_signal=signals.article_generator_context,
682682
context_sender=self,
683683
)
684-
except Exception as e:
685-
logger.error(
686-
"Could not process %s\n%s",
684+
except Exception:
685+
logger.exception(
686+
"Could not process %s",
687687
f,
688-
e,
689688
exc_info=self.settings.get("DEBUG", False),
690689
)
691690
self._add_failed_source_path(f)
@@ -896,11 +895,10 @@ def generate_context(self):
896895
context_signal=signals.page_generator_context,
897896
context_sender=self,
898897
)
899-
except Exception as e:
900-
logger.error(
901-
"Could not process %s\n%s",
898+
except Exception:
899+
logger.exception(
900+
"Could not process %s",
902901
f,
903-
e,
904902
exc_info=self.settings.get("DEBUG", False),
905903
)
906904
self._add_failed_source_path(f)

pelican/tools/pelican_themes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def install(path, v=False, u=False):
241241
f"or directory in `{theme_path}':\n{e!s}",
242242
die=False,
243243
)
244-
except Exception as e:
244+
except OSError as e:
245245
err(f"Cannot copy `{path}' to `{theme_path}':\n{e!s}")
246246

247247

@@ -262,7 +262,7 @@ def symlink(path, v=False):
262262
print(f"Linking `{path}' to `{theme_path}' ...")
263263
try:
264264
os.symlink(path, theme_path)
265-
except Exception as e:
265+
except OSError as e:
266266
err(f"Cannot link `{path}' to `{theme_path}':\n{e!s}")
267267

268268

pelican/utils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ def clean_output_dir(path: str, retention: Iterable[str]) -> None:
378378
if not os.path.isdir(path):
379379
try:
380380
os.remove(path)
381-
except Exception as e:
382-
logger.error("Unable to delete file %s; %s", path, e)
381+
except Exception:
382+
logger.exception("Unable to delete file %s", path)
383383
return
384384

385385
# remove existing content from output folder unless in retention list
@@ -393,14 +393,14 @@ def clean_output_dir(path: str, retention: Iterable[str]) -> None:
393393
try:
394394
shutil.rmtree(file)
395395
logger.debug("Deleted directory %s", file)
396-
except Exception as e:
397-
logger.error("Unable to delete directory %s; %s", file, e)
396+
except Exception:
397+
logger.exception("Unable to delete directory %s", file)
398398
elif os.path.isfile(file) or os.path.islink(file):
399399
try:
400400
os.remove(file)
401401
logger.debug("Deleted file/link %s", file)
402-
except Exception as e:
403-
logger.error("Unable to delete file %s; %s", file, e)
402+
except Exception:
403+
logger.exception("Unable to delete file %s", file)
404404
else:
405405
logger.error("Unable to delete %s, file type unknown", file)
406406

@@ -764,7 +764,7 @@ def order_content(
764764
try:
765765
content_list.sort(key=order_by)
766766
except Exception:
767-
logger.error("Error sorting with function %s", order_by)
767+
logger.exception("Error sorting with function %s", order_by)
768768
elif isinstance(order_by, str):
769769
if order_by.startswith("reversed-"):
770770
order_reversed = True

pelican/writers.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,7 @@ def write_feed(
161161
if path:
162162
complete_path = sanitised_join(self.output_path, path)
163163

164-
try:
165-
os.makedirs(os.path.dirname(complete_path))
166-
except Exception:
167-
pass
164+
os.makedirs(os.path.dirname(complete_path), exist_ok=True)
168165

169166
with self._open_w(complete_path, "utf-8", override_output) as fp:
170167
feed.write(fp, "utf-8")
@@ -215,10 +212,7 @@ def _write_file(template, localcontext, output_path, name, override):
215212
output = template.render(localcontext)
216213
path = sanitised_join(output_path, name)
217214

218-
try:
219-
os.makedirs(os.path.dirname(path))
220-
except Exception:
221-
pass
215+
os.makedirs(os.path.dirname(path), exist_ok=True)
222216

223217
with self._open_w(path, "utf-8", override=override) as f:
224218
f.write(output)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ select = [
127127
"A", # flake8-builtins
128128
"ARG", # flake8-unused-arguments
129129
"B", # flake8-bugbear
130-
# TODO: "BLE", # flake8-blind-except
130+
"BLE", # flake8-blind-except
131131
# TODO: Do I want "COM", # flake8-commas
132132
"C4", # flake8-comprehensions
133133
# TODO: "DJ", # flake8-django

0 commit comments

Comments
 (0)