Skip to content

Commit ef5d474

Browse files
jnsnowphilmd
authored andcommitted
python/qmp.py: Do not return None from cmd_obj
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 <[email protected]> Reviewed-by: Kevin Wolf <[email protected]> Message-Id: <[email protected]> Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
1 parent e3a23b4 commit ef5d474

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

python/qemu/qmp.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,22 +225,18 @@ def accept(self, timeout=15.0):
225225
self.__sockfile = self.__sock.makefile(mode='r')
226226
return self.__negotiate_capabilities()
227227

228-
def cmd_obj(self, qmp_cmd):
228+
def cmd_obj(self, qmp_cmd: QMPMessage) -> QMPMessage:
229229
"""
230230
Send a QMP command to the QMP Monitor.
231231
232232
@param qmp_cmd: QMP command to be sent as a Python dict
233-
@return QMP response as a Python dict or None if the connection has
234-
been closed
233+
@return QMP response as a Python dict
235234
"""
236235
self.logger.debug(">>> %s", qmp_cmd)
237-
try:
238-
self.__sock.sendall(json.dumps(qmp_cmd).encode('utf-8'))
239-
except OSError as err:
240-
if err.errno == errno.EPIPE:
241-
return None
242-
raise err
236+
self.__sock.sendall(json.dumps(qmp_cmd).encode('utf-8'))
243237
resp = self.__json_read()
238+
if resp is None:
239+
raise QMPConnectError("Unexpected empty reply from server")
244240
self.logger.debug("<<< %s", resp)
245241
return resp
246242

0 commit comments

Comments
 (0)