diff mbox series

[RFC] python: add __repr__ to ConsoleSocket to aid debugging

Message ID 20201207200737.5090-1-alex.bennee@linaro.org
State New
Headers show
Series [RFC] python: add __repr__ to ConsoleSocket to aid debugging | expand

Commit Message

Alex Bennée Dec. 7, 2020, 8:07 p.m. UTC
While attempting to debug some console weirdness I thought it would be
worth making it easier to see what it had inside.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

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

-- 
2.20.1

Comments

John Snow Dec. 7, 2020, 9:02 p.m. UTC | #1
On 12/7/20 3:07 PM, Alex Bennée wrote:
> While attempting to debug some console weirdness I thought it would be

> worth making it easier to see what it had inside.

> 

> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

> ---

>   python/qemu/console_socket.py | 8 ++++++++

>   1 file changed, 8 insertions(+)

> 

> diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py

> index f060d79e06..77966d1fe9 100644

> --- a/python/qemu/console_socket.py

> +++ b/python/qemu/console_socket.py

> @@ -45,6 +45,14 @@ class ConsoleSocket(socket.socket):

>           if drain:

>               self._drain_thread = self._thread_start()

>   

> +    def __repr__(self):


def __repr__(self) -> str:

> +        s = super(ConsoleSocket, self).__repr__()


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

> +        s = s.rstrip(">")

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

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

> +        s += ">"

> +        return s

> +


Reviewed-by: John Snow <jsnow@redhat.com>


feel free to take this through your testing tree.

--js
Willian Rampazzo Dec. 7, 2020, 9:35 p.m. UTC | #2
On Mon, Dec 7, 2020 at 5:10 PM Alex Bennée <alex.bennee@linaro.org> wrote:
>

> While attempting to debug some console weirdness I thought it would be

> worth making it easier to see what it had inside.

>

> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

> ---

>  python/qemu/console_socket.py | 8 ++++++++

>  1 file changed, 8 insertions(+)

>

> diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py

> index f060d79e06..77966d1fe9 100644

> --- a/python/qemu/console_socket.py

> +++ b/python/qemu/console_socket.py

> @@ -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.

> +        return s

> +

>      def _drain_fn(self) -> None:

>          """Drains the socket and runs while the socket is open."""

>          while self._open:

> --

> 2.20.1

>

>
Philippe Mathieu-Daudé Dec. 7, 2020, 10:14 p.m. UTC | #3
Hi Willian,

On 12/7/20 10:35 PM, Willian Rampazzo wrote:
> On Mon, Dec 7, 2020 at 5:10 PM Alex Bennée <alex.bennee@linaro.org> wrote:

>>

>> While attempting to debug some console weirdness I thought it would be

>> worth making it easier to see what it had inside.

>>

>> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

>> ---

>>  python/qemu/console_socket.py | 8 ++++++++

>>  1 file changed, 8 insertions(+)

>>

>> diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py

>> index f060d79e06..77966d1fe9 100644

>> --- a/python/qemu/console_socket.py

>> +++ b/python/qemu/console_socket.py

>> @@ -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.


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

> 

>> +        return s

>> +

>>      def _drain_fn(self) -> None:

>>          """Drains the socket and runs while the socket is open."""

>>          while self._open:

>> --

>> 2.20.1

>>

>>

> 

>
Willian Rampazzo Dec. 7, 2020, 11:14 p.m. UTC | #4
Em seg, 7 de dez de 2020 19:14, Philippe Mathieu-Daudé <philmd@redhat.com>
escreveu:

> Hi Willian,

>

> On 12/7/20 10:35 PM, Willian Rampazzo wrote:

> > On Mon, Dec 7, 2020 at 5:10 PM Alex Bennée <alex.bennee@linaro.org>

> wrote:

> >>

> >> While attempting to debug some console weirdness I thought it would be

> >> worth making it easier to see what it had inside.

> >>

> >> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

> >> ---

> >>  python/qemu/console_socket.py | 8 ++++++++

> >>  1 file changed, 8 insertions(+)

> >>

> >> diff --git a/python/qemu/console_socket.py

> b/python/qemu/console_socket.py

> >> index f060d79e06..77966d1fe9 100644

> >> --- a/python/qemu/console_socket.py

> >> +++ b/python/qemu/console_socket.py

> >> @@ -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.

>

> 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

Reviewed-by: Willian Rampazzo <willianr@redhat.com>



> >

> >> +        return s

> >> +

> >>      def _drain_fn(self) -> None:

> >>          """Drains the socket and runs while the socket is open."""

> >>          while self._open:

> >> --

> >> 2.20.1

> >>

> >>

> >

> >

>

>
<div dir="auto"><div><br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Em seg, 7 de dez de 2020 19:14, Philippe Mathieu-Daudé &lt;<a href="mailto:philmd@redhat.com">philmd@redhat.com</a>&gt; escreveu:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Willian,<br>
<br>
On 12/7/20 10:35 PM, Willian Rampazzo wrote:<br>
&gt; On Mon, Dec 7, 2020 at 5:10 PM Alex Bennée &lt;<a href="mailto:alex.bennee@linaro.org" target="_blank" rel="noreferrer">alex.bennee@linaro.org</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; While attempting to debug some console weirdness I thought it would be<br>
&gt;&gt; worth making it easier to see what it had inside.<br>
&gt;&gt;<br>
&gt;&gt; Signed-off-by: Alex Bennée &lt;<a href="mailto:alex.bennee@linaro.org" target="_blank" rel="noreferrer">alex.bennee@linaro.org</a>&gt;<br>
&gt;&gt; ---<br>
&gt;&gt;  python/qemu/console_socket.py | 8 ++++++++<br>
&gt;&gt;  1 file changed, 8 insertions(+)<br>
&gt;&gt;<br>
&gt;&gt; diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py<br>
&gt;&gt; index f060d79e06..77966d1fe9 100644<br>
&gt;&gt; --- a/python/qemu/console_socket.py<br>
&gt;&gt; +++ b/python/qemu/console_socket.py<br>
&gt;&gt; @@ -45,6 +45,14 @@ class ConsoleSocket(socket.socket):<br>
&gt;&gt;          if drain:<br>
&gt;&gt;              self._drain_thread = self._thread_start()<br>
&gt;&gt;<br>
&gt;&gt; +    def __repr__(self):<br>
&gt;&gt; +        s = super(ConsoleSocket, self).__repr__()<br>
&gt;&gt; +        s = s.rstrip(&quot;&gt;&quot;)<br>
&gt;&gt; +        s += &quot;, logfile=%s&quot; % (self._logfile)<br>
&gt;&gt; +        s += &quot;, drain_thread=%s&quot; % (self._drain_thread)<br>
&gt;&gt; +        s += &quot;&gt;&quot;<br>
&gt; <br>
&gt; We could use something more pythonic for this file. Instead of 3<br>
&gt; string concatenations, my suggestion is to go with string formatting,<br>
&gt; like:<br>
&gt; <br>
&gt; s = &quot;%s,  logfile=%s, drain_thread=%s&gt;&quot; % (s, self._logfile, self._drain_thread)<br>
&gt; <br>
&gt; As str is immutable in Python, it avoids unnecessary copies.<br>
<br>
With this (and John&#39;s comment) addressed, are you OK to add<br>
your R-b tag?<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">Absolutely!</div><div dir="auto"><br></div><div dir="auto">The result will be the same in the end, so</div><div dir="auto"><br></div><div dir="auto">Reviewed-by: Willian Rampazzo &lt;<a href="mailto:willianr@redhat.com">willianr@redhat.com</a>&gt;</div><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
&gt; <br>
&gt;&gt; +        return s<br>
&gt;&gt; +<br>
&gt;&gt;      def _drain_fn(self) -&gt; None:<br>
&gt;&gt;          &quot;&quot;&quot;Drains the socket and runs while the socket is open.&quot;&quot;&quot;<br>
&gt;&gt;          while self._open:<br>
&gt;&gt; --<br>
&gt;&gt; 2.20.1<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt; <br>
&gt; <br>
<br>
</blockquote></div></div></div>
John Snow Dec. 8, 2020, 3:09 p.m. UTC | #5
On 12/7/20 4:35 PM, Willian Rampazzo wrote:
> 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
diff mbox series

Patch

diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py
index f060d79e06..77966d1fe9 100644
--- a/python/qemu/console_socket.py
+++ b/python/qemu/console_socket.py
@@ -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: