[virt-tools-list] [virt-manager PATCH v2] Avoid getting AttributeError when handling libvirt error

Cole Robinson crobinso at redhat.com
Wed Feb 6 20:51:15 UTC 2013


On 02/05/2013 04:40 AM, Martin Kletzander wrote:
> My python libvirt module doesn't have VIR_ERR_* inside itself and
> according to some places in the code, this was already taken care of
> by using the 'getattr' with safe default.  However, there were still
> some places where this wasn't handled properly, so I tried to fixed it
> by adding getattr for these attributes wherever 'git grep VIR_ERR'
> found it.  While on that, I took the liberty of cleaning up some
> related lines that were too long.

Like Dan said, we really only use getattr in the cases where an error
code/flag value might be too new for some libvirt versions we still try and
work with. So I won't apply this patch.

However maybe after the gtk3 port we can bump to a much more modern libvirt
requirement on the virt-manager host.

- Cole

> ---
>  src/virtManager/cli.py        |  4 ++--
>  src/virtManager/connection.py | 20 +++++++++++++-------
>  src/virtManager/domain.py     |  7 ++++---
>  src/virtManager/migrate.py    |  6 ++++--
>  4 files changed, 23 insertions(+), 14 deletions(-)
> 
> diff --git a/src/virtManager/cli.py b/src/virtManager/cli.py
> index 3be6945..8d668fa 100644
> --- a/src/virtManager/cli.py
> +++ b/src/virtManager/cli.py
> @@ -1,5 +1,5 @@
>  #
> -# Copyright (C) 2011 Red Hat, Inc.
> +# Copyright (C) 2011, 2013 Red Hat, Inc.
>  # Copyright (C) 2011 Cole Robinson <crobinso at redhat.com>
>  #
>  # This program is free software; you can redistribute it and/or modify
> @@ -72,7 +72,7 @@ def setup_logging(appname, debug_stdout):
> 
>      # Register libvirt handler
>      def libvirt_callback(ctx_ignore, err):
> -        if err[3] != libvirt.VIR_ERR_ERROR:
> +        if err[3] != getattr(libvirt, "VIR_ERR_ERROR", None):
>              # Don't log libvirt errors: global error handler will do that
>              logging.warn("Non-error from libvirt: '%s'", err[2])
>      libvirt.registerErrorHandler(f=libvirt_callback, ctx=None)
> diff --git a/src/virtManager/connection.py b/src/virtManager/connection.py
> index fe96df2..e839611 100644
> --- a/src/virtManager/connection.py
> +++ b/src/virtManager/connection.py
> @@ -1,5 +1,5 @@
>  #
> -# Copyright (C) 2006 Red Hat, Inc.
> +# Copyright (C) 2006, 2013 Red Hat, Inc.
>  # Copyright (C) 2006 Daniel P. Berrange <berrange at redhat.com>
>  #
>  # This program is free software; you can redistribute it and/or modify
> @@ -1040,13 +1040,16 @@ class vmmConnection(vmmGObject):
>              self.state = self.STATE_DISCONNECTED
> 
>              if (libexc and
> -                (libexc.get_error_code() ==
> -                 getattr(libvirt, "VIR_ERR_AUTH_CANCELLED", None))):
> +                libexc.get_error_code() == getattr(libvirt,
> +                                                   "VIR_ERR_AUTH_CANCELLED",
> +                                                   None)):
>                  logging.debug("User cancelled auth, not raising any error.")
>                  break
> 
>              if (libexc and
> -                libexc.get_error_code() == libvirt.VIR_ERR_AUTH_FAILED and
> +                libexc.get_error_code() == getattr(libvirt,
> +                                                   "VIR_ERR_AUTH_FAILED",
> +                                                   None) and
>                  "not authorized" in libexc.get_error_message().lower()):
>                  logging.debug("Looks like we might have failed policykit "
>                                "auth. Checking to see if we have a valid "
> @@ -1056,7 +1059,9 @@ class vmmConnection(vmmGObject):
>                      warnconsole = True
> 
>              if (libexc and
> -                libexc.get_error_code() == libvirt.VIR_ERR_AUTH_FAILED and
> +                libexc.get_error_code() == getattr(libvirt,
> +                                                   "VIR_ERR_AUTH_FAILED",
> +                                                   None) and
>                  "GSSAPI Error" in libexc.get_error_message() and
>                  "No credentials cache found" in libexc.get_error_message()):
>                  if connectauth.acquire_tgt():
> @@ -1418,8 +1423,9 @@ class vmmConnection(vmmGObject):
>              except Exception, e:
>                  logging.exception("Tick for VM '%s' failed", vm.get_name())
>                  if (isinstance(e, libvirt.libvirtError) and
> -                    (getattr(e, "get_error_code")() ==
> -                     libvirt.VIR_ERR_SYSTEM_ERROR)):
> +                    e.get_error_code() == getattr(libvirt,
> +                                                  "VIR_ERR_SYSTEM_ERROR",
> +                                                  None)):
>                      # Try a simple getInfo call to see if conn was dropped
>                      self.vmm.getInfo()
>                      logging.debug("vm tick raised system error but "
> diff --git a/src/virtManager/domain.py b/src/virtManager/domain.py
> index 53aa560..6a11f0b 100644
> --- a/src/virtManager/domain.py
> +++ b/src/virtManager/domain.py
> @@ -1,5 +1,5 @@
>  #
> -# Copyright (C) 2006 Red Hat, Inc.
> +# Copyright (C) 2006, 2013 Red Hat, Inc.
>  # Copyright (C) 2006 Daniel P. Berrange <berrange at redhat.com>
>  #
>  # This program is free software; you can redistribute it and/or modify
> @@ -1466,8 +1466,9 @@ class vmmDomain(vmmLibvirtObject):
>          try:
>              info = self._backend.info()
>          except libvirt.libvirtError, e:
> -            if (hasattr(libvirt, "VIR_ERR_NO_DOMAIN") and
> -                e.get_error_code() == getattr(libvirt, "VIR_ERR_NO_DOMAIN")):
> +            if e.get_error_code() == getattr(libvirt,
> +                                             "VIR_ERR_NO_DOMAIN",
> +                                             None):
>                  # Possibly a transient domain that was removed on shutdown
>                  return
>              raise
> diff --git a/src/virtManager/migrate.py b/src/virtManager/migrate.py
> index 1a488e5..3ec3a76 100644
> --- a/src/virtManager/migrate.py
> +++ b/src/virtManager/migrate.py
> @@ -1,5 +1,5 @@
>  #
> -# Copyright (C) 2009 Red Hat, Inc.
> +# Copyright (C) 2009, 2013 Red Hat, Inc.
>  # Copyright (C) 2009 Cole Robinson <crobinso at redhat.com>
>  #
>  # This program is free software; you can redistribute it and/or modify
> @@ -506,7 +506,9 @@ class vmmMigrateDialog(vmmGObjectUI):
>              return False
>          except libvirt.libvirtError, e:
>              if (isinstance(e, libvirt.libvirtError) and
> -                e.get_error_code() == libvirt.VIR_ERR_OPERATION_INVALID):
> +                e.get_error_code() == getattr(libvirt,
> +                                              "VIR_ERR_OPERATION_INVALID",
> +                                              None)):
>                  # migration has not been started, wait 100 milliseconds
>                  return True
> 
> --
> 1.8.1.2
> 
> _______________________________________________
> virt-tools-list mailing list
> virt-tools-list at redhat.com
> https://www.redhat.com/mailman/listinfo/virt-tools-list
> 




More information about the virt-tools-list mailing list