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 <lukaszx.mrugala@intel.com>
This commit is contained in:
Lukasz Mrugala 2024-11-27 13:39:15 +00:00 committed by Carles Cufí
commit 0df8240b49
3 changed files with 7 additions and 15 deletions

View file

@ -770,7 +770,6 @@
"./scripts/pylib/twister/twisterlib/coverage.py" = [ "./scripts/pylib/twister/twisterlib/coverage.py" = [
"E501", # https://docs.astral.sh/ruff/rules/line-too-long "E501", # https://docs.astral.sh/ruff/rules/line-too-long
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders "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 "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting "UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
"UP032", # https://docs.astral.sh/ruff/rules/f-string "UP032", # https://docs.astral.sh/ruff/rules/f-string
@ -782,7 +781,6 @@
"./scripts/pylib/twister/twisterlib/handlers.py" = [ "./scripts/pylib/twister/twisterlib/handlers.py" = [
"E501", # https://docs.astral.sh/ruff/rules/line-too-long "E501", # https://docs.astral.sh/ruff/rules/line-too-long
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders "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 "SIM115", # https://docs.astral.sh/ruff/rules/open-file-with-context-handler
"UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation "UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes "UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes

View file

@ -3,6 +3,7 @@
# Copyright (c) 2018-2022 Intel Corporation # Copyright (c) 2018-2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
import contextlib
import glob import glob
import logging import logging
import os import os
@ -114,10 +115,8 @@ class CoverageTool:
# hence skipping it problem only in gcovr v4.1 # hence skipping it problem only in gcovr v4.1
if "kobject_hash" in filename: if "kobject_hash" in filename:
filename = (filename[:-4]) + "gcno" filename = (filename[:-4]) + "gcno"
try: with contextlib.suppress(Exception):
os.remove(filename) os.remove(filename)
except Exception:
pass
continue continue
try: try:

View file

@ -8,6 +8,7 @@
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
import argparse import argparse
import contextlib
import logging import logging
import math import math
import os import os
@ -60,10 +61,8 @@ def terminate_process(proc):
""" """
for child in psutil.Process(proc.pid).children(recursive=True): for child in psutil.Process(proc.pid).children(recursive=True):
try: with contextlib.suppress(ProcessLookupError, psutil.NoSuchProcess):
os.kill(child.pid, signal.SIGTERM) os.kill(child.pid, signal.SIGTERM)
except (ProcessLookupError, psutil.NoSuchProcess):
pass
proc.terminate() proc.terminate()
# sleep for a while before attempting to kill # sleep for a while before attempting to kill
time.sleep(0.5) time.sleep(0.5)
@ -193,10 +192,8 @@ class BinaryHandler(Handler):
pid = int(open(self.pid_fn).read()) pid = int(open(self.pid_fn).read())
os.unlink(self.pid_fn) os.unlink(self.pid_fn)
self.pid_fn = None # clear so we don't try to kill the binary twice 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) os.kill(pid, signal.SIGKILL)
except (ProcessLookupError, psutil.NoSuchProcess):
pass
def _output_reader(self, proc): def _output_reader(self, proc):
self.line = proc.stdout.readline() self.line = proc.stdout.readline()
@ -1283,11 +1280,9 @@ class QEMUWinHandler(Handler):
break break
if self.pid == 0 and os.path.exists(pid_fn): 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()) self.pid = int(open(pid_fn).read())
except ValueError:
# pid file probably not contains pid yet, continue
pass
try: try:
c = queue.get_nowait() c = queue.get_nowait()