[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 13/16] python/machine.py: fix _popen access
From: |
John Snow |
Subject: |
[PATCH v3 13/16] python/machine.py: fix _popen access |
Date: |
Thu, 4 Jun 2020 16:22:33 -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 | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/python/qemu/machine.py b/python/qemu/machine.py
index a451f9000d6..a81a422b60e 100644
--- a/python/qemu/machine.py
+++ b/python/qemu/machine.py
@@ -114,7 +114,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.
@@ -227,6 +227,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:
@@ -237,7 +243,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:
@@ -369,7 +375,7 @@ def wait(self):
"""
Wait for the VM to power off
"""
- self._popen.wait()
+ self._subp.wait()
if self._qmp:
self._qmp.close()
self._post_shutdown()
@@ -381,8 +387,8 @@ def _hard_shutdown(self) -> None:
if not self.is_running():
return
- self._popen.kill()
- self._popen.wait(timeout=60)
+ self._subp.kill()
+ self._subp.wait(timeout=60)
def _soft_shutdown(self, has_quit: bool = False, timeout: int = 3) -> None:
"""
@@ -399,7 +405,7 @@ def _soft_shutdown(self, has_quit: bool = False, timeout:
int = 3) -> None:
self._qmp.cmd('quit')
self._qmp.close()
- self._popen.wait(timeout=timeout)
+ self._subp.wait(timeout=timeout)
def _do_shutdown(self, has_quit: bool = False, timeout: int = 3) -> None:
"""
--
2.21.3
- [PATCH v3 12/16] python/machine.py: Add _qmp access shim, (continued)
[PATCH v3 10/16] python/machine.py: Handle None events in events_wait, John Snow, 2020/06/04
[PATCH v3 11/16] python/machine.py: use qmp.command, John Snow, 2020/06/04
[PATCH v3 15/16] iotests.py: Adjust HMP kwargs typing, John Snow, 2020/06/04
[PATCH v3 13/16] python/machine.py: fix _popen access,
John Snow <=
[PATCH v3 14/16] python/qemu: make 'args' style arguments immutable, John Snow, 2020/06/04
[PATCH v3 16/16] python/qemu: Add mypy type annotations, John Snow, 2020/06/04
[PATCH v3 01/16] python/qmp.py: Define common types, John Snow, 2020/06/04
Re: [PATCH v3 00/16] python: add mypy support to python/qemu, Kevin Wolf, 2020/06/05
- Re: [PATCH v3 00/16] python: add mypy support to python/qemu, John Snow, 2020/06/08
- Re: [PATCH v3 00/16] python: add mypy support to python/qemu, Kevin Wolf, 2020/06/08
- Re: [PATCH v3 00/16] python: add mypy support to python/qemu, Philippe Mathieu-Daudé, 2020/06/08
- Re: [PATCH v3 00/16] python: add mypy support to python/qemu, Markus Armbruster, 2020/06/09
- applying mailing list review tags (was: Re: [PATCH v3 00/16] python: add mypy support to python/qemu), John Snow, 2020/06/16
- Re: applying mailing list review tags (was: Re: [PATCH v3 00/16] python: add mypy support to python/qemu), Philippe Mathieu-Daudé, 2020/06/16