Skip to content

Commit 93db9c8

Browse files
author
Markus Armbruster
committed
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. Signed-off-by: Markus Armbruster <[email protected]> Message-ID: <[email protected]> Reviewed-by: Philippe Mathieu-Daudé <[email protected]>
1 parent e95ffab commit 93db9c8

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

scripts/qapi/main.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,28 @@ def create_backend(path: str) -> QAPIBackend:
3131

3232
module_path, dot, class_name = path.rpartition('.')
3333
if not dot:
34-
print("argument of -B must be of the form MODULE.CLASS",
35-
file=sys.stderr)
36-
sys.exit(1)
34+
raise QAPIError("argument of -B must be of the form MODULE.CLASS")
3735

3836
try:
3937
mod = import_module(module_path)
4038
except Exception as ex:
41-
print(f"unable to import '{module_path}': {ex}", file=sys.stderr)
42-
sys.exit(1)
39+
raise QAPIError(f"unable to import '{module_path}': {ex}") from ex
4340

4441
try:
4542
klass = getattr(mod, class_name)
46-
except AttributeError:
47-
print(f"module '{module_path}' has no class '{class_name}'",
48-
file=sys.stderr)
49-
sys.exit(1)
43+
except AttributeError as ex:
44+
raise QAPIError(
45+
f"module '{module_path}' has no class '{class_name}'") from ex
5046

5147
try:
5248
backend = klass()
5349
except Exception as ex:
54-
print(f"backend '{path}' cannot be instantiated: {ex}",
55-
file=sys.stderr)
56-
sys.exit(1)
50+
raise QAPIError(
51+
f"backend '{path}' cannot be instantiated: {ex}") from ex
5752

5853
if not isinstance(backend, QAPIBackend):
59-
print(f"backend '{path}' must be an instance of QAPIBackend",
60-
file=sys.stderr)
61-
sys.exit(1)
54+
raise QAPIError(
55+
f"backend '{path}' must be an instance of QAPIBackend")
6256

6357
return backend
6458

0 commit comments

Comments
 (0)