From 587a6f30e207794045db3e322f42c8cdb4b30174 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Thu, 22 Jun 2023 11:46:53 +1000 Subject: [PATCH] pylint: Fix places that raise an overly broad exception pylint 2.17.2 (Fedora 38) complains about places that raise an overly broad exception, such as bare 'Exception' rather than a derived class. Change most places that do that to raise an appropriate derived class. One remaining place appears to be deliberately raising the generic exception, so use a pylint suppression there instead. Signed-off-by: David Gibson --- avocado/utils/external/gdbmi_parser.py | 10 +++++++--- avocado/utils/gdb.py | 4 +++- avocado/utils/kernel.py | 2 +- examples/tests/multiple_tests.py | 2 +- examples/tests/uncaught_exception.py | 2 +- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/avocado/utils/external/gdbmi_parser.py b/avocado/utils/external/gdbmi_parser.py index 5e33816699..927351378f 100644 --- a/avocado/utils/external/gdbmi_parser.py +++ b/avocado/utils/external/gdbmi_parser.py @@ -30,6 +30,10 @@ from avocado.utils.external import spark +class GdbMiException(Exception): + pass + + class Token: def __init__(self, token_type, value=None): self.type = token_type @@ -126,7 +130,7 @@ def t_c_string(self, s): def t_default(self, s): # pylint: disable=W0221 r"( . | \n )+" - raise Exception(f"Specification error: unmatched input for '{s}'") + raise GdbMiException(f"Specification error: unmatched input for '{s}'") @staticmethod def __unescape(s): @@ -200,7 +204,7 @@ def nonterminal(self, token_type, args): def error(self, token, i=0, tokens=None): # pylint: disable=W0221 if i > 2: print(f"{tokens[i - 3]} {tokens[i - 2]} " f"{tokens[i - 1]} {tokens[i]}") - raise Exception(f"Syntax error at or near {int(i)}:'{token}' token") + raise GdbMiException(f"Syntax error at or near {int(i)}:'{token}' token") class GdbMiInterpreter(spark.GenericASTTraversal): @@ -249,7 +253,7 @@ def n_tuple(node): else: node.value[n] = v else: - raise Exception("Invalid tuple") + raise GdbMiException("Invalid tuple") # print 'tuple: %s' % node.value @staticmethod diff --git a/avocado/utils/gdb.py b/avocado/utils/gdb.py index 3e93cda113..629448360b 100644 --- a/avocado/utils/gdb.py +++ b/avocado/utils/gdb.py @@ -397,7 +397,9 @@ def cmd(self, command): if result_response_received: # raise an exception here, because no two result # responses should come from a single command AFAIK - raise Exception("Many result responses to a single cmd") + raise UnexpectedResponseError( + "Many result responses to a single cmd" + ) result_response_received = True cmd.result = parsed_response else: diff --git a/avocado/utils/kernel.py b/avocado/utils/kernel.py index 1badacce91..878e61e198 100644 --- a/avocado/utils/kernel.py +++ b/avocado/utils/kernel.py @@ -119,7 +119,7 @@ def uncompress(self): LOG.info("Uncompressing tarball") archive.extract(self.asset_path, self.work_dir) else: - raise Exception("Unable to find the tarball") + raise RuntimeError("Unable to find the tarball") def configure(self, targets=("defconfig"), extra_configs=None): """ diff --git a/examples/tests/multiple_tests.py b/examples/tests/multiple_tests.py index aabf441db9..826962d707 100644 --- a/examples/tests/multiple_tests.py +++ b/examples/tests/multiple_tests.py @@ -30,4 +30,4 @@ def action(): """ This method should never execute """ - raise Exception("This action method should never be executed.") + raise RuntimeError("This action method should never be executed.") diff --git a/examples/tests/uncaught_exception.py b/examples/tests/uncaught_exception.py index 3336607008..e1414e9343 100644 --- a/examples/tests/uncaught_exception.py +++ b/examples/tests/uncaught_exception.py @@ -14,4 +14,4 @@ def test(): """ This should end with ERROR. """ - raise Exception("This is a generic exception") + raise Exception("This is a generic exception") # pylint: disable=W0719