Skip to contentSkip to conversation

python: add __repr__ to ConsoleSocket to aid debugging

RFC PATCH In review Alex Bennée 4 participants 6 messages Dec 7, 2020 – Dec 8, 2020

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 python: add __repr__ to ConsoleSocket to aid debugging

While attempting to debug some console weirdness I thought it would be worth making it easier to see what it had inside.

---
 python/qemu/console_socket.py | 8 ++++++++
 1 file changed, 8 insertions(+)
python/qemu/console_socket.py +8 -0
index f060d79e06..77966d1fe9 100644
@@ -45,6 +45,14 @@ class ConsoleSocket(socket.socket):
         if drain:
             self._drain_thread = self._thread_start()
 
+    def __repr__(self):
+        s = super(ConsoleSocket, self).__repr__()
+        s = s.rstrip(">")
+        s += ", logfile=%s" % (self._logfile)
+        s += ", drain_thread=%s" % (self._drain_thread)
+        s += ">"
+        return s
+
     def _drain_fn(self) -> None:
         """Drains the socket and runs while the socket is open."""
         while self._open:
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Alex Bennée

While attempting to debug some console weirdness I thought it would be worth making it easier to see what it had inside.

---
  python/qemu/console_socket.py | 8 ++++++++
  1 file changed, 8 insertions(+)
python/qemu/console_socket.py +0 -0
index f060d79e06..77966d1fe9 100644

python/qemu/console_socket.py class ConsoleSocket(socket.socket): · line 48

Quoted 4 lines
python/qemu/console_socket.py +1 -0
@@ -45,6 +45,14 @@ class ConsoleSocket(socket.socket):
          if drain:
              self._drain_thread = self._thread_start()
 
+    def __repr__(self):

def __repr__(self) -> str:

python/qemu/console_socket.py class ConsoleSocket(socket.socket): · line 49

Quoted 1 line
python/qemu/console_socket.py +1 -0
+        s = super(ConsoleSocket, self).__repr__()

Use python3-style super(): super().__repr__()

python/qemu/console_socket.py class ConsoleSocket(socket.socket): · line 55

Quoted 6 lines
python/qemu/console_socket.py +6 -0
+        s = s.rstrip(">")
+        s += ", logfile=%s" % (self._logfile)
+        s += ", drain_thread=%s" % (self._drain_thread)
+        s += ">"
+        return s
+

feel free to take this through your testing tree.

--js

#2 lore raw
In reply to Alex Bennée
Quoted 14 lines · Alex Bennée

While attempting to debug some console weirdness I thought it would be worth making it easier to see what it had inside.

---
 python/qemu/console_socket.py | 8 ++++++++
 1 file changed, 8 insertions(+)
python/qemu/console_socket.py +0 -0
index f060d79e06..77966d1fe9 100644

python/qemu/console_socket.py class ConsoleSocket(socket.socket): · line 53

Quoted 9 lines
python/qemu/console_socket.py +6 -0
@@ -45,6 +45,14 @@ class ConsoleSocket(socket.socket):
         if drain:
             self._drain_thread = self._thread_start()
 
+    def __repr__(self):
+        s = super(ConsoleSocket, self).__repr__()
+        s = s.rstrip(">")
+        s += ", logfile=%s" % (self._logfile)
+        s += ", drain_thread=%s" % (self._drain_thread)
+        s += ">"

We could use something more pythonic for this file. Instead of 3 string concatenations, my suggestion is to go with string formatting, like:

s = "%s, logfile=%s, drain_thread=%s>" % (s, self._logfile, self._drain_thread)

As str is immutable in Python, it avoids unnecessary copies.

Quoted 9 lines
python/qemu/console_socket.py +2 -1
+        return s
+
     def _drain_fn(self) -> None:
         """Drains the socket and runs while the socket is open."""
         while self._open:
--

2.20.1

#3 lore raw

Hi Willian,

Quoted 29 lines · Willian Rampazzo
Quoted 14 lines · Alex Bennée

While attempting to debug some console weirdness I thought it would be worth making it easier to see what it had inside.

---
 python/qemu/console_socket.py | 8 ++++++++
 1 file changed, 8 insertions(+)
python/qemu/console_socket.py +0 -0
index f060d79e06..77966d1fe9 100644

python/qemu/console_socket.py class ConsoleSocket(socket.socket): · line 53

Quoted 9 lines
python/qemu/console_socket.py +6 -0
@@ -45,6 +45,14 @@ class ConsoleSocket(socket.socket):
         if drain:
             self._drain_thread = self._thread_start()
 
+    def __repr__(self):
+        s = super(ConsoleSocket, self).__repr__()
+        s = s.rstrip(">")
+        s += ", logfile=%s" % (self._logfile)
+        s += ", drain_thread=%s" % (self._drain_thread)
+        s += ">"

We could use something more pythonic for this file. Instead of 3 string concatenations, my suggestion is to go with string formatting, like:

Quoted 3 lines

s = "%s, logfile=%s, drain_thread=%s>" % (s, self._logfile, self._drain_thread)

As str is immutable in Python, it avoids unnecessary copies.

With this (and John's comment) addressed, are you OK to add your R-b tag?

Quoted 12 lines
Quoted 10 lines
python/qemu/console_socket.py +2 -1
+        return s
+
     def _drain_fn(self) -> None:
         """Drains the socket and runs while the socket is open."""
         while self._open:
--

2.20.1

Reviewed-by
#4 lore raw

Em seg, 7 de dez de 2020 19:14, Philippe Mathieu-Daudé <[email protected]> escreveu:

Quoted 34 lines

Hi Willian,

Quoted 1 line · Willian Rampazzo

On Mon, Dec 7, 2020 at 5:10 PM Alex Bennée <[email protected]>

Quoted 10 lines · wrote:

Review

Quoted 10 lines

While attempting to debug some console weirdness I thought it would be worth making it easier to see what it had inside.

---
 python/qemu/console_socket.py | 8 ++++++++
 1 file changed, 8 insertions(+)
(quoted diff) +0 -0

b/python/qemu/console_socket.py

Quoted 18 lines

python/qemu/console_socket.py class ConsoleSocket(socket.socket): · line 53

Quoted 13 lines

index f060d79e06..77966d1fe9 100644

python/qemu/console_socket.py +6 -0
@@ -45,6 +45,14 @@ class ConsoleSocket(socket.socket):
         if drain:
             self._drain_thread = self._thread_start()
 
+    def __repr__(self):
+        s = super(ConsoleSocket, self).__repr__()
+        s = s.rstrip(">")
+        s += ", logfile=%s" % (self._logfile)
+        s += ", drain_thread=%s" % (self._drain_thread)
+        s += ">"

We could use something more pythonic for this file. Instead of 3 string concatenations, my suggestion is to go with string formatting, like:

Quoted 8 lines
Quoted 1 line

s = "%s, logfile=%s, drain_thread=%s>" % (s, self._logfile,

self._drain_thread)

Quoted 2 lines

As str is immutable in Python, it avoids unnecessary copies.

With this (and John's comment) addressed, are you OK to add your R-b tag?

Absolutely!

The result will be the same in the end, so

Quoted 14 lines
Quoted 12 lines
Quoted 10 lines
python/qemu/console_socket.py +2 -1
+        return s
+
     def _drain_fn(self) -> None:
         """Drains the socket and runs while the socket is open."""
         while self._open:
--

2.20.1

#5 lore raw
In reply to Willian Rampazzo
Quoted 7 lines · Willian Rampazzo

We could use something more pythonic for this file. Instead of 3 string concatenations, my suggestion is to go with string formatting, like:

s = "%s, logfile=%s, drain_thread=%s>" % (s, self._logfile, self._drain_thread)

As str is immutable in Python, it avoids unnecessary copies.

Sure, this is fine too. I'm not too worried about performance of debugging methods.

Alex, use your own discretion -- feel free to keep my RB/ACK and merge alongside your other tests when ready. Thanks!

--js