From 4e3305270351cb7a4f4d2a6d8fa4ded1e88bf701 Mon Sep 17 00:00:00 2001 From: Dave Jones Date: Wed, 23 Sep 2015 13:47:32 +0100 Subject: [PATCH] Fix #28 Re-order the test bindings to prefer using no parameters if possible --- gpiozero/input_devices.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/gpiozero/input_devices.py b/gpiozero/input_devices.py index 5dd3a28..bef371d 100644 --- a/gpiozero/input_devices.py +++ b/gpiozero/input_devices.py @@ -75,18 +75,20 @@ class WaitableInputDevice(InputDevice): raise InputDeviceError('value must be None or a callable') else: # Try binding ourselves to the argspec of the provided callable. - # If this works, assume the function is capable of accepting us - # as the only (mandatory) parameter. + # If this works, assume the function is capable of accepting no + # parameters try: - inspect.getcallargs(fn, self) - @wraps(fn) - def wrapper(): - return fn(self) - return wrapper + inspect.getcallargs(fn) + return fn except TypeError: try: - inspect.getcallargs(fn) - return fn + # If the above fails, try binding with a single parameter + # (ourselves). If this works, wrap the specified callback + inspect.getcallargs(fn, self) + @wraps(fn) + def wrapper(): + return fn(self) + return wrapper except TypeError: raise InputDeviceError( 'value must be a callable which accepts up to one '