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

[libvirt] [PATCH] python: virConnect: fix destructor



Fixed virConnect destructor checking for attribute
existance before trying to use it.
Avoids:
Exception AttributeError: AttributeError("virConnect instance has  no
attribute 'domainEventCallbacks'",) in <bound method  virConnect.__del__
of <libvirt.virConnect instance at  0x4280f38>> ignored

However, something still doesn't work:
Exception TypeError: TypeError("'NoneType' object is not callable",)
in <bound method virConnect.__del__ of <libvirt.virConnect object at 0x37576d0>> ignored

Signed-off-by: Sandro Bonazzola <sbonazzo redhat com>
---
 python/libvirt-override-virConnect.py | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)

diff --git a/python/libvirt-override-virConnect.py b/python/libvirt-override-virConnect.py
index 5495b70..28d6d41 100644
--- a/python/libvirt-override-virConnect.py
+++ b/python/libvirt-override-virConnect.py
@@ -1,11 +1,12 @@
     def __del__(self):
-        try:
-           for cb,opaque in self.domainEventCallbacks.items():
-               del self.domainEventCallbacks[cb]
-           del self.domainEventCallbacks
-           libvirtmod.virConnectDomainEventDeregister(self._o, self)
-        except AttributeError:
-           pass
+        if hasattr(self, 'domainEventCallbacks'):
+            try:
+                for cb,opaque in self.domainEventCallbacks.items():
+                    del self.domainEventCallbacks[cb]
+                del self.domainEventCallbacks
+                libvirtmod.virConnectDomainEventDeregister(self._o, self)
+            except AttributeError:
+                pass
 
         if self._o != None:
             libvirtmod.virConnectClose(self._o)
@@ -14,14 +15,15 @@
     def domainEventDeregister(self, cb):
         """Removes a Domain Event Callback. De-registering for a
            domain callback will disable delivery of this event type """
-        try:
-            del self.domainEventCallbacks[cb]
-            if len(self.domainEventCallbacks) == 0:
-                del self.domainEventCallbacks
-                ret = libvirtmod.virConnectDomainEventDeregister(self._o, self)
-                if ret == -1: raise libvirtError ('virConnectDomainEventDeregister() failed', conn=self)
-        except AttributeError:
-            pass
+        if hasattr(self, 'domainEventCallbacks'):
+            try:
+                del self.domainEventCallbacks[cb]
+                if len(self.domainEventCallbacks) == 0:
+                    del self.domainEventCallbacks
+                    ret = libvirtmod.virConnectDomainEventDeregister(self._o, self)
+                    if ret == -1: raise libvirtError ('virConnectDomainEventDeregister() failed', conn=self)
+            except AttributeError:
+                pass
 
     def domainEventRegister(self, cb, opaque):
         """Adds a Domain Event Callback. Registering for a domain
-- 
1.8.1.4


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