[libvirt] [PATCH libvirt-python 2/2] event-test.py: Future proof lifecycle event handling

Philipp Hahn posted 2 patches 6 years, 2 months ago
There is a newer version of this series
[libvirt] [PATCH libvirt-python 2/2] event-test.py: Future proof lifecycle event handling
Posted by Philipp Hahn 6 years, 2 months ago
Catch exception if the event or details is outside the known type to
prevent a traceback like the following:
> raceback (most recent call last):
>   File "/usr/lib/python2.7/dist-packages/libvirt.py", line 4601, in _dispatchDomainEventCallbacks
>     cb(self, virDomain(self, _obj=dom), event, detail, opaque)
>   File "libvirt-python/examples/event-test.py", line 505, in myDomainEventCallback1
>     domDetailToString(event, detail)))
>   File "libvirt-python/examples/event-test.py", line 484, in domDetailToString
>     return domEventStrings[event][detail]
> IndexError: tuple index out of range

Signed-off-by: Philipp Hahn <hahn@univention.de>
---
 examples/event-test.py | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/examples/event-test.py b/examples/event-test.py
index 04310e1..4632110 100755
--- a/examples/event-test.py
+++ b/examples/event-test.py
@@ -467,7 +467,10 @@ def domEventToString(event):
                      "PMSuspended",
                      "Crashed",
     )
-    return domEventStrings[event]
+    try:
+        return domEventStrings[event]
+    except IndexError:
+        return 'Unknown domain event %d' % (event,)
 
 def domDetailToString(event, detail):
     domEventStrings = (
@@ -481,7 +484,10 @@ def domDetailToString(event, detail):
         ( "Memory", "Disk" ),
         ( "Panicked", ),
         )
-    return domEventStrings[event][detail]
+    try:
+        return domEventStrings[event][detail]
+    except IndexError:
+        return 'Unknown domain event %d detail %d' % (event, detail)
 
 def blockJobTypeToString(type):
     blockJobTypes = ( "unknown", "Pull", "Copy", "Commit", "ActiveCommit", )
-- 
2.11.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list