[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 08/20] python/machine.py: fix _popen access
From: |
John Snow |
Subject: |
[PATCH 08/20] python/machine.py: fix _popen access |
Date: |
Tue, 6 Oct 2020 19:58:05 -0400 |
As always, Optional[T] causes problems with unchecked access. Add a
helper that asserts the pipe is present before we attempt to talk with
it.
Signed-off-by: John Snow <jsnow@redhat.com>
---
python/qemu/machine.py | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/python/qemu/machine.py b/python/qemu/machine.py
index 3e9cf09fd2d..4e762fcd529 100644
--- a/python/qemu/machine.py
+++ b/python/qemu/machine.py
@@ -131,7 +131,7 @@ def __init__(self, binary, args=None, wrapper=None,
name=None,
# Runstate
self._qemu_log_path = None
self._qemu_log_file = None
- self._popen = None
+ self._popen: Optional['subprocess.Popen[bytes]'] = None
self._events = []
self._iolog = None
self._qmp_set = True # Enable QMP monitor by default.
@@ -244,6 +244,12 @@ def is_running(self):
"""Returns true if the VM is running."""
return self._popen is not None and self._popen.poll() is None
+ @property
+ def _subp(self) -> 'subprocess.Popen[bytes]':
+ if self._popen is None:
+ raise QEMUMachineError('Subprocess pipe not present')
+ return self._popen
+
def exitcode(self):
"""Returns the exit code if possible, or None."""
if self._popen is None:
@@ -254,7 +260,7 @@ def get_pid(self):
"""Returns the PID of the running process, or None."""
if not self.is_running():
return None
- return self._popen.pid
+ return self._subp.pid
def _load_io_log(self):
if self._qemu_log_path is not None:
@@ -415,8 +421,8 @@ def _hard_shutdown(self) -> None:
waiting for the QEMU process to terminate.
"""
self._early_cleanup()
- self._popen.kill()
- self._popen.wait(timeout=60)
+ self._subp.kill()
+ self._subp.wait(timeout=60)
def _soft_shutdown(self, timeout: Optional[int],
has_quit: bool = False) -> None:
@@ -440,7 +446,7 @@ def _soft_shutdown(self, timeout: Optional[int],
self._qmp.cmd('quit')
# May raise subprocess.TimeoutExpired
- self._popen.wait(timeout=timeout)
+ self._subp.wait(timeout=timeout)
def _do_shutdown(self, timeout: Optional[int],
has_quit: bool = False) -> None:
--
2.26.2
- Re: [PATCH 03/20] python/machine.py: reorder __init__, (continued)
- [PATCH 02/20] python/machine.py: Fix monitor address typing, John Snow, 2020/10/06
- [PATCH 04/20] python/machine.py: Don't modify state in _base_args(), John Snow, 2020/10/06
- [PATCH 05/20] python/machine.py: Handle None events in events_wait, John Snow, 2020/10/06
- [PATCH 06/20] python/machine.py: use qmp.command, John Snow, 2020/10/06
- [PATCH 07/20] python/machine.py: Add _qmp access shim, John Snow, 2020/10/06
- [PATCH 08/20] python/machine.py: fix _popen access,
John Snow <=
[PATCH 09/20] python/qemu: make 'args' style arguments immutable, John Snow, 2020/10/06
[PATCH 10/20] iotests.py: Adjust HMP kwargs typing, John Snow, 2020/10/06
[PATCH 11/20] python/qemu: Add mypy type annotations, John Snow, 2020/10/06