summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-03-30 19:55:17 +0200
committerhut <hut@lavabit.com>2010-03-30 19:55:17 +0200
commitda0771094b1a1e10c70e9e359296ae105b7f6ea2 (patch)
tree4440c1f9fbde9e4fdad29764a6c9117cfbded989
parent9b83f1144024c98cad6ece752b7e625bfec608f2 (diff)
downloadranger-da0771094b1a1e10c70e9e359296ae105b7f6ea2.tar.gz
ext.signal_dispatcher: fixed weak refs to bound methods
-rw-r--r--ranger/ext/signal_dispatcher.py23
-rw-r--r--test/tc_signal.py7
2 files changed, 23 insertions, 7 deletions
diff --git a/ranger/ext/signal_dispatcher.py b/ranger/ext/signal_dispatcher.py
index 2762493b..11e9c955 100644
--- a/ranger/ext/signal_dispatcher.py
+++ b/ranger/ext/signal_dispatcher.py
@@ -15,6 +15,7 @@
 
 import re
 import weakref
+from types import MethodType
 
 class Signal(dict):
 	stopped = False
@@ -48,9 +49,17 @@ class SignalDispatcher(object):
 			handlers = self._signals[signal_name]
 		except:
 			handlers = self._signals[signal_name] = []
-		nargs = function.__code__.co_argcount - hasattr(function, 'im_func')
-		if weak:
-			function = weakref.proxy(function)
+		nargs = function.__code__.co_argcount
+
+		try:
+			instance = function.__self__
+		except:
+			if weak:
+				function = weakref.proxy(function)
+		else:
+			nargs -= 1
+			if weak:
+				function = (function.__func__, weakref.proxy(function.__self__))
 		handler = SignalHandler(signal_name, function, priority, nargs > 0)
 		handlers.append(handler)
 		handlers.sort(key=lambda handler: -handler.priority)
@@ -82,10 +91,14 @@ class SignalDispatcher(object):
 		for handler in tuple(handlers):
 			if handler.active:
 				try:
+					if isinstance(handler.function, tuple):
+						fnc = MethodType(*handler.function)
+					else:
+						fnc = handler.function
 					if handler.pass_signal:
-						handler.function(signal)
+						fnc(signal)
 					else:
-						handler.function()
+						fnc()
 					if signal.stopped:
 						return
 				except ReferenceError:
diff --git a/test/tc_signal.py b/test/tc_signal.py
index 639cf7c3..ad04beab 100644
--- a/test/tc_signal.py
+++ b/test/tc_signal.py
@@ -119,13 +119,16 @@ class TestSignal(unittest.TestCase):
 
 		self.assertEqual('a', ''.join(alphabet))
 		sd.signal_bind('mysignal', foo.calc, weak=True)
-		del foo
+		print("jambo")
 
 		sd.signal_emit('mysignal')
 		self.assertEqual('ab', ''.join(alphabet))
 		self.assertFalse(is_deleted[0])
 
-		del calc
+		del foo
+
+		sd.signal_emit('mysignal')
+		self.assertEqual('ab', ''.join(alphabet))
 		self.assertTrue(is_deleted[0])