[vlc-commits] commit: python-vlc: remove the _EventManagers global dict (Olivier Aubert )

git at videolan.org git at videolan.org
Tue Nov 16 14:29:28 CET 2010


vlc/python | branch: master | Olivier Aubert <olivier.aubert at liris.cnrs.fr> | Tue Nov 16 10:58:43 2010 +0100| [2bb3aed0b03faa8d0ce237f08ba971d6af10d9c9] | committer: Olivier Aubert 

python-vlc: remove the _EventManagers global dict

> http://git.videolan.org/gitweb.cgi/vlc/python.git/?a=commit;h=2bb3aed0b03faa8d0ce237f08ba971d6af10d9c9
---

 footer.py   |   17 -----------------
 override.py |   31 +++++++++++++++++++++++++------
 2 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/footer.py b/footer.py
index 3c5c6a4..c6e4580 100644
--- a/footer.py
+++ b/footer.py
@@ -1,22 +1,5 @@
 ### Start of footer.py ###
 
-_EventManagers = {}
-
-# FIXME: the EventManager global dict could be removed if
-# _callback_handler was made a method of EventManager.
-_called_from_ctypes = ctypes.CFUNCTYPE(None, ctypes.POINTER(Event), ctypes.c_void_p)
- at _called_from_ctypes
-def _callback_handler(event, key):
-    '''(INTERNAL) handle callback call from ctypes.
-    '''
-    try: # retrieve Python callback and arguments
-        call, args, kwds = _EventManagers[key]._callbacks_[event.contents.type.value]
-        # FIXME: event could be dereferenced here to event.contents,
-        # this would simplify the callback code.
-        call(event, *args, **kwds)
-    except KeyError:  # detached?
-        pass
-
 def callbackmethod(f):
     """Backward compatibility with the now useless @callbackmethod decorator.
     
diff --git a/override.py b/override.py
index aabae9d..aeabb91 100644
--- a/override.py
+++ b/override.py
@@ -63,10 +63,10 @@ class Instance:
         l = []
         head = ao = libvlc_audio_output_list_get(self)
         while ao:
-            l.append( { 'name': ao.contents.name, 
+            l.append( { 'name': ao.contents.name,
                         'description': ao.contents.description,
-                        'devices': [ { 'id': libvlc_audio_output_device_id(self, ao.contents.name, i), 
-                                       'longname': libvlc_audio_output_device_longname(self, ao.contents.name, i) } 
+                        'devices': [ { 'id': libvlc_audio_output_device_id(self, ao.contents.name, i),
+                                       'longname': libvlc_audio_output_device_longname(self, ao.contents.name, i) }
                                      for i in range(libvlc_audio_output_device_count(self, ao.contents.name) ) ] } )
             ao = ao.contents.next
         libvlc_audio_output_list_release(head)
@@ -229,7 +229,7 @@ class EventManager:
         o = object.__new__(cls)
         o._as_parameter_ = ptr  # was ctypes.c_void_p(ptr)
         o._callbacks_ = {}  # 3-tuples of Python objs
-        _EventManagers[id(o)] = o  # map id to instance
+        o._callback_handler = None
         return o
 
     def event_attach(self, eventtype, callback, *args, **kwds):
@@ -250,7 +250,26 @@ class EventManager:
         if not hasattr(callback, '__call__'):  # callable()
             raise LibVLCException("%s required: %r" % ('callable', callback))
 
-        r = libvlc_event_attach(self, eventtype, _callback_handler, id(self))
+        if self._callback_handler is None:
+            _called_from_ctypes = ctypes.CFUNCTYPE(None, ctypes.POINTER(Event), ctypes.c_void_p)
+            @_called_from_ctypes
+            def _callback_handler(event, data):
+                """(INTERNAL) handle callback call from ctypes.
+
+                Note: we cannot simply make this an instance method of
+                EventManager since ctypes callback does not append
+                self as first parameter. Hence we use a closure.
+                """
+                try: # retrieve Python callback and arguments
+                    call, args, kwds = self._callbacks_[event.contents.type.value]
+                    # FIXME: event could be dereferenced here to event.contents,
+                    # this would simplify the callback code.
+                    call(event, *args, **kwds)
+                except KeyError:  # detached?
+                    pass
+            self._callback_handler = _callback_handler
+
+        r = libvlc_event_attach(self, eventtype, self._callback_handler, None)
         if not r:
             self._callbacks_[eventtype.value] = (callback, args, kwds)
         return r
@@ -266,4 +285,4 @@ class EventManager:
         t = eventtype.value
         if t in self._callbacks_:
             del self._callbacks_[t] # remove, regardless of libvlc return value
-            libvlc_event_detach(self, eventtype, _callback_handler, id(self))
+            libvlc_event_detach(self, eventtype, self._callback_handler, None)



More information about the vlc-commits mailing list