diff mbox series

[3/3] qapi: provide a friendly string representation of QAPI classes

Message ID 20210302175524.1290840-4-berrange@redhat.com
State New
Headers show
Series audio: make audiodev introspectable by mgmt apps | expand

Commit Message

Daniel P. Berrangé March 2, 2021, 5:55 p.m. UTC
If printing a QAPI schema object for debugging we get the classname and
a hex value for the instance. With this change we instead get the
classname and the human friendly name of the QAPI type instance.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 scripts/qapi/schema.py | 3 +++
 1 file changed, 3 insertions(+)

Comments

Eric Blake March 2, 2021, 7:06 p.m. UTC | #1
On 3/2/21 11:55 AM, Daniel P. Berrangé wrote:
> If printing a QAPI schema object for debugging we get the classname and
> a hex value for the instance. With this change we instead get the
> classname and the human friendly name of the QAPI type instance.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  scripts/qapi/schema.py | 3 +++
>  1 file changed, 3 insertions(+)

Reviewed-by: Eric Blake <eblake@redhat.com>

> 
> diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
> index ff16578f6d..800bc5994b 100644
> --- a/scripts/qapi/schema.py
> +++ b/scripts/qapi/schema.py
> @@ -46,6 +46,9 @@ def __init__(self, name: str, info, doc, ifcond=None, features=None):
>          self.features = features or []
>          self._checked = False
>  
> +    def __repr__(self):
> +        return "%s<%s>" % (type(self).__name__, self.name)
> +
>      def c_name(self):
>          return c_name(self.name)
>  
>
Philippe Mathieu-Daudé March 2, 2021, 9:02 p.m. UTC | #2
On 3/2/21 6:55 PM, Daniel P. Berrangé wrote:
> If printing a QAPI schema object for debugging we get the classname and
> a hex value for the instance. With this change we instead get the
> classname and the human friendly name of the QAPI type instance.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  scripts/qapi/schema.py | 3 +++
>  1 file changed, 3 insertions(+)

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Markus Armbruster March 5, 2021, 1:18 p.m. UTC | #3
Daniel P. Berrangé <berrange@redhat.com> writes:

> If printing a QAPI schema object for debugging we get the classname and
> a hex value for the instance. With this change we instead get the
> classname and the human friendly name of the QAPI type instance.
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  scripts/qapi/schema.py | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
> index ff16578f6d..800bc5994b 100644
> --- a/scripts/qapi/schema.py
> +++ b/scripts/qapi/schema.py
> @@ -46,6 +46,9 @@ def __init__(self, name: str, info, doc, ifcond=None, features=None):
>          self.features = features or []
>          self._checked = False
>  
> +    def __repr__(self):
> +        return "%s<%s>" % (type(self).__name__, self.name)
> +
>      def c_name(self):
>          return c_name(self.name)

https://docs.python.org/3.6/reference/datamodel.html#object.__repr__

    Called by the repr() built-in function to compute the “official”
    string representation of an object.  If at all possible, this should
    look like a valid Python expression that could be used to recreate
    an object with the same value (given an appropriate environment).

Making QAPISchemaEntity.__repr__() return "a valid Python expression
that could be used to recreate an object with the same value" is
probably more trouble than it's worth.

    If this is not possible, a string of the form <...some useful
    description...> should be returned.

I'm afraid your .__repr__() has the < in the wrong place.

                                         The return value must be a
    __repr__() is also used when an “informal” string representation of
    instances of that class is required.

    This is typically used for debugging, so it is important that the
    representation is information-rich and unambiguous.


I guess your .__repr__() is unambiguous enough for practical purposes,
as entity names are typically unique within a schema.  *Except* for
QAPISchemaInclude, where self.name is always None.

What about self.name or id(self)?
diff mbox series

Patch

diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
index ff16578f6d..800bc5994b 100644
--- a/scripts/qapi/schema.py
+++ b/scripts/qapi/schema.py
@@ -46,6 +46,9 @@  def __init__(self, name: str, info, doc, ifcond=None, features=None):
         self.features = features or []
         self._checked = False
 
+    def __repr__(self):
+        return "%s<%s>" % (type(self).__name__, self.name)
+
     def c_name(self):
         return c_name(self.name)