From 10c0c2917199d069a61a1d8abb0c3afc1704e38e Mon Sep 17 00:00:00 2001 From: Ilya Simpson Date: Thu, 24 Jul 2025 19:46:47 +1200 Subject: [PATCH] Enable Ruff rule `blind-except`. Note that two inline suppressions are used. I expect, provided that my [PR](https://github.com/astral-sh/ruff/pull/19520) is merged in, that these suppressions will become superfluous in a later version of Ruff. --- .pre-commit-config.yaml | 2 +- pelican/__init__.py | 16 ++++++++-------- pelican/generators.py | 14 ++++++-------- pelican/tools/pelican_themes.py | 4 ++-- pelican/utils.py | 14 +++++++------- pelican/writers.py | 10 ++-------- pyproject.toml | 4 ++-- 7 files changed, 28 insertions(+), 36 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 38287df9..b5291afb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,7 +16,7 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit # ruff version should match the one in pyproject.toml - rev: v0.12.2 + rev: v0.12.7 hooks: - id: ruff-check args: [--fix, --exit-non-zero-on-fix] diff --git a/pelican/__init__.py b/pelican/__init__.py index 8d916fc7..ead3fa7a 100644 --- a/pelican/__init__.py +++ b/pelican/__init__.py @@ -37,7 +37,7 @@ from pelican.writers import Writer try: __version__ = importlib.metadata.version("pelican") -except Exception: +except importlib.metadata.PackageNotFoundError: __version__ = "unknown" DEFAULT_CONFIG_NAME = "pelicanconf.py" @@ -78,11 +78,10 @@ class Pelican: try: plugin.register() self.plugins.append(plugin) - except Exception as e: - logger.error( - "Cannot register plugin `%s`\n%s", + except Exception: + logger.exception( + "Cannot register plugin `%s`", name, - e, stacklevel=2, ) if self.settings.get("DEBUG", False): @@ -258,7 +257,7 @@ class PrintSettings(argparse.Action): try: instance, settings = get_instance(namespace) except Exception as e: - logger.critical("%s: %s", e.__class__.__name__, e) + logger.critical("%s", e.__class__.__name__, exc_info=True) console.print_exception() sys.exit(getattr(e, "exitcode", 1)) @@ -621,7 +620,8 @@ def listen(server, port, output, excqueue=None): except Exception as e: if excqueue is not None: excqueue.put(traceback.format_exception_only(type(e), e)[-1]) - return + else: + logging.exception("Listening aborted unexpectedly.") except KeyboardInterrupt: httpd.socket.close() @@ -680,7 +680,7 @@ def main(argv=None): except KeyboardInterrupt: logger.warning("Keyboard interrupt received. Exiting.") except Exception as e: - logger.critical("%s: %s", e.__class__.__name__, e) + logger.critical("%s: %s", e.__class__.__name__, e, exc_info=True) if args.verbosity == logging.DEBUG: console.print_exception() diff --git a/pelican/generators.py b/pelican/generators.py index e04d1733..bf1d1cb8 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -681,11 +681,10 @@ class ArticlesGenerator(CachingGenerator): context_signal=signals.article_generator_context, context_sender=self, ) - except Exception as e: - logger.error( - "Could not process %s\n%s", + except Exception: + logger.exception( + "Could not process %s", f, - e, exc_info=self.settings.get("DEBUG", False), ) self._add_failed_source_path(f) @@ -896,11 +895,10 @@ class PagesGenerator(CachingGenerator): context_signal=signals.page_generator_context, context_sender=self, ) - except Exception as e: - logger.error( - "Could not process %s\n%s", + except Exception: + logger.exception( + "Could not process %s", f, - e, exc_info=self.settings.get("DEBUG", False), ) self._add_failed_source_path(f) diff --git a/pelican/tools/pelican_themes.py b/pelican/tools/pelican_themes.py index 1ae707e3..29fd7a30 100755 --- a/pelican/tools/pelican_themes.py +++ b/pelican/tools/pelican_themes.py @@ -241,7 +241,7 @@ def install(path, v=False, u=False): f"or directory in `{theme_path}':\n{e!s}", die=False, ) - except Exception as e: + except OSError as e: err(f"Cannot copy `{path}' to `{theme_path}':\n{e!s}") @@ -262,7 +262,7 @@ def symlink(path, v=False): print(f"Linking `{path}' to `{theme_path}' ...") try: os.symlink(path, theme_path) - except Exception as e: + except OSError as e: err(f"Cannot link `{path}' to `{theme_path}':\n{e!s}") diff --git a/pelican/utils.py b/pelican/utils.py index eb281ce9..0dd36a1f 100644 --- a/pelican/utils.py +++ b/pelican/utils.py @@ -378,8 +378,8 @@ def clean_output_dir(path: str, retention: Iterable[str]) -> None: if not os.path.isdir(path): try: os.remove(path) - except Exception as e: - logger.error("Unable to delete file %s; %s", path, e) + except Exception: + logger.exception("Unable to delete file %s", path) return # remove existing content from output folder unless in retention list @@ -393,14 +393,14 @@ def clean_output_dir(path: str, retention: Iterable[str]) -> None: try: shutil.rmtree(file) logger.debug("Deleted directory %s", file) - except Exception as e: - logger.error("Unable to delete directory %s; %s", file, e) + except Exception: + logger.exception("Unable to delete directory %s", file) elif os.path.isfile(file) or os.path.islink(file): try: os.remove(file) logger.debug("Deleted file/link %s", file) - except Exception as e: - logger.error("Unable to delete file %s; %s", file, e) + except Exception: + logger.exception("Unable to delete file %s", file) else: logger.error("Unable to delete %s, file type unknown", file) @@ -764,7 +764,7 @@ def order_content( try: content_list.sort(key=order_by) except Exception: - logger.error("Error sorting with function %s", order_by) + logger.exception("Error sorting with function %s", order_by) elif isinstance(order_by, str): if order_by.startswith("reversed-"): order_reversed = True diff --git a/pelican/writers.py b/pelican/writers.py index 1a99532a..009d761a 100644 --- a/pelican/writers.py +++ b/pelican/writers.py @@ -161,10 +161,7 @@ class Writer: if path: complete_path = sanitised_join(self.output_path, path) - try: - os.makedirs(os.path.dirname(complete_path)) - except Exception: - pass + os.makedirs(os.path.dirname(complete_path), exist_ok=True) with self._open_w(complete_path, "utf-8", override_output) as fp: feed.write(fp, "utf-8") @@ -215,10 +212,7 @@ class Writer: output = template.render(localcontext) path = sanitised_join(output_path, name) - try: - os.makedirs(os.path.dirname(path)) - except Exception: - pass + os.makedirs(os.path.dirname(path), exist_ok=True) with self._open_w(path, "utf-8", override=override) as f: f.write(output) diff --git a/pyproject.toml b/pyproject.toml index 8d0fd9b9..863fde08 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -96,7 +96,7 @@ dev = [ "tox>=4.11.3", "invoke>=2.2.0", # ruff version should match the one in .pre-commit-config.yaml - "ruff==0.12.2", + "ruff==0.12.7", "tomli>=2.0.1; python_version < \"3.11\"", ] @@ -127,7 +127,7 @@ select = [ "A", # flake8-builtins "ARG", # flake8-unused-arguments "B", # flake8-bugbear - # TODO: "BLE", # flake8-blind-except + "BLE", # flake8-blind-except # TODO: Do I want "COM", # flake8-commas "C4", # flake8-comprehensions # TODO: "DJ", # flake8-django