From 0df8240b4915168617f4795b79b98eeba5a8d20b Mon Sep 17 00:00:00 2001 From: Lukasz Mrugala Date: Wed, 27 Nov 2024 13:39:15 +0000 Subject: [PATCH] scripts: Fix twisterlib for ruff - SIM105 This fixes ruff linting error SIM105, where try-except-pass construct was used instead of contextlib.suppress(). Signed-off-by: Lukasz Mrugala --- .ruff-excludes.toml | 2 -- scripts/pylib/twister/twisterlib/coverage.py | 5 ++--- scripts/pylib/twister/twisterlib/handlers.py | 15 +++++---------- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/.ruff-excludes.toml b/.ruff-excludes.toml index dbf3cb4a90b..45f271d5854 100644 --- a/.ruff-excludes.toml +++ b/.ruff-excludes.toml @@ -770,7 +770,6 @@ "./scripts/pylib/twister/twisterlib/coverage.py" = [ "E501", # https://docs.astral.sh/ruff/rules/line-too-long "F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders - "SIM105", # https://docs.astral.sh/ruff/rules/suppressible-exception "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes "UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting "UP032", # https://docs.astral.sh/ruff/rules/f-string @@ -782,7 +781,6 @@ "./scripts/pylib/twister/twisterlib/handlers.py" = [ "E501", # https://docs.astral.sh/ruff/rules/line-too-long "F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders - "SIM105", # https://docs.astral.sh/ruff/rules/suppressible-exception "SIM115", # https://docs.astral.sh/ruff/rules/open-file-with-context-handler "UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes diff --git a/scripts/pylib/twister/twisterlib/coverage.py b/scripts/pylib/twister/twisterlib/coverage.py index c135d19e22d..31ac94dc26c 100644 --- a/scripts/pylib/twister/twisterlib/coverage.py +++ b/scripts/pylib/twister/twisterlib/coverage.py @@ -3,6 +3,7 @@ # Copyright (c) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 +import contextlib import glob import logging import os @@ -114,10 +115,8 @@ class CoverageTool: # hence skipping it problem only in gcovr v4.1 if "kobject_hash" in filename: filename = (filename[:-4]) + "gcno" - try: + with contextlib.suppress(Exception): os.remove(filename) - except Exception: - pass continue try: diff --git a/scripts/pylib/twister/twisterlib/handlers.py b/scripts/pylib/twister/twisterlib/handlers.py index 7af897b1cc3..d7f10a6b3c6 100755 --- a/scripts/pylib/twister/twisterlib/handlers.py +++ b/scripts/pylib/twister/twisterlib/handlers.py @@ -8,6 +8,7 @@ # SPDX-License-Identifier: Apache-2.0 import argparse +import contextlib import logging import math import os @@ -60,10 +61,8 @@ def terminate_process(proc): """ for child in psutil.Process(proc.pid).children(recursive=True): - try: + with contextlib.suppress(ProcessLookupError, psutil.NoSuchProcess): os.kill(child.pid, signal.SIGTERM) - except (ProcessLookupError, psutil.NoSuchProcess): - pass proc.terminate() # sleep for a while before attempting to kill time.sleep(0.5) @@ -193,10 +192,8 @@ class BinaryHandler(Handler): pid = int(open(self.pid_fn).read()) os.unlink(self.pid_fn) self.pid_fn = None # clear so we don't try to kill the binary twice - try: + with contextlib.suppress(ProcessLookupError, psutil.NoSuchProcess): os.kill(pid, signal.SIGKILL) - except (ProcessLookupError, psutil.NoSuchProcess): - pass def _output_reader(self, proc): self.line = proc.stdout.readline() @@ -1283,11 +1280,9 @@ class QEMUWinHandler(Handler): break if self.pid == 0 and os.path.exists(pid_fn): - try: + # pid file probably not contains pid yet, continue + with contextlib.suppress(ValueError): self.pid = int(open(pid_fn).read()) - except ValueError: - # pid file probably not contains pid yet, continue - pass try: c = queue.get_nowait()