Skip to content

Commit 4b84d87

Browse files
Robert Foleystsquad
authored andcommitted
python/qemu: Cleanup changes to ConsoleSocket
The changes to console_socket.py and machine.py are to cleanup for pylint and flake8. Signed-off-by: Robert Foley <[email protected]> Signed-off-by: Alex Bennée <[email protected]> Reviewed-by: Alex Bennée <[email protected]> Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Message-Id: <[email protected]> Message-Id: <[email protected]>
1 parent 4458838 commit 4b84d87

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

python/qemu/console_socket.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
#!/usr/bin/env python3
2-
#
3-
# This python module implements a ConsoleSocket object which is
4-
# designed always drain the socket itself, and place
5-
# the bytes into a in memory buffer for later processing.
6-
#
7-
# Optionally a file path can be passed in and we will also
8-
# dump the characters to this file for debug.
9-
#
1+
"""
2+
QEMU Console Socket Module:
3+
4+
This python module implements a ConsoleSocket object,
5+
which can drain a socket and optionally dump the bytes to file.
6+
"""
107
# Copyright 2020 Linaro
118
#
129
# Authors:
@@ -15,20 +12,27 @@
1512
# This code is licensed under the GPL version 2 or later. See
1613
# the COPYING file in the top-level directory.
1714
#
15+
1816
import asyncore
1917
import socket
2018
import threading
21-
import io
22-
import os
23-
import sys
2419
from collections import deque
2520
import time
26-
import traceback
21+
2722

2823
class ConsoleSocket(asyncore.dispatcher):
24+
"""
25+
ConsoleSocket represents a socket attached to a char device.
2926
27+
Drains the socket and places the bytes into an in memory buffer
28+
for later processing.
29+
30+
Optionally a file path can be passed in and we will also
31+
dump the characters to this file for debugging purposes.
32+
"""
3033
def __init__(self, address, file=None):
3134
self._recv_timeout_sec = 300
35+
self._sleep_time = 0.5
3236
self._buffer = deque()
3337
self._asyncore_thread = None
3438
self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
@@ -70,31 +74,28 @@ def close(self):
7074

7175
def handle_read(self):
7276
"""process arriving characters into in memory _buffer"""
73-
try:
74-
data = asyncore.dispatcher.recv(self, 1)
75-
# latin1 is needed since there are some chars
76-
# we are receiving that cannot be encoded to utf-8
77-
# such as 0xe2, 0x80, 0xA6.
78-
string = data.decode("latin1")
79-
except:
80-
print("Exception seen.")
81-
traceback.print_exc()
82-
return
77+
data = asyncore.dispatcher.recv(self, 1)
78+
# latin1 is needed since there are some chars
79+
# we are receiving that cannot be encoded to utf-8
80+
# such as 0xe2, 0x80, 0xA6.
81+
string = data.decode("latin1")
8382
if self._logfile:
8483
self._logfile.write("{}".format(string))
8584
self._logfile.flush()
8685
for c in string:
8786
self._buffer.extend(c)
8887

89-
def recv(self, n=1, sleep_delay_s=0.1):
90-
"""Return chars from in memory buffer"""
88+
def recv(self, buffer_size=1):
89+
"""Return chars from in memory buffer.
90+
Maintains the same API as socket.socket.recv.
91+
"""
9192
start_time = time.time()
92-
while len(self._buffer) < n:
93-
time.sleep(sleep_delay_s)
93+
while len(self._buffer) < buffer_size:
94+
time.sleep(self._sleep_time)
9495
elapsed_sec = time.time() - start_time
9596
if elapsed_sec > self._recv_timeout_sec:
9697
raise socket.timeout
97-
chars = ''.join([self._buffer.popleft() for i in range(n)])
98+
chars = ''.join([self._buffer.popleft() for i in range(buffer_size)])
9899
# We choose to use latin1 to remain consistent with
99100
# handle_read() and give back the same data as the user would
100101
# receive if they were reading directly from the

python/qemu/machine.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import tempfile
2828
from typing import Optional, Type
2929
from types import TracebackType
30-
from qemu.console_socket import ConsoleSocket
30+
from . import console_socket
3131

3232
from . import qmp
3333

@@ -674,8 +674,9 @@ def console_socket(self):
674674
"""
675675
if self._console_socket is None:
676676
if self._drain_console:
677-
self._console_socket = ConsoleSocket(self._console_address,
678-
file=self._console_log_path)
677+
self._console_socket = console_socket.ConsoleSocket(
678+
self._console_address,
679+
file=self._console_log_path)
679680
else:
680681
self._console_socket = socket.socket(socket.AF_UNIX,
681682
socket.SOCK_STREAM)

python/qemu/pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ good-names=i,
3333
Run,
3434
_,
3535
fd,
36-
36+
c,
3737
[VARIABLES]
3838

3939
[STRING]

0 commit comments

Comments
 (0)