python: events: Fix C->Python handle callback prototype

If registering our own event loop implementation written in python,
any handles or timeouts callbacks registered by libvirt C code must
be wrapped in a python function. There is some argument trickery that
makes this all work, by wrapping the user passed opaque value in
a tuple, along with the callback function.

Problem is, the current setup requires the user's event loop to know
about this trickery, rather than just treating the opaque value
as truly opaque.

Fix this in a backwards compatible manner, and adjust the example
python event loop to do things the proper way.
This commit is contained in:
Cole Robinson 2011-06-15 20:02:27 -04:00
parent d0e3f3d6a6
commit f2fb235b1d
2 changed files with 26 additions and 6 deletions

View File

@ -66,8 +66,7 @@ class virEventLoopPure:
self.cb(self.handle,
self.fd,
events,
self.opaque[0],
self.opaque[1])
self.opaque)
# This class contains the data we need to track for a
# single periodic timer
@ -96,8 +95,7 @@ class virEventLoopPure:
def dispatch(self):
self.cb(self.timer,
self.opaque[0],
self.opaque[1])
self.opaque)
def __init__(self):

View File

@ -117,19 +117,41 @@ def getVersion (name = None):
#
# Invoke an EventHandle callback
#
def eventInvokeHandleCallback (watch, fd, event, callback, opaque):
def eventInvokeHandleCallback(watch, fd, event, opaque, opaquecompat=None):
"""
Invoke the Event Impl Handle Callback in C
"""
# libvirt 0.9.2 and earlier required custom event loops to know
# that opaque=(cb, original_opaque) and pass the values individually
# to this wrapper. This should handle the back compat case, and make
# future invocations match the virEventHandleCallback prototype
if opaquecompat:
callback = opaque
opaque = opaquecompat
else:
callback = opaque[0]
opaque = opaque[1]
libvirtmod.virEventInvokeHandleCallback(watch, fd, event, callback, opaque);
#
# Invoke an EventTimeout callback
#
def eventInvokeTimeoutCallback (timer, callback, opaque):
def eventInvokeTimeoutCallback(timer, opaque, opaquecompat=None):
"""
Invoke the Event Impl Timeout Callback in C
"""
# libvirt 0.9.2 and earlier required custom event loops to know
# that opaque=(cb, original_opaque) and pass the values individually
# to this wrapper. This should handle the back compat case, and make
# future invocations match the virEventTimeoutCallback prototype
if opaquecompat:
callback = opaque
opaque = opaquecompat
else:
callback = opaque[0]
opaque = opaque[1]
libvirtmod.virEventInvokeTimeoutCallback(timer, callback, opaque);
def _dispatchEventHandleCallback(watch, fd, events, cbData):