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

Martin Kletzander mkletzan at redhat.com
Mon Feb 4 21:51:46 UTC 2013


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.
---
 src/virtManager/cli.py        |  4 ++--
 src/virtManager/connection.py | 15 +++++++++------
 src/virtManager/domain.py     |  7 ++++---
 src/virtManager/migrate.py    |  4 +++-
 4 files changed, 18 insertions(+), 12 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..df8264b 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,15 @@ 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(libvir, "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 +1058,8 @@ 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():
@@ -1419,7 +1422,7 @@ class vmmConnection(vmmGObject):
                 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)):
+                     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..1da7e7a 100644
--- a/src/virtManager/migrate.py
+++ b/src/virtManager/migrate.py
@@ -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




More information about the virt-tools-list mailing list