changeset 350:7ea98190a75e

Allow objects with an __call__ attribute used as event callbacks. Tested in pychan demo.
author phoku@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 04 Sep 2009 06:40:50 +0000
parents 493f7492f0ba
children 295b7ba75020
files clients/pychan_demo/pychan_test.py engine/extensions/pychan/tools.py
diffstat 2 files changed, 16 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/clients/pychan_demo/pychan_test.py	Thu Sep 03 23:22:28 2009 +0000
+++ b/clients/pychan_demo/pychan_test.py	Fri Sep 04 06:40:50 2009 +0000
@@ -76,6 +76,12 @@
 			self.widget.hide()
 		self.widget = None
 
+class TextSetter(object):
+	def __init__(self,text):
+		self.text = text
+	def __call__(self,widget):
+		widget.text = self.text
+
 class DemoApplication(basicapplication.ApplicationBase):
 	def __init__(self):
 		# Let the ApplicationBase initialise FIFE
@@ -101,7 +107,7 @@
 		# A simple hover-effect for the credits label
 		credits = self.gui.findChild(name="creditsLink")
 		# setEnterCallback is deprecated - we use it here to test it.
-		credits.setEnterCallback(lambda w : credits._setText(u"CREDITS"))
+		credits.setEnterCallback(TextSetter(u"CREDITS"))
 		# Note that we can't simply write:
 		# credits.capture(credits._setText(u"Credits"), event_name="mouseExited")
 		# that's because that would call credits._setText _NOW_ and we want to call
--- a/engine/extensions/pychan/tools.py	Thu Sep 03 23:22:28 2009 +0000
+++ b/engine/extensions/pychan/tools.py	Fri Sep 04 06:40:50 2009 +0000
@@ -40,9 +40,17 @@
 	if hasattr(func,'im_func'):
 		code = func.im_func.func_code
 		varnames = code.co_varnames[1:code.co_argcount]#ditch bound instance
-	else:
+	elif hasattr(func,'func_code'):
 		code = func.func_code
 		varnames = code.co_varnames[0:code.co_argcount]
+	elif hasattr(func,'__call__'):
+		func = func.__call__
+		if hasattr(func,'im_func'):
+			code = func.im_func.func_code
+			varnames = code.co_varnames[1:code.co_argcount]#ditch bound instance
+		elif hasattr(func,'func_code'):
+			code = func.func_code
+			varnames = code.co_varnames[0:code.co_argcount]
 
 	#http://docs.python.org/lib/inspect-types.html
 	if code.co_flags & 8: