[PATCH v2 02/18] python: backport 'EventListener: add __repr__ method'

John Snow posted 18 patches 5 months, 1 week ago
Maintainers: John Snow <jsnow@redhat.com>, Cleber Rosa <crosa@redhat.com>, Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>
There is a newer version of this series
[PATCH v2 02/18] python: backport 'EventListener: add __repr__ method'
Posted by John Snow 5 months, 1 week ago
When the object is not stateful, this repr method prints what you'd
expect. In cases where there are pending events, the output is augmented
to illustrate that.

The object itself has no idea if it's "active" or not, so it cannot
convey that information.

Signed-off-by: John Snow <jsnow@redhat.com>
cherry picked from commit 8a6f2e136dae395fec8aa5fd77487cfe12d9e05e
---
 python/qemu/qmp/events.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/python/qemu/qmp/events.py b/python/qemu/qmp/events.py
index 6199776cc66..66583496192 100644
--- a/python/qemu/qmp/events.py
+++ b/python/qemu/qmp/events.py
@@ -497,6 +497,21 @@ def __init__(
         #: Optional, secondary event filter.
         self.event_filter: Optional[EventFilter] = event_filter
 
+    def __repr__(self) -> str:
+        args: List[str] = []
+        if self.names:
+            args.append(f"names={self.names!r}")
+        if self.event_filter:
+            args.append(f"event_filter={self.event_filter!r}")
+
+        if self._queue.qsize():
+            state = f"<pending={self._queue.qsize()}>"
+        else:
+            state = ''
+
+        argstr = ", ".join(args)
+        return f"{type(self).__name__}{state}({argstr})"
+
     @property
     def history(self) -> Tuple[Message, ...]:
         """
-- 
2.50.1
Re: [PATCH v2 02/18] python: backport 'EventListener: add __repr__ method'
Posted by Daniel P. Berrangé 5 months ago
On Wed, Sep 03, 2025 at 01:11:08AM -0400, John Snow wrote:
> When the object is not stateful, this repr method prints what you'd
> expect. In cases where there are pending events, the output is augmented
> to illustrate that.
> 
> The object itself has no idea if it's "active" or not, so it cannot
> convey that information.
> 
> Signed-off-by: John Snow <jsnow@redhat.com>
> cherry picked from commit 8a6f2e136dae395fec8aa5fd77487cfe12d9e05e
> ---
>  python/qemu/qmp/events.py | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|