diff options
author | John Snow | 2020-05-14 07:53:43 +0200 |
---|---|---|
committer | Philippe Mathieu-Daudé | 2020-05-31 18:25:31 +0200 |
commit | 0add048fbd9992151e4c592977df9cff8558ca60 (patch) | |
tree | b698f92d82a4c748dda9b917d3ba35317ca2cf04 /python/qemu/qtest.py | |
parent | python/qemu: remove Python2 style super() calls (diff) | |
download | qemu-0add048fbd9992151e4c592977df9cff8558ca60.tar.gz qemu-0add048fbd9992151e4c592977df9cff8558ca60.tar.xz qemu-0add048fbd9992151e4c592977df9cff8558ca60.zip |
python/qemu: fix socket.makefile() typing
Note:
A bug in typeshed (https://github.com/python/typeshed/issues/3977)
misinterprets the type of makefile(). Work around this by explicitly
stating that we are opening a text-mode file.
Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20200514055403.18902-13-jsnow@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Diffstat (limited to 'python/qemu/qtest.py')
-rw-r--r-- | python/qemu/qtest.py | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/python/qemu/qtest.py b/python/qemu/qtest.py index 7943487c2b..4c88590eb0 100644 --- a/python/qemu/qtest.py +++ b/python/qemu/qtest.py @@ -19,6 +19,7 @@ subclass of QEMUMachine, respectively. import socket import os +from typing import Optional, TextIO from .machine import QEMUMachine @@ -40,7 +41,7 @@ class QEMUQtestProtocol: def __init__(self, address, server=False): self._address = address self._sock = self._get_sock() - self._sockfile = None + self._sockfile: Optional[TextIO] = None if server: self._sock.bind(self._address) self._sock.listen(1) @@ -59,7 +60,7 @@ class QEMUQtestProtocol: @raise socket.error on socket connection errors """ self._sock.connect(self._address) - self._sockfile = self._sock.makefile() + self._sockfile = self._sock.makefile(mode='r') def accept(self): """ @@ -68,7 +69,7 @@ class QEMUQtestProtocol: @raise socket.error on socket connection errors """ self._sock, _ = self._sock.accept() - self._sockfile = self._sock.makefile() + self._sockfile = self._sock.makefile(mode='r') def cmd(self, qtest_cmd): """ @@ -76,6 +77,7 @@ class QEMUQtestProtocol: @param qtest_cmd: qtest command text to be sent """ + assert self._sockfile is not None self._sock.sendall((qtest_cmd + "\n").encode('utf-8')) resp = self._sockfile.readline() return resp @@ -83,7 +85,9 @@ class QEMUQtestProtocol: def close(self): """Close this socket.""" self._sock.close() - self._sockfile.close() + if self._sockfile: + self._sockfile.close() + self._sockfile = None def settimeout(self, timeout): """Set a timeout, in seconds.""" |