qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 3/3] qapi: provide a friendly string representation of QAPI c


From: Markus Armbruster
Subject: Re: [PATCH 3/3] qapi: provide a friendly string representation of QAPI classes
Date: Fri, 05 Mar 2021 14:18:08 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)

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)?




reply via email to

[Prev in Thread] Current Thread [Next in Thread]