From 932ca4bbde5ed6c57a8ae0b9cabb6e0a1ca1047a Mon Sep 17 00:00:00 2001 From: John Snow Date: Tue, 6 Oct 2020 19:57:58 -0400 Subject: python/qemu: use isort to lay out imports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Borrowed from the QAPI cleanup series, use the same configuration to standardize the way we write and sort imports. Signed-off-by: John Snow Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Kevin Wolf Message-id: 20201006235817.3280413-2-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/console_socket.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python/qemu/console_socket.py') diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py index 70869fbbdc..69f604c77f 100644 --- a/python/qemu/console_socket.py +++ b/python/qemu/console_socket.py @@ -13,9 +13,9 @@ which can drain a socket and optionally dump the bytes to file. # the COPYING file in the top-level directory. # +from collections import deque import socket import threading -from collections import deque import time -- cgit 1.4.1 From ff3513e6329ee0c2e7ea4a862c615cdb9c1ffc1b Mon Sep 17 00:00:00 2001 From: John Snow Date: Tue, 6 Oct 2020 19:58:09 -0400 Subject: python/qemu/console_socket.py: Correct type of recv() The type and parameter names of recv() should match socket.socket(). OK, easy enough, but in the cases we don't pass straight through to the real socket implementation, we probably can't accept such flags. OK, for now, assert that we don't receive flags in such cases. Signed-off-by: John Snow Reviewed-by: Kevin Wolf Message-id: 20201006235817.3280413-13-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/console_socket.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'python/qemu/console_socket.py') diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py index 69f604c77f..cb3400a038 100644 --- a/python/qemu/console_socket.py +++ b/python/qemu/console_socket.py @@ -92,13 +92,14 @@ class ConsoleSocket(socket.socket): for c in string: self._buffer.extend(c) - def recv(self, bufsize=1): + def recv(self, bufsize: int = 1, flags: int = 0) -> bytes: """Return chars from in memory buffer. Maintains the same API as socket.socket.recv. """ if self._drain_thread is None: # Not buffering the socket, pass thru to socket. - return socket.socket.recv(self, bufsize) + return socket.socket.recv(self, bufsize, flags) + assert not flags, "Cannot pass flags to recv() in drained mode" start_time = time.time() while len(self._buffer) < bufsize: time.sleep(self._sleep_time) -- cgit 1.4.1 From 6cf4cce7cb489fb5ed7eb72124f6b0c422155ebc Mon Sep 17 00:00:00 2001 From: John Snow Date: Tue, 6 Oct 2020 19:58:10 -0400 Subject: python/qemu/console_socket.py: fix typing of settimeout The types and names of the parameters must match the socket.socket interface. Signed-off-by: John Snow Reviewed-by: Kevin Wolf Message-id: 20201006235817.3280413-14-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/console_socket.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'python/qemu/console_socket.py') diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py index cb3400a038..3945682506 100644 --- a/python/qemu/console_socket.py +++ b/python/qemu/console_socket.py @@ -17,6 +17,7 @@ from collections import deque import socket import threading import time +from typing import Optional class ConsoleSocket(socket.socket): @@ -31,6 +32,7 @@ class ConsoleSocket(socket.socket): """ def __init__(self, address, file=None, drain=False): self._recv_timeout_sec = 300 + self._recv_timeout_sec = 300.0 self._sleep_time = 0.5 self._buffer = deque() socket.socket.__init__(self, socket.AF_UNIX, socket.SOCK_STREAM) @@ -120,11 +122,11 @@ class ConsoleSocket(socket.socket): if self._drain_thread is None: socket.socket.setblocking(self, value) - def settimeout(self, seconds): + def settimeout(self, value: Optional[float]) -> None: """When not draining we pass thru to the socket, since when draining we control the timeout. """ - if seconds is not None: - self._recv_timeout_sec = seconds + if value is not None: + self._recv_timeout_sec = value if self._drain_thread is None: - socket.socket.settimeout(self, seconds) + socket.socket.settimeout(self, value) -- cgit 1.4.1 From 714ac05a1996c0adf2740bce935f790d8824ea9e Mon Sep 17 00:00:00 2001 From: John Snow Date: Tue, 6 Oct 2020 19:58:11 -0400 Subject: python/qemu/console_socket.py: Clarify type of drain_thread Mypy needs just a little help to guess the type here. Signed-off-by: John Snow Reviewed-by: Kevin Wolf Message-id: 20201006235817.3280413-15-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/console_socket.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'python/qemu/console_socket.py') diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py index 3945682506..d4669c441d 100644 --- a/python/qemu/console_socket.py +++ b/python/qemu/console_socket.py @@ -41,10 +41,9 @@ class ConsoleSocket(socket.socket): if file: self._logfile = open(file, "w") self._open = True + self._drain_thread = None if drain: self._drain_thread = self._thread_start() - else: - self._drain_thread = None def _drain_fn(self): """Drains the socket and runs while the socket is open.""" -- cgit 1.4.1 From e35c13826768cf4224b4f9c6884b08f3a7ed887e Mon Sep 17 00:00:00 2001 From: John Snow Date: Tue, 6 Oct 2020 19:58:12 -0400 Subject: python/qemu/console_socket.py: Add type hint annotations Finish the typing of console_socket.py with annotations and no code changes. Signed-off-by: John Snow Reviewed-by: Kevin Wolf Message-id: 20201006235817.3280413-16-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/console_socket.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'python/qemu/console_socket.py') diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py index d4669c441d..57e6eee017 100644 --- a/python/qemu/console_socket.py +++ b/python/qemu/console_socket.py @@ -17,7 +17,7 @@ from collections import deque import socket import threading import time -from typing import Optional +from typing import Deque, Optional class ConsoleSocket(socket.socket): @@ -30,11 +30,11 @@ class ConsoleSocket(socket.socket): Optionally a file path can be passed in and we will also dump the characters to this file for debugging purposes. """ - def __init__(self, address, file=None, drain=False): - self._recv_timeout_sec = 300 + def __init__(self, address: str, file: Optional[str] = None, + drain: bool = False): self._recv_timeout_sec = 300.0 self._sleep_time = 0.5 - self._buffer = deque() + self._buffer: Deque[str] = deque() socket.socket.__init__(self, socket.AF_UNIX, socket.SOCK_STREAM) self.connect(address) self._logfile = None @@ -45,7 +45,7 @@ class ConsoleSocket(socket.socket): if drain: self._drain_thread = self._thread_start() - def _drain_fn(self): + def _drain_fn(self) -> None: """Drains the socket and runs while the socket is open.""" while self._open: try: @@ -56,7 +56,7 @@ class ConsoleSocket(socket.socket): # self._open is set to False. time.sleep(self._sleep_time) - def _thread_start(self): + def _thread_start(self) -> threading.Thread: """Kick off a thread to drain the socket.""" # Configure socket to not block and timeout. # This allows our drain thread to not block @@ -68,7 +68,7 @@ class ConsoleSocket(socket.socket): drain_thread.start() return drain_thread - def close(self): + def close(self) -> None: """Close the base object and wait for the thread to terminate""" if self._open: self._open = False @@ -80,7 +80,7 @@ class ConsoleSocket(socket.socket): self._logfile.close() self._logfile = None - def _drain_socket(self): + def _drain_socket(self) -> None: """process arriving characters into in memory _buffer""" data = socket.socket.recv(self, 1) # latin1 is needed since there are some chars @@ -114,7 +114,7 @@ class ConsoleSocket(socket.socket): # socket w/o our intervention. return chars.encode("latin1") - def setblocking(self, value): + def setblocking(self, value: bool) -> None: """When not draining we pass thru to the socket, since when draining we control socket blocking. """ -- cgit 1.4.1 From af0db8825400ea8c79fd77ed4ba6fb6699bd0855 Mon Sep 17 00:00:00 2001 From: John Snow Date: Tue, 6 Oct 2020 19:58:13 -0400 Subject: python/qemu/console_socket.py: avoid encoding to/from string We can work directly in bytes instead of translating back and forth to string, which removes the question of which encodings to use. Signed-off-by: John Snow Reviewed-by: Kevin Wolf Message-id: 20201006235817.3280413-17-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/console_socket.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) (limited to 'python/qemu/console_socket.py') diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py index 57e6eee017..f060d79e06 100644 --- a/python/qemu/console_socket.py +++ b/python/qemu/console_socket.py @@ -34,12 +34,12 @@ class ConsoleSocket(socket.socket): drain: bool = False): self._recv_timeout_sec = 300.0 self._sleep_time = 0.5 - self._buffer: Deque[str] = deque() + self._buffer: Deque[int] = deque() socket.socket.__init__(self, socket.AF_UNIX, socket.SOCK_STREAM) self.connect(address) self._logfile = None if file: - self._logfile = open(file, "w") + self._logfile = open(file, "bw") self._open = True self._drain_thread = None if drain: @@ -83,15 +83,10 @@ class ConsoleSocket(socket.socket): def _drain_socket(self) -> None: """process arriving characters into in memory _buffer""" data = socket.socket.recv(self, 1) - # latin1 is needed since there are some chars - # we are receiving that cannot be encoded to utf-8 - # such as 0xe2, 0x80, 0xA6. - string = data.decode("latin1") if self._logfile: - self._logfile.write("{}".format(string)) + self._logfile.write(data) self._logfile.flush() - for c in string: - self._buffer.extend(c) + self._buffer.extend(data) def recv(self, bufsize: int = 1, flags: int = 0) -> bytes: """Return chars from in memory buffer. @@ -107,12 +102,7 @@ class ConsoleSocket(socket.socket): elapsed_sec = time.time() - start_time if elapsed_sec > self._recv_timeout_sec: raise socket.timeout - chars = ''.join([self._buffer.popleft() for i in range(bufsize)]) - # We choose to use latin1 to remain consistent with - # handle_read() and give back the same data as the user would - # receive if they were reading directly from the - # socket w/o our intervention. - return chars.encode("latin1") + return bytes((self._buffer.popleft() for i in range(bufsize))) def setblocking(self, value: bool) -> None: """When not draining we pass thru to the socket, -- cgit 1.4.1