[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH RFC 17/32] python//qmp.py: Do not return None from cmd_obj
From: |
John Snow |
Subject: |
[PATCH RFC 17/32] python//qmp.py: Do not return None from cmd_obj |
Date: |
Thu, 14 May 2020 01:53:48 -0400 |
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.
Signed-off-by: John Snow <address@hidden>
---
python/qemu/lib/qmp.py | 14 +++++---------
1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/python/qemu/lib/qmp.py b/python/qemu/lib/qmp.py
index 82f86b4e45..0036204218 100644
--- a/python/qemu/lib/qmp.py
+++ b/python/qemu/lib/qmp.py
@@ -224,22 +224,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
--
2.21.1
- [PATCH RFC 08/32] python/qemu: Add Pipfile, (continued)
- [PATCH RFC 08/32] python/qemu: Add Pipfile, John Snow, 2020/05/14
- [PATCH RFC 11/32] python/qemu/lib: remove Python2 style super() calls, John Snow, 2020/05/14
- [PATCH RFC 14/32] python//qmp.py: use True/False for non/blocking modes, John Snow, 2020/05/14
- [PATCH RFC 16/32] python//qmp.py: re-absorb MonitorResponseError, John Snow, 2020/05/14
- [PATCH RFC 15/32] python//qmp.py: Define common types, John Snow, 2020/05/14
- [PATCH RFC 17/32] python//qmp.py: Do not return None from cmd_obj,
John Snow <=
- [PATCH RFC 18/32] python//qmp.py: add casts to JSON deserialization, John Snow, 2020/05/14
- [PATCH RFC 20/32] python//qmp.py: assert sockfile is not None, John Snow, 2020/05/14
- [PATCH RFC 21/32] python//machine.py: remove logging configuration, John Snow, 2020/05/14
- [PATCH RFC 22/32] python//machine.py: Fix monitor address typing, John Snow, 2020/05/14