Skip to contentSkip to conversation

scripts/qapi/backend: Clean up create_backend()'s failure mode

PATCH In review Markus Armbruster 2 participants 2 messages Mar 11, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts/qapi/backend: Clean up create_backend()'s failure mode

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.

---
 scripts/qapi/main.py | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
scripts/qapi/main.py +9 -15
index 5b4679abcf..0e2a6ae3f0 100644
@@ -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
 
Signature · 1 line
2.48.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Markus Armbruster

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.

Quoted 6 lines

Change it to raise QAPIError instead.

---
  scripts/qapi/main.py | 24 +++++++++---------------
  1 file changed, 9 insertions(+), 15 deletions(-)