Skip to contentSkip to conversation

python: add mypy support to python/qemu

PATCH Superseded John Snow 4 participants 28 messages Jun 2, 2020 – Jun 4, 2020

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

John Snow

author
#0 lore raw

v2 cover python: add mypy support to python/qemu

Requires: [email protected]

This series is extracted from my larger series that attempted to bundle our python module as an installable module. These fixes don't require that, so they are being sent first as I think there's less up for debate in here.

This requires my "refactor shutdown" patch as a pre-requisite.

"v2":
- This version supports iotests 297
- Many patches merged by Phil are removed
- Replaces iotests.py type aliases with centralized ones (See patch 2)
- Imports etc are reworked to use the non-installable package layout instead. (Mostly important for patch 3)

Testing this out:
- You'll need Python3.6+
- I encourage you to use a virtual environment!
- You don't necessarily need these exact versions, but I didn't test the lower bounds, use older versions at your peril:
- pylint==2.5.0
- mypy=0.770
- flake8=3.7.8

Quoted 5 lines

cd ~/src/qemu/python/
flake8 qemu
mypy --strict qemu
cd qemu
pylint *.py

These should all 100% pass.

---

Open RFC: What's the right way to hook this into make check to prevent regressions?

John Snow (16):
python/qmp.py: Define common types iotests.py: use qemu.qmp type aliases python/qmp.py: re-absorb MonitorResponseError python/qmp.py: Do not return None from cmd_obj python/qmp.py: add casts to JSON deserialization python/qmp.py: add QMPProtocolError python/machine.py: Fix monitor address typing python/machine.py: reorder __init__ python/machine.py: Don't modify state in _base_args() python/machine.py: Handle None events in event_wait python/machine.py: use qmp.command python/machine.py: Add _qmp access shim python/machine.py: fix _popen access python/qemu: make 'args' style arguments immutable iotests.py: Adjust HMP kwargs typing python/qemu: Add mypy type annotations

 python/qemu/accel.py          |   8 +-
 python/qemu/machine.py        | 267 ++++++++++++++++++++--------------
 python/qemu/qmp.py            | 111 ++++++++++----
 python/qemu/qtest.py          |  54 ++++---
 scripts/render_block_graph.py |   7 +-
 tests/qemu-iotests/iotests.py |  11 +-
 6 files changed, 285 insertions(+), 173 deletions(-)
Signature · 1 line
2.21.3
author
#1 lore raw

PATCH v2 1/16 python/qmp.py: Define common types

Define some common types that we'll need to annotate a lot of other functions going forward.

---
 python/qemu/qmp.py | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
python/qemu/qmp.py +18 -0
index e64b6b5faa7..8388c7b6030 100644
@@ -12,13 +12,31 @@
 import socket
 import logging
 from typing import (
+    Any,
+    Dict,
     Optional,
     TextIO,
     Type,
+    Tuple,
+    Union,
 )
 from types import TracebackType
 
 
+# QMPMessage is a QMP Message of any kind.
+# e.g. {'yee': 'haw'}
+#
+# QMPReturnValue is the inner value of return values only.
+# {'return': {}} is the QMPMessage,
+# {} is the QMPReturnValue.
+QMPMessage = Dict[str, Any]
+QMPReturnValue = Dict[str, Any]
+
+InternetAddrT = Tuple[str, str]
+UnixAddrT = str
+SocketAddrT = Union[InternetAddrT, UnixAddrT]
+
+
 class QMPError(Exception):
     """
     QMP base exception
Signature · 1 line
2.21.3
author
#2 lore raw

PATCH v2 2/16 iotests.py: use qemu.qmp type aliases

In reply to John Snow

iotests.py should use the type definitions from qmp.py instead of its own.

---
 tests/qemu-iotests/iotests.py | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
tests/qemu-iotests/iotests.py +3 -6
index f20d90f9698..7c1773bba37 100644
@@ -35,13 +35,10 @@
 # pylint: disable=import-error, wrong-import-position
 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
 from qemu import qtest
+from qemu.qmp import QMPMessage
 
 assert sys.version_info >= (3, 6)
 
-# Type Aliases
-QMPResponse = Dict[str, Any]
-
-
 # Use this logger for logging messages directly from the iotests module
 logger = logging.getLogger('qemu.iotests')
 logger.addHandler(logging.NullHandler())
@@ -554,7 +551,7 @@ def add_incoming(self, addr):
         self._args.append(addr)
         return self
 
-    def hmp(self, command_line: str, use_log: bool = False) -> QMPResponse:
+    def hmp(self, command_line: str, use_log: bool = False) -> QMPMessage:
         cmd = 'human-monitor-command'
         kwargs = {'command-line': command_line}
         if use_log:
@@ -575,7 +572,7 @@ def resume_drive(self, drive: str) -> None:
         self.hmp(f'qemu-io {drive} "remove_break bp_{drive}"')
 
     def hmp_qemu_io(self, drive: str, cmd: str,
-                    use_log: bool = False) -> QMPResponse:
+                    use_log: bool = False) -> QMPMessage:
         """Write to a given drive using an HMP command"""
         return self.hmp(f'qemu-io {drive} "{cmd}"', use_log=use_log)
 
Signature · 1 line
2.21.3
author
#3 lore raw

PATCH v2 3/16 python/qmp.py: re-absorb MonitorResponseError

In reply to John Snow

When I initially split this out, I considered this more of a machine error than a QMP protocol error, but I think that's misguided.

Move this back to qmp.py and name it QMPResponseError. Convert qmp.command() to use this exception type.

---
 python/qemu/machine.py        | 15 +--------------
 python/qemu/qmp.py            | 17 +++++++++++++++--
 scripts/render_block_graph.py |  7 +++++--
 3 files changed, 21 insertions(+), 18 deletions(-)
python/qemu/machine.py +1 -14
index a2abd2c35e3..b2b7ae9fbf9 100644
@@ -48,19 +48,6 @@ class QEMUMachineAddDeviceError(QEMUMachineError):
     """
 
 
-class MonitorResponseError(qmp.QMPError):
-    """
-    Represents erroneous QMP monitor reply
-    """
-    def __init__(self, reply):
-        try:
-            desc = reply["error"]["desc"]
-        except KeyError:
-            desc = reply
-        super().__init__(desc)
-        self.reply = reply
-
-
 class QEMUMachine:
     """
     A QEMU VM
@@ -464,7 +451,7 @@ def command(self, cmd, conv_keys=True, **args):
         if reply is None:
             raise qmp.QMPError("Monitor is closed")
         if "error" in reply:
-            raise MonitorResponseError(reply)
+            raise qmp.QMPResponseError(reply)
         return reply["return"]
 
     def get_qmp_event(self, wait=False):
python/qemu/qmp.py +15 -2
index 8388c7b6030..aa8a666b8ab 100644
@@ -61,6 +61,19 @@ class QMPTimeoutError(QMPError):
     """
 
 
+class QMPResponseError(QMPError):
+    """
+    Represents erroneous QMP monitor reply
+    """
+    def __init__(self, reply: QMPMessage):
+        try:
+            desc = reply['error']['desc']
+        except KeyError:
+            desc = reply
+        super().__init__(desc)
+        self.reply = reply
+
+
 class QEMUMonitorProtocol:
     """
     Provide an API to connect to QEMU via QEMU Monitor Protocol (QMP) and then
@@ -251,8 +264,8 @@ def command(self, cmd, **kwds):
         Build and send a QMP command to the monitor, report errors if any
         """
         ret = self.cmd(cmd, kwds)
-        if "error" in ret:
-            raise Exception(ret['error']['desc'])
+        if 'error' in ret:
+            raise QMPResponseError(ret)
         return ret['return']
 
     def pull_event(self, wait=False):
scripts/render_block_graph.py +5 -2
index 409b4321f2e..da6acf050d1 100755
@@ -25,7 +25,10 @@
 from graphviz import Digraph
 
 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'python'))
-from qemu.machine import MonitorResponseError
+from qemu.qmp import (
+    QEMUMonitorProtocol,
+    QMPResponseError,
+)
 
 
 def perm(arr):
@@ -102,7 +105,7 @@ def command(self, cmd):
         reply = json.loads(subprocess.check_output(ar))
 
         if 'error' in reply:
-            raise MonitorResponseError(reply)
+            raise QMPResponseError(reply)
 
         return reply['return']
 
Signature · 1 line
2.21.3
author
#4 lore raw

PATCH v2 4/16 python/qmp.py: Do not return None from cmd_obj

In reply to John Snow

This makes typing the qmp library difficult, as it necessitates wrapping Optional[] around the type for every return type up the stack. At some point, it becomes difficult to discern or remember why it's None instead of the expected object.

Use the python exception system to tell us exactly why we didn't get an object. Remove this special-cased return.

---
 python/qemu/qmp.py | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
python/qemu/qmp.py +5 -9
index aa8a666b8ab..ef3c919b76c 100644
@@ -225,22 +225,18 @@ def accept(self, timeout=15.0):
         self.__sockfile = self.__sock.makefile(mode='r')
         return self.__negotiate_capabilities()
 
-    def cmd_obj(self, qmp_cmd):
+    def cmd_obj(self, qmp_cmd: QMPMessage) -> QMPMessage:
         """
         Send a QMP command to the QMP Monitor.
 
         @param qmp_cmd: QMP command to be sent as a Python dict
-        @return QMP response as a Python dict or None if the connection has
-                been closed
+        @return QMP response as a Python dict
         """
         self.logger.debug(">>> %s", qmp_cmd)
-        try:
-            self.__sock.sendall(json.dumps(qmp_cmd).encode('utf-8'))
-        except OSError as err:
-            if err.errno == errno.EPIPE:
-                return None
-            raise err
+        self.__sock.sendall(json.dumps(qmp_cmd).encode('utf-8'))
         resp = self.__json_read()
+        if resp is None:
+            raise QMPConnectError("Unexpected empty reply from server")
         self.logger.debug("<<< %s", resp)
         return resp
 
Signature · 1 line
2.21.3
author
#5 lore raw

PATCH v2 5/16 python/qmp.py: add casts to JSON deserialization

In reply to John Snow

mypy and python type hints are not powerful enough to properly describe JSON messages in Python 3.6. The best we can do, generally, is describe them as Dict[str, Any].

Add casts to coerce this type for static analysis; but do NOT enforce this type at runtime in any way.

Note: Python 3.8 adds a TypedDict construct which allows for the description of more arbitrary Dictionary shapes. There is a third-party module, "Pydantic", which is compatible with 3.6 that can be used instead of the JSON library that parses JSON messages to fully-typed Python objects, and may be preferable in some cases.

(That is well beyond the scope of this commit or series.)

---
 python/qemu/qmp.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
python/qemu/qmp.py +6 -2
index ef3c919b76c..5f3558e3066 100644
@@ -13,6 +13,7 @@
 import logging
 from typing import (
     Any,
+    cast,
     Dict,
     Optional,
     TextIO,
@@ -130,7 +131,10 @@ def __json_read(self, only_event=False):
             data = self.__sockfile.readline()
             if not data:
                 return None
-            resp = json.loads(data)
+            # By definition, any JSON received from QMP is a QMPMessage,
+            # and we are asserting only at static analysis time that it
+            # has a particular shape.
+            resp = cast(QMPMessage, json.loads(data))
             if 'event' in resp:
                 self.logger.debug("<<< %s", resp)
                 self.__events.append(resp)
@@ -262,7 +266,7 @@ def command(self, cmd, **kwds):
         ret = self.cmd(cmd, kwds)
         if 'error' in ret:
             raise QMPResponseError(ret)
-        return ret['return']
+        return cast(QMPReturnValue, ret['return'])
 
     def pull_event(self, wait=False):
         """
Signature · 1 line
2.21.3
#6 lore raw

Re: [PATCH v2 05/16] python/qmp.py: add casts to JSON deserialization

Am 02.06.2020 um 23:45 hat John Snow geschrieben:

Quoted 33 lines

mypy and python type hints are not powerful enough to properly describe JSON messages in Python 3.6. The best we can do, generally, is describe them as Dict[str, Any].

Add casts to coerce this type for static analysis; but do NOT enforce this type at runtime in any way.

Note: Python 3.8 adds a TypedDict construct which allows for the description of more arbitrary Dictionary shapes. There is a third-party module, "Pydantic", which is compatible with 3.6 that can be used instead of the JSON library that parses JSON messages to fully-typed Python objects, and may be preferable in some cases.

(That is well beyond the scope of this commit or series.)

---
 python/qemu/qmp.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
python/qemu/qmp.py +1 -0
index ef3c919b76c..5f3558e3066 100644
@@ -13,6 +13,7 @@
 import logging
 from typing import (
     Any,
+    cast,
     Dict,
     Optional,
     TextIO,

python/qemu/qmp.py def __json_read(self, only_event=False): · line 137

Quoted 8 lines
python/qemu/qmp.py +4 -1
@@ -130,7 +131,10 @@ def __json_read(self, only_event=False):
             data = self.__sockfile.readline()
             if not data:
                 return None
-            resp = json.loads(data)
+            # By definition, any JSON received from QMP is a QMPMessage,
+            # and we are asserting only at static analysis time that it
+            # has a particular shape.
+            resp = cast(QMPMessage, json.loads(data))

Instead of casting, you can just specify the variable type:

resp: QMPMessage = json.loads(data)

I don't think that json.loads() will actually return something other than Any anytime soon, but it's generally nicer to avoid casts and if it eventually does change, we'll get the type check instead of silencing it.

python/qemu/qmp.py def command(self, cmd, **kwds): · line 269

Quoted 9 lines
python/qemu/qmp.py +1 -1
             if 'event' in resp:
                 self.logger.debug("<<< %s", resp)
                 self.__events.append(resp)
@@ -262,7 +266,7 @@ def command(self, cmd, **kwds):
         ret = self.cmd(cmd, kwds)
         if 'error' in ret:
             raise QMPResponseError(ret)
-        return ret['return']
+        return cast(QMPReturnValue, ret['return'])

This one can't be easily avoided, though.

Kevin

author
#7 lore raw

Re: [PATCH v2 05/16] python/qmp.py: add casts to JSON deserialization

Quoted 45 lines · Kevin Wolf

Am 02.06.2020 um 23:45 hat John Snow geschrieben:

Quoted 33 lines

mypy and python type hints are not powerful enough to properly describe JSON messages in Python 3.6. The best we can do, generally, is describe them as Dict[str, Any].

Add casts to coerce this type for static analysis; but do NOT enforce this type at runtime in any way.

Note: Python 3.8 adds a TypedDict construct which allows for the description of more arbitrary Dictionary shapes. There is a third-party module, "Pydantic", which is compatible with 3.6 that can be used instead of the JSON library that parses JSON messages to fully-typed Python objects, and may be preferable in some cases.

(That is well beyond the scope of this commit or series.)

---
 python/qemu/qmp.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
python/qemu/qmp.py +1 -0
index ef3c919b76c..5f3558e3066 100644
@@ -13,6 +13,7 @@
 import logging
 from typing import (
     Any,
+    cast,
     Dict,
     Optional,
     TextIO,

python/qemu/qmp.py def __json_read(self, only_event=False): · line 137

Quoted 8 lines
python/qemu/qmp.py +4 -1
@@ -130,7 +131,10 @@ def __json_read(self, only_event=False):
             data = self.__sockfile.readline()
             if not data:
                 return None
-            resp = json.loads(data)
+            # By definition, any JSON received from QMP is a QMPMessage,
+            # and we are asserting only at static analysis time that it
+            # has a particular shape.
+            resp = cast(QMPMessage, json.loads(data))

Instead of casting, you can just specify the variable type:

Quoted 7 lines

resp: QMPMessage = json.loads(data)

I don't think that json.loads() will actually return something other than Any anytime soon, but it's generally nicer to avoid casts and if it eventually does change, we'll get the type check instead of silencing it.

Nice, that works with --strict too. Done.

Quoted 12 lines

python/qemu/qmp.py def command(self, cmd, **kwds): · line 269

Quoted 9 lines
python/qemu/qmp.py +1 -1
             if 'event' in resp:
                 self.logger.debug("<<< %s", resp)
                 self.__events.append(resp)
@@ -262,7 +266,7 @@ def command(self, cmd, **kwds):
         ret = self.cmd(cmd, kwds)
         if 'error' in ret:
             raise QMPResponseError(ret)
-        return ret['return']
+        return cast(QMPReturnValue, ret['return'])

This one can't be easily avoided, though.

Sadly not -- we could conceivably make use of e.g. pydantic to create arbitrarily nuanced/strict structure definitions that are actually validated at runtime, but I will save that for a future series.

--js

author
#8 lore raw

PATCH v2 6/16 python/qmp.py: add QMPProtocolError

In reply to John Snow

In the case that we receive a reply but are unable to understand it, use this exception name to indicate that case.

---
 python/qemu/qmp.py | 10 ++++++++++
 1 file changed, 10 insertions(+)
python/qemu/qmp.py +10 -0
index 5f3558e3066..1aefc00c93b 100644
@@ -62,6 +62,12 @@ class QMPTimeoutError(QMPError):
     """
 
 
+class QMPProtocolError(QMPError):
+    """
+    QMP protocol error; unexpected response
+    """
+
+
 class QMPResponseError(QMPError):
     """
     Represents erroneous QMP monitor reply
@@ -266,6 +272,10 @@ def command(self, cmd, **kwds):
         ret = self.cmd(cmd, kwds)
         if 'error' in ret:
             raise QMPResponseError(ret)
+        if 'return' not in ret:
+            raise QMPProtocolError(
+                "'return' key not found in QMP response '{}'".format(str(ret))
+            )
         return cast(QMPReturnValue, ret['return'])
 
     def pull_event(self, wait=False):
Signature · 1 line
2.21.3
author
#9 lore raw

PATCH v2 7/16 python/machine.py: Fix monitor address typing

In reply to John Snow

Prior to this, it's difficult for mypy to intuit what the concrete type of the monitor address is; it has difficulty inferring the type across two variables.

Create _monitor_address as a property that always returns a valid address to simply static type analysis.

To preserve our ability to clean up, use a simple boolean to indicate whether or not we should try to clean up the sock file after execution.

---
 python/qemu/machine.py | 45 +++++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 16 deletions(-)
python/qemu/machine.py +29 -16
index b2b7ae9fbf9..e8d7f9e4298 100644
@@ -24,10 +24,14 @@
 import shutil
 import socket
 import tempfile
-from typing import Optional, Type
+from typing import (
+    Optional,
+    Type,
+)
 from types import TracebackType
 
 from . import qmp
+from .qmp import SocketAddrT
 
 LOG = logging.getLogger(__name__)
 
@@ -61,7 +65,8 @@ class QEMUMachine:
     """
 
     def __init__(self, binary, args=None, wrapper=None, name=None,
-                 test_dir="/var/tmp", monitor_address=None,
+                 test_dir="/var/tmp",
+                 monitor_address: Optional[SocketAddrT] = None,
                  socket_scm_helper=None, sock_dir=None):
         '''
         Initialize a QEMUMachine
@@ -84,8 +89,14 @@ def __init__(self, binary, args=None, wrapper=None, name=None,
         if sock_dir is None:
             sock_dir = test_dir
         self._name = name
-        self._monitor_address = monitor_address
-        self._vm_monitor = None
+        if monitor_address is not None:
+            self._monitor_address = monitor_address
+            self._remove_monitor_sockfile = False
+        else:
+            self._monitor_address = os.path.join(
+                sock_dir, f"{name}-monitor.sock"
+            )
+            self._remove_monitor_sockfile = True
         self._qemu_log_path = None
         self._qemu_log_file = None
         self._popen = None
@@ -223,15 +234,17 @@ def _load_io_log(self):
 
     def _base_args(self):
         args = ['-display', 'none', '-vga', 'none']
+
         if self._qmp_set:
             if isinstance(self._monitor_address, tuple):
-                moncdev = "socket,id=mon,host=%s,port=%s" % (
-                    self._monitor_address[0],
-                    self._monitor_address[1])
+                moncdev = "socket,id=mon,host={},port={}".format(
+                    *self._monitor_address
+                )
             else:
-                moncdev = 'socket,id=mon,path=%s' % self._vm_monitor
+                moncdev = f"socket,id=mon,path={self._monitor_address}"
             args.extend(['-chardev', moncdev, '-mon',
                          'chardev=mon,mode=control'])
+
         if self._machine is not None:
             args.extend(['-machine', self._machine])
         for _ in range(self._console_index):
@@ -256,14 +269,14 @@ def _pre_launch(self):
         self._qemu_log_file = open(self._qemu_log_path, 'wb')
 
         if self._qmp_set:
-            if self._monitor_address is not None:
-                self._vm_monitor = self._monitor_address
-            else:
-                self._vm_monitor = os.path.join(self._sock_dir,
-                                                self._name + "-monitor.sock")
-                self._remove_files.append(self._vm_monitor)
-            self._qmp = qmp.QEMUMonitorProtocol(self._vm_monitor, server=True,
-                                                nickname=self._name)
+            if self._remove_monitor_sockfile:
+                assert isinstance(self._monitor_address, str)
+                self._remove_files.append(self._monitor_address)
+            self._qmp = qmp.QEMUMonitorProtocol(
+                self._monitor_address,
+                server=True,
+                nickname=self._name
+            )
 
     def _post_launch(self):
         if self._qmp:
Signature · 1 line
2.21.3
author
#10 lore raw

PATCH v2 8/16 python/machine.py: reorder __init__

In reply to John Snow

Put the init arg handling all at the top, and mostly in order (deviating when one is dependent on another), and put what is effectively runtime state declaration at the bottom.

---
 python/qemu/machine.py | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)
python/qemu/machine.py +17 -12
index e8d7f9e4298..6b7d188b951 100644
@@ -80,38 +80,43 @@ def __init__(self, binary, args=None, wrapper=None, name=None,
         @param socket_scm_helper: helper program, required for send_fd_scm()
         @note: Qemu process is not started until launch() is used.
         '''
+        # Direct user configuration
+
+        self._binary = binary
+
         if args is None:
             args = []
+        # Copy mutable input: we will be modifying our copy
+        self._args = list(args)
+
         if wrapper is None:
             wrapper = []
-        if name is None:
-            name = "qemu-%d" % os.getpid()
-        if sock_dir is None:
-            sock_dir = test_dir
-        self._name = name
+        self._wrapper = wrapper
+
+        self._name = name or "qemu-%d" % os.getpid()
+        self._test_dir = test_dir
+        self._sock_dir = sock_dir or self._test_dir
+        self._socket_scm_helper = socket_scm_helper
+
         if monitor_address is not None:
             self._monitor_address = monitor_address
             self._remove_monitor_sockfile = False
         else:
             self._monitor_address = os.path.join(
-                sock_dir, f"{name}-monitor.sock"
+                self._sock_dir, f"{self._name}-monitor.sock"
             )
             self._remove_monitor_sockfile = True
+
+        # Runstate
         self._qemu_log_path = None
         self._qemu_log_file = None
         self._popen = None
-        self._binary = binary
-        self._args = list(args)     # Force copy args in case we modify them
-        self._wrapper = wrapper
         self._events = []
         self._iolog = None
-        self._socket_scm_helper = socket_scm_helper
         self._qmp_set = True   # Enable QMP monitor by default.
         self._qmp = None
         self._qemu_full_args = None
-        self._test_dir = test_dir
         self._temp_dir = None
-        self._sock_dir = sock_dir
         self._launched = False
         self._machine = None
         self._console_index = 0
Signature · 1 line
2.21.3
author
#11 lore raw

PATCH v2 9/16 python/machine.py: Don't modify state in _base_args()

In reply to John Snow

Don't append to the _remove_files list during _base_args; instead do so during _launch. Rework _base_args as a @property to help facilitate this impression.

This has the additional benefit of making the type of _console_address easier to analyze statically.

---
 python/qemu/machine.py | 16 ++++++++++------
 python/qemu/qtest.py   | 11 ++++++++---
 2 files changed, 18 insertions(+), 9 deletions(-)
python/qemu/machine.py +10 -6
index 6b7d188b951..2f12cebde40 100644
@@ -25,6 +25,7 @@
 import socket
 import tempfile
 from typing import (
+    List,
     Optional,
     Type,
 )
@@ -122,7 +123,9 @@ def __init__(self, binary, args=None, wrapper=None, name=None,
         self._console_index = 0
         self._console_set = False
         self._console_device_type = None
-        self._console_address = None
+        self._console_address = os.path.join(
+            self._sock_dir, f"{self._name}-console.sock"
+        )
         self._console_socket = None
         self._remove_files = []
 
@@ -237,7 +240,8 @@ def _load_io_log(self):
             with open(self._qemu_log_path, "r") as iolog:
                 self._iolog = iolog.read()
 
-    def _base_args(self):
+    @property
+    def _base_args(self) -> List[str]:
         args = ['-display', 'none', '-vga', 'none']
 
         if self._qmp_set:
@@ -255,9 +259,6 @@ def _base_args(self):
         for _ in range(self._console_index):
             args.extend(['-serial', 'null'])
         if self._console_set:
-            self._console_address = os.path.join(self._sock_dir,
-                                                 self._name + "-console.sock")
-            self._remove_files.append(self._console_address)
             chardev = ('socket,id=console,path=%s,server,nowait' %
                        self._console_address)
             args.extend(['-chardev', chardev])
@@ -273,6 +274,9 @@ def _pre_launch(self):
         self._qemu_log_path = os.path.join(self._temp_dir, self._name + ".log")
         self._qemu_log_file = open(self._qemu_log_path, 'wb')
 
+        if self._console_set:
+            self._remove_files.append(self._console_address)
+
         if self._qmp_set:
             if self._remove_monitor_sockfile:
                 assert isinstance(self._monitor_address, str)
@@ -345,7 +349,7 @@ def _launch(self):
         devnull = open(os.path.devnull, 'rb')
         self._pre_launch()
         self._qemu_full_args = (self._wrapper + [self._binary] +
-                                self._base_args() + self._args)
+                                self._base_args + self._args)
         LOG.debug('VM launch command: %r', ' '.join(self._qemu_full_args))
         self._popen = subprocess.Popen(self._qemu_full_args,
                                        stdin=devnull,
python/qemu/qtest.py +8 -3
index 888c8bd2f60..05c63a1d583 100644
@@ -19,7 +19,11 @@
 
 import socket
 import os
-from typing import Optional, TextIO
+from typing import (
+    List,
+    Optional,
+    TextIO,
+)
 
 from .machine import QEMUMachine
 
@@ -111,8 +115,9 @@ def __init__(self, binary, args=None, name=None, test_dir="/var/tmp",
         self._qtest = None
         self._qtest_path = os.path.join(sock_dir, name + "-qtest.sock")
 
-    def _base_args(self):
-        args = super()._base_args()
+    @property
+    def _base_args(self) -> List[str]:
+        args = super()._base_args
         args.extend(['-qtest', 'unix:path=' + self._qtest_path,
                      '-accel', 'qtest'])
         return args
Signature · 1 line
2.21.3
author
#12 lore raw

PATCH v2 10/16 python/machine.py: Handle None events in event_wait

In reply to John Snow

If the timeout is 0, we can get None back. Handle this explicitly.

---
 python/qemu/machine.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
python/qemu/machine.py +5 -1
index 2f12cebde40..a835b7550da 100644
@@ -32,7 +32,7 @@
 from types import TracebackType
 
 from . import qmp
-from .qmp import SocketAddrT
+from .qmp import SocketAddrT, QMPMessage
 
 LOG = logging.getLogger(__name__)
 
@@ -553,6 +553,8 @@ def _match(event):
                     return True
             return False
 
+        event: Optional[QMPMessage]
+
         # Search cached events
         for event in self._events:
             if _match(event):
@@ -562,6 +564,8 @@ def _match(event):
         # Poll for new events
         while True:
             event = self._qmp.pull_event(wait=timeout)
+            if event is None:
+                break
             if _match(event):
                 return event
             self._events.append(event)
Signature · 1 line
2.21.3
#13 lore raw

Re: [PATCH v2 10/16] python/machine.py: Handle None events in event_wait

Am 02.06.2020 um 23:45 hat John Snow geschrieben:

Quoted 3 lines

If the timeout is 0, we can get None back. Handle this explicitly.

Subject line: This is events_wait(), not event_wait(). Both functions exist.

python/qemu/machine.py def _match(event): · line 571

Quoted 9 lines
python/qemu/machine.py +2 -0
@@ -562,6 +564,8 @@ def _match(event):
         # Poll for new events
         while True:
             event = self._qmp.pull_event(wait=timeout)
+            if event is None:
+                break
             if _match(event):
                 return event
             self._events.append(event)

Hm... How could this ever work? I guess we just never really tested whether timeouts actually time out?

(It's still somewhat unintuitive that receiving an unrelated event resets the timeout, but not the problem of this series...)

Kevin

author
#14 lore raw

Re: [PATCH v2 10/16] python/machine.py: Handle None events in event_wait

Quoted 8 lines · Kevin Wolf

Am 02.06.2020 um 23:45 hat John Snow geschrieben:

Quoted 3 lines

If the timeout is 0, we can get None back. Handle this explicitly.

Subject line: This is events_wait(), not event_wait(). Both functions exist.

Quoted 13 lines

python/qemu/machine.py def _match(event): · line 571

Quoted 9 lines
python/qemu/machine.py +2 -0
@@ -562,6 +564,8 @@ def _match(event):
         # Poll for new events
         while True:
             event = self._qmp.pull_event(wait=timeout)
+            if event is None:
+                break
             if _match(event):
                 return event
             self._events.append(event)

Hm... How could this ever work? I guess we just never really tested whether timeouts actually time out?

It's weirder than you think.

pull_event, when a timeout is supplied, will pass that timeout to QEMUMonitorProtocol.__get_events, which populates the received event queue but does not return data on the stack. If there are no events in the queue and we are given a timeout, we will raise QMPTimeoutError if no event appears in that timeframe.

pull_event() will only return None in the case that you don't give it a timeout or tell it to wait.

The typing of events_wait allows us to specify a timeout of 0 here, which is treated as no-wait down the stack, which may return None if there was no such event ready.

Thus, break and also return None.

(I should update the docstring here.)

Quoted 5 lines

(It's still somewhat unintuitive that receiving an unrelated event resets the timeout, but not the problem of this series...)

Kevin

Yes, it's weird. It's not a timeout on this one event, it's a timeout on event-receiving activity. In practice, it works quite well for the iotest suite where there are often not other event drivers present. It was a quick hack (that I introduced!) to ensure that iotests would halt in some finite amount of time.

--js

author
#15 lore raw

PATCH v2 11/16 python/machine.py: use qmp.command

In reply to John Snow

machine.py and qmp.py both do the same thing here; refactor machine.py to use qmp.py's functionality more directly.

---
 python/qemu/machine.py | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)
python/qemu/machine.py +15 -11
index a835b7550da..5add7a28eaf 100644
@@ -25,6 +25,8 @@
 import socket
 import tempfile
 from typing import (
+    Any,
+    Dict,
     List,
     Optional,
     Type,
@@ -450,17 +452,23 @@ def set_qmp_monitor(self, enabled=True):
             self._qmp_set = False
             self._qmp = None
 
-    def qmp(self, cmd, conv_keys=True, **args):
-        """
-        Invoke a QMP command and return the response dict
-        """
+    @classmethod
+    def _qmp_args(cls, _conv_keys: bool = True, **args: Any) -> Dict[str, Any]:
         qmp_args = dict()
         for key, value in args.items():
-            if conv_keys:
+            if _conv_keys:
                 qmp_args[key.replace('_', '-')] = value
             else:
                 qmp_args[key] = value
+        return qmp_args
 
+    def qmp(self, cmd: str,
+            conv_keys: bool = True,
+            **args: Any) -> QMPMessage:
+        """
+        Invoke a QMP command and return the response dict
+        """
+        qmp_args = self._qmp_args(conv_keys, **args)
         return self._qmp.cmd(cmd, args=qmp_args)
 
     def command(self, cmd, conv_keys=True, **args):
@@ -469,12 +477,8 @@ def command(self, cmd, conv_keys=True, **args):
         On success return the response dict.
         On failure raise an exception.
         """
-        reply = self.qmp(cmd, conv_keys, **args)
-        if reply is None:
-            raise qmp.QMPError("Monitor is closed")
-        if "error" in reply:
-            raise qmp.QMPResponseError(reply)
-        return reply["return"]
+        qmp_args = self._qmp_args(conv_keys, **args)
+        return self._qmp.command(cmd, **qmp_args)
 
     def get_qmp_event(self, wait=False):
         """
Signature · 1 line
2.21.3
#16 lore raw
In reply to John Snow
Quoted 1 line · John Snow

Requires: [email protected]

I don't know if patchew understand that, or if it requires:

Based-on: [email protected]

Quoted 30 lines

This series is extracted from my larger series that attempted to bundle our python module as an installable module. These fixes don't require that, so they are being sent first as I think there's less up for debate in here.

This requires my "refactor shutdown" patch as a pre-requisite.

"v2":
- This version supports iotests 297
- Many patches merged by Phil are removed
- Replaces iotests.py type aliases with centralized ones (See patch 2)
- Imports etc are reworked to use the non-installable package layout instead. (Mostly important for patch 3)

Testing this out:
- You'll need Python3.6+
- I encourage you to use a virtual environment!
- You don't necessarily need these exact versions, but I didn't test the lower bounds, use older versions at your peril:
- pylint==2.5.0
- mypy=0.770
- flake8=3.7.8

Quoted 5 lines

cd ~/src/qemu/python/
flake8 qemu
mypy --strict qemu
cd qemu
pylint *.py

Quoted 6 lines

These should all 100% pass.

---

Open RFC: What's the right way to hook this into make check to prevent regressions?

We recently added iotest 297 in group meta; is there a way to run './check -g meta' alongside the other iotests that 'make check' already triggers?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
author
#17 lore raw
Quoted 41 lines · Eric Blake
Quoted 1 line · John Snow

Requires: [email protected]

I don't know if patchew understand that, or if it requires:

Based-on: [email protected]

Quoted 34 lines

This series is extracted from my larger series that attempted to bundle our python module as an installable module. These fixes don't require that,
so they are being sent first as I think there's less up for debate in here.

This requires my "refactor shutdown" patch as a pre-requisite.

"v2":
- This version supports iotests 297
- Many patches merged by Phil are removed
- Replaces iotests.py type aliases with centralized ones (See patch 2)
- Imports etc are reworked to use the non-installable package layout instead. (Mostly important for patch 3)

Testing this out:
- You'll need Python3.6+
- I encourage you to use a virtual environment!
- You don't necessarily need these exact versions, but I didn't test the lower bounds, use older versions at your peril:
   - pylint==2.5.0
   - mypy=0.770
   - flake8=3.7.8

Quoted 5 lines

cd ~/src/qemu/python/
flake8 qemu
mypy --strict qemu
cd qemu
pylint *.py

These should all 100% pass.

Quoted 9 lines
Quoted 4 lines

---

Open RFC: What's the right way to hook this into make check to prevent regressions?

We recently added iotest 297 in group meta; is there a way to run './check -g meta' alongside the other iotests that 'make check' already triggers?

If we want to distribute any of this code independently of qemu.git (And I think we do), I think relying on the iotests infrastructure will hurt more than it will help.

I think I should try to maintain some independence of this folder from the rest of the QEMU base; so it should be able to run the linting tests under its own power.

(So, I guess, a Makefile?)

but there's further problems: this infrastructure is 3.6+ only, but the build system only requires 3.5+. It has to be an optional testing target that executes only when it's possible to. It also requires additional dependencies not checked for in configure -- mypy, pylint, and flake8.

I am wondering if there's a nice way to create a check target that builds a virtual environment with pinned dependencies, and then uses that to run the lint tests. As long as the machine you're running on has at least python3.6+ it should be able to run the tests.

I just don't really have a plan yet...

--js

author
#18 lore raw

PATCH v2 12/16 python/machine.py: Add _qmp access shim

In reply to John Snow

Like many other Optional[] types, it's not always a given that this object will be set. Wrap it in a type-shim that raises a meaningful error and will always return a concrete type.

---
 python/qemu/machine.py | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
python/qemu/machine.py +9 -3
index 5add7a28eaf..9c45bdfb1e4 100644
@@ -117,7 +117,7 @@ def __init__(self, binary, args=None, wrapper=None, name=None,
         self._events = []
         self._iolog = None
         self._qmp_set = True   # Enable QMP monitor by default.
-        self._qmp = None
+        self._qmp_connection: Optional[qmp.QEMUMonitorProtocol] = None
         self._qemu_full_args = None
         self._temp_dir = None
         self._launched = False
@@ -283,7 +283,7 @@ def _pre_launch(self):
             if self._remove_monitor_sockfile:
                 assert isinstance(self._monitor_address, str)
                 self._remove_files.append(self._monitor_address)
-            self._qmp = qmp.QEMUMonitorProtocol(
+            self._qmp_connection = qmp.QEMUMonitorProtocol(
                 self._monitor_address,
                 server=True,
                 nickname=self._name
@@ -450,7 +450,13 @@ def set_qmp_monitor(self, enabled=True):
             self._qmp_set = True
         else:
             self._qmp_set = False
-            self._qmp = None
+            self._qmp_connection = None
+
+    @property
+    def _qmp(self) -> qmp.QEMUMonitorProtocol:
+        if self._qmp_connection is None:
+            raise QEMUMachineError("Attempt to access QMP with no connection")
+        return self._qmp_connection
 
     @classmethod
     def _qmp_args(cls, _conv_keys: bool = True, **args: Any) -> Dict[str, Any]:
Signature · 1 line
2.21.3
author
#19 lore raw

PATCH v2 13/16 python/machine.py: fix _popen access

In reply to John Snow

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.

---
 python/qemu/machine.py | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
python/qemu/machine.py +12 -6
index 9c45bdfb1e4..64197fc8492 100644
@@ -113,7 +113,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.
@@ -225,6 +225,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:
@@ -235,7 +241,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:
@@ -365,7 +371,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()
@@ -377,8 +383,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:
         """
@@ -395,7 +401,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:
         """
Signature · 1 line
2.21.3
author
#20 lore raw

PATCH v2 14/16 python/qemu: make 'args' style arguments immutable

In reply to John Snow

These arguments don't need to be mutable and aren't really used as such. Clarify their types as immutable and adjust code to match where necessary.

In general, It's probably best not to accept a user-defined mutable object and store it as internal object state unless there's a strong justification for doing so. Instead, try to use generic types as input with empty tuples as the default, and coerce to list where necessary.

---
 python/qemu/machine.py | 30 +++++++++++++++++-------------
 python/qemu/qtest.py   | 16 ++++++++++++----
 2 files changed, 29 insertions(+), 17 deletions(-)
python/qemu/machine.py +17 -13
index 64197fc8492..d32e5599ce3 100644
@@ -18,6 +18,7 @@
 #
 
 import errno
+from itertools import chain
 import logging
 import os
 import subprocess
@@ -29,6 +30,8 @@
     Dict,
     List,
     Optional,
+    Sequence,
+    Tuple,
     Type,
 )
 from types import TracebackType
@@ -67,8 +70,12 @@ class QEMUMachine:
         # vm is guaranteed to be shut down here
     """
 
-    def __init__(self, binary, args=None, wrapper=None, name=None,
-                 test_dir="/var/tmp",
+    def __init__(self,
+                 binary: str,
+                 args: Sequence[str] = (),
+                 wrapper: Sequence[str] = (),
+                 name: Optional[str] = None,
+                 test_dir: str = "/var/tmp",
                  monitor_address: Optional[SocketAddrT] = None,
                  socket_scm_helper=None, sock_dir=None):
         '''
@@ -86,14 +93,7 @@ def __init__(self, binary, args=None, wrapper=None, name=None,
         # Direct user configuration
 
         self._binary = binary
-
-        if args is None:
-            args = []
-        # Copy mutable input: we will be modifying our copy
         self._args = list(args)
-
-        if wrapper is None:
-            wrapper = []
         self._wrapper = wrapper
 
         self._name = name or "qemu-%d" % os.getpid()
@@ -118,7 +118,7 @@ def __init__(self, binary, args=None, wrapper=None, name=None,
         self._iolog = None
         self._qmp_set = True   # Enable QMP monitor by default.
         self._qmp_connection: Optional[qmp.QEMUMonitorProtocol] = None
-        self._qemu_full_args = None
+        self._qemu_full_args: Tuple[str, ...] = ()
         self._temp_dir = None
         self._launched = False
         self._machine = None
@@ -336,7 +336,7 @@ def launch(self):
             raise QEMUMachineError('VM already launched')
 
         self._iolog = None
-        self._qemu_full_args = None
+        self._qemu_full_args = ()
         try:
             self._launch()
             self._launched = True
@@ -356,8 +356,12 @@ def _launch(self):
         """
         devnull = open(os.path.devnull, 'rb')
         self._pre_launch()
-        self._qemu_full_args = (self._wrapper + [self._binary] +
-                                self._base_args + self._args)
+        self._qemu_full_args = tuple(
+            chain(self._wrapper,
+                  [self._binary],
+                  self._base_args,
+                  self._args)
+        )
         LOG.debug('VM launch command: %r', ' '.join(self._qemu_full_args))
         self._popen = subprocess.Popen(self._qemu_full_args,
                                        stdin=devnull,
python/qemu/qtest.py +12 -4
index 05c63a1d583..ae4661d4d3e 100644
@@ -22,6 +22,7 @@
 from typing import (
     List,
     Optional,
+    Sequence,
     TextIO,
 )
 
@@ -103,8 +104,13 @@ class QEMUQtestMachine(QEMUMachine):
     A QEMU VM, with a qtest socket available.
     """
 
-    def __init__(self, binary, args=None, name=None, test_dir="/var/tmp",
-                 socket_scm_helper=None, sock_dir=None):
+    def __init__(self,
+                 binary: str,
+                 args: Sequence[str] = (),
+                 name: Optional[str] = None,
+                 test_dir: str = "/var/tmp",
+                 socket_scm_helper: Optional[str] = None,
+                 sock_dir: Optional[str] = None):
         if name is None:
             name = "qemu-%d" % os.getpid()
         if sock_dir is None:
@@ -118,8 +124,10 @@ def __init__(self, binary, args=None, name=None, test_dir="/var/tmp",
     @property
     def _base_args(self) -> List[str]:
         args = super()._base_args
-        args.extend(['-qtest', 'unix:path=' + self._qtest_path,
-                     '-accel', 'qtest'])
+        args.extend([
+            '-qtest', f"unix:path={self._qtest_path}",
+            '-accel', 'qtest'
+        ])
         return args
 
     def _pre_launch(self):
Signature · 1 line
2.21.3
author
#21 lore raw

PATCH v2 15/16 iotests.py: Adjust HMP kwargs typing

In reply to John Snow

mypy wants to ensure there's consistency between the kwargs arguments types and any unspecified keyword arguments. In this case, conv_keys is a bool, but the remaining keys are Any type. Mypy (correctly) infers the **kwargs type to be **Dict[str, str], which is not compatible with
conv_keys: bool.

Because QMP typing is a little fraught right now anyway, re-type kwargs to Dict[str, Any] which has the benefit of silencing this check right now.

A future re-design might type these more aggressively, but this will give us a baseline to work from with minimal disruption.

(Thanks Kevin Wolf for the debugging assist here)

---
 tests/qemu-iotests/iotests.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/qemu-iotests/iotests.py +1 -1
index 7c1773bba37..19b22bee618 100644
@@ -553,7 +553,7 @@ def add_incoming(self, addr):
 
     def hmp(self, command_line: str, use_log: bool = False) -> QMPMessage:
         cmd = 'human-monitor-command'
-        kwargs = {'command-line': command_line}
+        kwargs: Dict[str, Any] = {'command-line': command_line}
         if use_log:
             return self.qmp_log(cmd, **kwargs)
         else:
Signature · 1 line
2.21.3
author
#22 lore raw

PATCH v2 16/16 python/qemu: Add mypy type annotations

In reply to John Snow

These should all be purely annotations with no changes in behavior at all. You need to be in the python folder, but you should be able to confirm that these annotations are correct (or at least self-consistent) by running mypy --strict qemu.

---
 python/qemu/accel.py   |  8 ++--
 python/qemu/machine.py | 96 ++++++++++++++++++++++++------------------
 python/qemu/qmp.py     | 44 ++++++++++---------
 python/qemu/qtest.py   | 27 +++++++-----
 4 files changed, 100 insertions(+), 75 deletions(-)
python/qemu/accel.py +5 -3
index 7fabe629208..4325114e51f 100644
@@ -17,6 +17,7 @@
 import logging
 import os
 import subprocess
+from typing import List, Optional
 
 LOG = logging.getLogger(__name__)
 
@@ -29,7 +30,7 @@
 }
 
 
-def list_accel(qemu_bin):
+def list_accel(qemu_bin: str) -> List[str]:
     """
     List accelerators enabled in the QEMU binary.
 
@@ -49,7 +50,8 @@ def list_accel(qemu_bin):
     return [acc.strip() for acc in out.splitlines()[1:]]
 
 
-def kvm_available(target_arch=None, qemu_bin=None):
+def kvm_available(target_arch: Optional[str] = None,
+                  qemu_bin: Optional[str] = None) -> bool:
     """
     Check if KVM is available using the following heuristic:
       - Kernel module is present in the host;
@@ -72,7 +74,7 @@ def kvm_available(target_arch=None, qemu_bin=None):
     return True
 
 
-def tcg_available(qemu_bin):
+def tcg_available(qemu_bin: str) -> bool:
     """
     Check if TCG is available.
 
python/qemu/machine.py +54 -42

299 lines not on this page · raw message

python/qemu/qmp.py +25 -19

163 lines not on this page · raw message

python/qemu/qtest.py +16 -11
index ae4661d4d3e..df6300cd4f7 100644
@@ -24,6 +24,8 @@
     Optional,
     Sequence,
     TextIO,
+    Tuple,
+    Union,
 )
 
 from .machine import QEMUMachine
@@ -43,7 +45,8 @@ class QEMUQtestProtocol:
        No conection is estabalished by __init__(), this is done
        by the connect() or accept() methods.
     """
-    def __init__(self, address, server=False):
+    def __init__(self, address: Union[Tuple[str, str], str],
+                 server: bool = False):
         self._address = address
         self._sock = self._get_sock()
         self._sockfile: Optional[TextIO] = None
@@ -51,14 +54,14 @@ def __init__(self, address, server=False):
             self._sock.bind(self._address)
             self._sock.listen(1)
 
-    def _get_sock(self):
+    def _get_sock(self) -> socket.socket:
         if isinstance(self._address, tuple):
             family = socket.AF_INET
         else:
             family = socket.AF_UNIX
         return socket.socket(family, socket.SOCK_STREAM)
 
-    def connect(self):
+    def connect(self) -> None:
         """
         Connect to the qtest socket.
 
@@ -67,7 +70,7 @@ def connect(self):
         self._sock.connect(self._address)
         self._sockfile = self._sock.makefile(mode='r')
 
-    def accept(self):
+    def accept(self) -> None:
         """
         Await connection from QEMU.
 
@@ -76,7 +79,7 @@ def accept(self):
         self._sock, _ = self._sock.accept()
         self._sockfile = self._sock.makefile(mode='r')
 
-    def cmd(self, qtest_cmd):
+    def cmd(self, qtest_cmd: str) -> str:
         """
         Send a qtest command on the wire.
 
@@ -87,14 +90,16 @@ def cmd(self, qtest_cmd):
         resp = self._sockfile.readline()
         return resp
 
-    def close(self):
-        """Close this socket."""
+    def close(self) -> None:
+        """
+        Close this socket.
+        """
         self._sock.close()
         if self._sockfile:
             self._sockfile.close()
             self._sockfile = None
 
-    def settimeout(self, timeout):
+    def settimeout(self, timeout: Optional[float]) -> None:
         """Set a timeout, in seconds."""
         self._sock.settimeout(timeout)
 
@@ -118,7 +123,7 @@ def __init__(self,
         super().__init__(binary, args, name=name, test_dir=test_dir,
                          socket_scm_helper=socket_scm_helper,
                          sock_dir=sock_dir)
-        self._qtest = None
+        self._qtest: Optional[QEMUQtestProtocol] = None
         self._qtest_path = os.path.join(sock_dir, name + "-qtest.sock")
 
     @property
@@ -130,7 +135,7 @@ def _base_args(self) -> List[str]:
         ])
         return args
 
-    def _pre_launch(self):
+    def _pre_launch(self) -> None:
         super()._pre_launch()
         self._qtest = QEMUQtestProtocol(self._qtest_path, server=True)
 
@@ -139,7 +144,7 @@ def _post_launch(self) -> None:
         super()._post_launch()
         self._qtest.accept()
 
-    def _post_shutdown(self):
+    def _post_shutdown(self) -> None:
         super()._post_shutdown()
         self._remove_if_exists(self._qtest_path)
 
Signature · 1 line
2.21.3
#23 lore raw

Re: [PATCH v2 16/16] python/qemu: Add mypy type annotations

Am 02.06.2020 um 23:45 hat John Snow geschrieben:

Quoted 21 lines

These should all be purely annotations with no changes in behavior at all. You need to be in the python folder, but you should be able to confirm that these annotations are correct (or at least self-consistent) by running mypy --strict qemu.

python/qemu/qtest.py +2 -0
index ae4661d4d3e..df6300cd4f7 100644
@@ -24,6 +24,8 @@
     Optional,
     Sequence,
     TextIO,
+    Tuple,
+    Union,
 )
 
 from .machine import QEMUMachine

python/qemu/qtest.py class QEMUQtestProtocol: · line 49

Quoted 6 lines
python/qemu/qtest.py +2 -1
@@ -43,7 +45,8 @@ class QEMUQtestProtocol:
        No conection is estabalished by __init__(), this is done
        by the connect() or accept() methods.
     """
-    def __init__(self, address, server=False):
+    def __init__(self, address: Union[Tuple[str, str], str],
+                 server: bool = False):

Shouldn't this be SocketAddrT now?

Kevin

author
#24 lore raw

Re: [PATCH v2 16/16] python/qemu: Add mypy type annotations

Quoted 31 lines · Kevin Wolf

Am 02.06.2020 um 23:45 hat John Snow geschrieben:

Quoted 21 lines

These should all be purely annotations with no changes in behavior at all. You need to be in the python folder, but you should be able to confirm that these annotations are correct (or at least self-consistent) by running mypy --strict qemu.

python/qemu/qtest.py +2 -0
index ae4661d4d3e..df6300cd4f7 100644
@@ -24,6 +24,8 @@
     Optional,
     Sequence,
     TextIO,
+    Tuple,
+    Union,
 )
 
 from .machine import QEMUMachine

python/qemu/qtest.py class QEMUQtestProtocol: · line 49

Quoted 6 lines
python/qemu/qtest.py +2 -1
@@ -43,7 +45,8 @@ class QEMUQtestProtocol:
        No conection is estabalished by __init__(), this is done
        by the connect() or accept() methods.
     """
-    def __init__(self, address, server=False):
+    def __init__(self, address: Union[Tuple[str, str], str],
+                 server: bool = False):

Shouldn't this be SocketAddrT now?

Quoted 2 lines

Kevin

Ah, yeah. I had intended it for QMP, but it obviously works here, too.

author
#26 lore raw
Quoted 10 lines · Vladimir Sementsov-Ogievskiy
Quoted 1 line · John Snow

Requires: [email protected]

Hmm, somehow, I can't find it neither in
https://lists.gnu.org/archive/html/qemu-devel/
nor in my thunderbird..

Could you post sequence of your series by subject, or export a git branch?

1.
[PATCH v2 0/1] python/machine.py: refactor shutdown​
https://github.com/jnsnow/qemu/tree/python-package-reorder
(Patch that re-writes shutdown and kill)

2.
[PATCH v2 00/16] python: add mypy support to python/qemu​
https://github.com/jnsnow/qemu/tree/python-package-mypy
(This patchset.)

3.
[PATCH 0/7] python: create installable package
https://github.com/jnsnow/qemu/tree/python-package-refactor
(Python package series)

Reviewed-by on the v2 series
#27 lore raw
In reply to John Snow

Am 02.06.2020 um 23:45 hat John Snow geschrieben:

Quoted 7 lines

Requires: [email protected]

This series is extracted from my larger series that attempted to bundle our python module as an installable module. These fixes don't require that, so they are being sent first as I think there's less up for debate in here.

This requires my "refactor shutdown" patch as a pre-requisite.

I had only minor comments, so with or without the suggested changes: