[libvirt] [test-API][PATCH 1/2] Declare hypervisor connection variable as the global

Guannan Ren gren at redhat.com
Thu Aug 4 08:37:20 UTC 2011


The is solve the problem where the failure of open() will lead to
null conn variable, and when we call close() later,it will report
no global 'conn' attribute to the ConnectAPI object.

remove a duplicated close() function
---
 lib/connectAPI.py |   19 ++++++-------------
 1 files changed, 6 insertions(+), 13 deletions(-)

diff --git a/lib/connectAPI.py b/lib/connectAPI.py
index 5d5b94f..702a088 100644
--- a/lib/connectAPI.py
+++ b/lib/connectAPI.py
@@ -40,11 +40,11 @@ import exception
 
 class ConnectAPI(object):
     def __init__(self):
-        pass
+        self.conn = None
 
     def open(self, uri):
         try:
-            self.conn = libvirt.open(uri)
+            conn = libvirt.open(uri)
             return self.conn
         except libvirt.libvirtError, e:
             message = e.get_error_message()
@@ -53,7 +53,7 @@ class ConnectAPI(object):
 
     def open_read_only(self, uri):
         try:
-            self.conn = libvirt.openReadOnly(uri)
+            conn = libvirt.openReadOnly(uri)
             return self.conn
         except libvirt.libvirtError, e:
             message = e.get_error_message()
@@ -62,21 +62,13 @@ class ConnectAPI(object):
 
     def openAuth(self, uri, auth, flags = 0):
         try:
-            self.conn = libvirt.openAuth(uri, auth, flags)
+            conn = libvirt.openAuth(uri, auth, flags)
             return self.conn
         except libvirt.libvirtError, e:
             message = e.get_error_message()
             code = e.get_error_code()
             raise exception.LibvirtAPI(message, code)
 
-    def close(self):
-        try:
-            self.conn.close()
-        except libvirt.libvirtError, e:
-            message = e.get_error_message()
-            code = e.get_error_code()
-            raise exception.LibvirtAPI(message, code)
-
     def get_caps(self):
         try:
             caps = self.conn.getCapabilities()
@@ -398,7 +390,8 @@ class ConnectAPI(object):
 
     def close(self):
         try:
-            return self.conn.close()
+            if self.conn:
+                return self.conn.close()
         except libvirt.libvirtError, e:
             message = e.get_error_message()
             code = e.get_error_code()
-- 
1.7.1




More information about the libvir-list mailing list