[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 61/72] scripts/qmp-shell: Use context manager instead of atexit
From: |
John Snow |
Subject: |
[PULL 61/72] scripts/qmp-shell: Use context manager instead of atexit |
Date: |
Fri, 18 Jun 2021 19:04:44 -0400 |
We can invoke the shell history writing when we leave the QMPShell scope
instead of relying on atexit. Doing so may be preferable to avoid global
state being registered from within a class instead of from the
application logic directly.
Use QMP's context manager to hook this history saving at close time,
which gets invoked when we leave the context block.
Signed-off-by: John Snow <jsnow@redhat.com>
Message-id: 20210607200649.1840382-32-jsnow@redhat.com
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qmp/qmp-shell | 33 ++++++++++++++++++---------------
1 file changed, 18 insertions(+), 15 deletions(-)
diff --git a/scripts/qmp/qmp-shell b/scripts/qmp/qmp-shell
index f14fe211cc..ec028d662e 100755
--- a/scripts/qmp/qmp-shell
+++ b/scripts/qmp/qmp-shell
@@ -66,7 +66,6 @@
# sent to QEMU, which is useful for debugging and documentation generation.
import argparse
import ast
-import atexit
import json
import os
import re
@@ -142,6 +141,11 @@ class QMPShell(qmp.QEMUMonitorProtocol):
self.pretty = pretty
self.verbose = verbose
+ def close(self) -> None:
+ # Hook into context manager of parent to save shell history.
+ self._save_history()
+ super().close()
+
def _fill_completion(self) -> None:
cmds = self.cmd('query-commands')
if 'error' in cmds:
@@ -164,9 +168,8 @@ class QMPShell(qmp.QEMUMonitorProtocol):
pass
except IOError as err:
print(f"Failed to read history '{self._histfile}': {err!s}")
- atexit.register(self.__save_history)
- def __save_history(self) -> None:
+ def _save_history(self) -> None:
try:
readline.write_history_file(self._histfile)
except IOError as err:
@@ -448,25 +451,25 @@ def main() -> None:
parser.error("QMP socket or TCP address must be specified")
shell_class = HMPShell if args.hmp else QMPShell
+
try:
address = shell_class.parse_address(args.qmp_server)
except qmp.QMPBadPortError:
parser.error(f"Bad port number: {args.qmp_server}")
return # pycharm doesn't know error() is noreturn
- qemu = shell_class(address, args.pretty, args.verbose)
+ with shell_class(address, args.pretty, args.verbose) as qemu:
+ try:
+ qemu.connect(negotiate=not args.skip_negotiation)
+ except qmp.QMPConnectError:
+ die("Didn't get QMP greeting message")
+ except qmp.QMPCapabilitiesError:
+ die("Couldn't negotiate capabilities")
+ except OSError as err:
+ die(f"Couldn't connect to {args.qmp_server}: {err!s}")
- try:
- qemu.connect(negotiate=not args.skip_negotiation)
- except qmp.QMPConnectError:
- die("Didn't get QMP greeting message")
- except qmp.QMPCapabilitiesError:
- die("Couldn't negotiate capabilities")
- except OSError as err:
- die(f"Couldn't connect to {args.qmp_server}: {err!s}")
-
- for _ in qemu.repl():
- pass
+ for _ in qemu.repl():
+ pass
if __name__ == '__main__':
--
2.31.1
- [PULL 52/72] scripts/qmp-shell: move the REPL functionality into QMPShell, (continued)
- [PULL 52/72] scripts/qmp-shell: move the REPL functionality into QMPShell, John Snow, 2021/06/18
- [PULL 47/72] scripts/qmp-shell: use argparse, John Snow, 2021/06/18
- [PULL 45/72] scripts/qmp-shell: remove if-raise-else patterns, John Snow, 2021/06/18
- [PULL 55/72] scripts/qmp-shell: initialize completer early, John Snow, 2021/06/18
- [PULL 56/72] python/qmp: add QMPObject type alias, John Snow, 2021/06/18
- [PULL 57/72] scripts/qmp-shell: add mypy types, John Snow, 2021/06/18
- [PULL 59/72] scripts/qmp-shell: unprivatize 'pretty' property, John Snow, 2021/06/18
- [PULL 60/72] python/qmp: return generic type from context manager, John Snow, 2021/06/18
- [PULL 64/72] scripts/qmp-shell: Fix empty-transaction invocation, John Snow, 2021/06/18
- [PULL 63/72] scripts/qmp-shell: remove TODO, John Snow, 2021/06/18
- [PULL 61/72] scripts/qmp-shell: Use context manager instead of atexit,
John Snow <=
- [PULL 66/72] scripts/qmp-shell: convert usage comment to docstring, John Snow, 2021/06/18
- [PULL 68/72] scripts/qmp-shell: make QMPShellError inherit QMPError, John Snow, 2021/06/18
- [PULL 70/72] scripts/qmp-shell: move to python/qemu/qmp/qmp_shell.py, John Snow, 2021/06/18
- [PULL 72/72] scripts/qmp-shell: add redirection shim, John Snow, 2021/06/18
- [PULL 62/72] scripts/qmp-shell: use logging to show warnings, John Snow, 2021/06/18
- [PULL 65/72] scripts/qmp-shell: Remove too-broad-exception, John Snow, 2021/06/18
- [PULL 67/72] scripts/qmp-shell: remove double-underscores, John Snow, 2021/06/18
- [PULL 69/72] scripts/qmp-shell: add docstrings, John Snow, 2021/06/18
- [PULL 71/72] python: add qmp-shell entry point, John Snow, 2021/06/18
- [PULL 58/72] scripts/qmp-shell: Accept SocketAddrT instead of string, John Snow, 2021/06/18