aboutsummaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorJohn Snow <jsnow@redhat.com>2023-05-17 12:34:05 -0400
committerJohn Snow <jsnow@redhat.com>2023-05-31 16:25:35 -0400
commit5bbc5936bb5b973848cbcabcc19346ffa812ae06 (patch)
tree3e84ebdd7026086d673e19ad8ea1485d9022e1c7 /python
parent7f5f3ae7d589d9297b2903e79cba1492807883d4 (diff)
python/qmp/legacy: remove open_with_socket() calls
Favor using connect() when passing a socket instead of open_with_socket(). Simultaneously, update constructor calls to use the combined address argument for QEMUMonitorProtocol(). Signed-off-by: John Snow <jsnow@redhat.com> Message-id: 20230517163406.2593480-5-jsnow@redhat.com Signed-off-by: John Snow <jsnow@redhat.com>
Diffstat (limited to 'python')
-rw-r--r--python/qemu/machine/machine.py7
-rw-r--r--python/qemu/qmp/legacy.py29
2 files changed, 16 insertions, 20 deletions
diff --git a/python/qemu/machine/machine.py b/python/qemu/machine/machine.py
index cc636cb6bd..c16a0b6fed 100644
--- a/python/qemu/machine/machine.py
+++ b/python/qemu/machine/machine.py
@@ -337,16 +337,17 @@ class QEMUMachine:
self._remove_files.append(self._console_address)
if self._qmp_set:
- sock = None
if self._monitor_address is None:
self._sock_pair = socket.socketpair()
sock = self._sock_pair[1]
if isinstance(self._monitor_address, str):
self._remove_files.append(self._monitor_address)
+ sock_or_addr = self._monitor_address or sock
+ assert sock_or_addr is not None
+
self._qmp_connection = QEMUMonitorProtocol(
- address=self._monitor_address,
- sock=sock,
+ sock_or_addr,
server=bool(self._monitor_address),
nickname=self._name
)
diff --git a/python/qemu/qmp/legacy.py b/python/qemu/qmp/legacy.py
index b1eb3f360f..e1e9383978 100644
--- a/python/qemu/qmp/legacy.py
+++ b/python/qemu/qmp/legacy.py
@@ -68,34 +68,31 @@ class QEMUMonitorProtocol:
Provide an API to connect to QEMU via QEMU Monitor Protocol (QMP)
and then allow to handle commands and events.
- :param address: QEMU address, can be either a unix socket path (string)
- or a tuple in the form ( address, port ) for a TCP
- connection or None
- :param sock: a socket or None
+ :param address: QEMU address, can be a unix socket path (string), a tuple
+ in the form ( address, port ) for a TCP connection, or an
+ existing `socket.socket` object.
:param server: Act as the socket server. (See 'accept')
+ Not applicable when passing a socket directly.
:param nickname: Optional nickname used for logging.
"""
def __init__(self,
- address: Optional[SocketAddrT] = None,
- sock: Optional[socket.socket] = None,
+ address: Union[SocketAddrT, socket.socket],
server: bool = False,
nickname: Optional[str] = None):
- assert address or sock
+ if server and isinstance(address, socket.socket):
+ raise ValueError(
+ "server argument should be False when passing a socket")
+
self._qmp = QMPClient(nickname)
self._aloop = asyncio.get_event_loop()
self._address = address
- self._sock = sock
self._timeout: Optional[float] = None
if server:
- if sock:
- assert self._sock is not None
- self._sync(self._qmp.open_with_socket(self._sock))
- else:
- assert self._address is not None
- self._sync(self._qmp.start_server(self._address))
+ assert not isinstance(self._address, socket.socket)
+ self._sync(self._qmp.start_server(self._address))
_T = TypeVar('_T')
@@ -150,13 +147,11 @@ class QEMUMonitorProtocol:
:return: QMP greeting dict, or None if negotiate is false
:raise ConnectError: on connection errors
"""
- addr_or_sock = self._address or self._sock
- assert addr_or_sock is not None
self._qmp.await_greeting = negotiate
self._qmp.negotiate = negotiate
self._sync(
- self._qmp.connect(addr_or_sock)
+ self._qmp.connect(self._address)
)
return self._get_greeting()