qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] scripts/qapi/backend: Clean up create_backend()'s failure mode
@ 2025-03-11  6:53 Markus Armbruster
  2025-03-11  7:22 ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 2+ messages in thread
From: Markus Armbruster @ 2025-03-11  6:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: michael.roth, berrange, jsnow

create_backend()'s caller catches QAPIError, and returns non-zero exit
code on catch.  The caller's caller passes the exit code to
sys.exit().

create_backend() doesn't care: it reports errors to stderr and
sys.exit()s.

Change it to raise QAPIError instead.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 scripts/qapi/main.py | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)

diff --git a/scripts/qapi/main.py b/scripts/qapi/main.py
index 5b4679abcf..0e2a6ae3f0 100644
--- a/scripts/qapi/main.py
+++ b/scripts/qapi/main.py
@@ -31,34 +31,28 @@ def create_backend(path: str) -> QAPIBackend:
 
     module_path, dot, class_name = path.rpartition('.')
     if not dot:
-        print("argument of -B must be of the form MODULE.CLASS",
-              file=sys.stderr)
-        sys.exit(1)
+        raise QAPIError("argument of -B must be of the form MODULE.CLASS")
 
     try:
         mod = import_module(module_path)
     except Exception as ex:
-        print(f"unable to import '{module_path}': {ex}", file=sys.stderr)
-        sys.exit(1)
+        raise QAPIError(f"unable to import '{module_path}': {ex}") from ex
 
     try:
         klass = getattr(mod, class_name)
-    except AttributeError:
-        print(f"module '{module_path}' has no class '{class_name}'",
-              file=sys.stderr)
-        sys.exit(1)
+    except AttributeError as ex:
+        raise QAPIError(
+            f"module '{module_path}' has no class '{class_name}'") from ex
 
     try:
         backend = klass()
     except Exception as ex:
-        print(f"backend '{path}' cannot be instantiated: {ex}",
-              file=sys.stderr)
-        sys.exit(1)
+        raise QAPIError(
+            f"backend '{path}' cannot be instantiated: {ex}") from ex
 
     if not isinstance(backend, QAPIBackend):
-        print(f"backend '{path}' must be an instance of QAPIBackend",
-              file=sys.stderr)
-        sys.exit(1)
+        raise QAPIError(
+            f"backend '{path}' must be an instance of QAPIBackend")
 
     return backend
 
-- 
2.48.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] scripts/qapi/backend: Clean up create_backend()'s failure mode
  2025-03-11  6:53 [PATCH] scripts/qapi/backend: Clean up create_backend()'s failure mode Markus Armbruster
@ 2025-03-11  7:22 ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 2+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-03-11  7:22 UTC (permalink / raw)
  To: Markus Armbruster, qemu-devel; +Cc: michael.roth, berrange, jsnow

On 11/3/25 07:53, Markus Armbruster wrote:
> create_backend()'s caller catches QAPIError, and returns non-zero exit
> code on catch.  The caller's caller passes the exit code to
> sys.exit().
> 
> create_backend() doesn't care: it reports errors to stderr and
> sys.exit()s.
> 
> Change it to raise QAPIError instead.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>   scripts/qapi/main.py | 24 +++++++++---------------
>   1 file changed, 9 insertions(+), 15 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-03-11  7:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-11  6:53 [PATCH] scripts/qapi/backend: Clean up create_backend()'s failure mode Markus Armbruster
2025-03-11  7:22 ` Philippe Mathieu-Daudé

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).