It's only necessary for static functions, not methods.
return "goodbye"
# The binding directive is necessary for compatibility with
-# ArgReplacer (and therefore return_future).
+# ArgReplacer (and therefore return_future), but only because
+# this is a static function.
@cython.binding(True)
def function_with_args(one, two, three):
return (one, two, three)
+
+
+class AClass:
+ # methods don't need the binding directive.
+ def method_with_args(one, two, three):
+ return (one, two, three)
class CythonArgReplacerTest(unittest.TestCase):
- def test_arg_replacer(self):
+ def test_arg_replacer_function(self):
replacer = ArgReplacer(cythonapp.function_with_args, 'two')
args = (1, 'old', 3)
kwargs = {}
self.assertEqual(replacer.get_old_value(args, kwargs), 'old')
self.assertEqual(replacer.replace('new', args, kwargs),
('old', [1, 'new', 3], {}))
+
+ def test_arg_replacer_method(self):
+ replacer = ArgReplacer(cythonapp.AClass().method_with_args, 'two')
+ args = (1, 'old', 3)
+ kwargs = {}
+ self.assertEqual(replacer.get_old_value(args, kwargs), 'old')
+ self.assertEqual(replacer.replace('new', args, kwargs),
+ ('old', [1, 'new', 3], {}))
except TypeError:
if hasattr(func, 'func_code'):
# Cython-generated code has all the attributes needed
- # by inspect.getargspec (when the
- # @cython.binding(True) directive is used), but the
- # inspect module only works with ordinary functions.
- # Inline the portion of getargspec that we need here.
+ # by inspect.getargspec, but the inspect module only
+ # works with ordinary functions. Inline the portion of
+ # getargspec that we need here. Note that for static
+ # functions the @cython.binding(True) decorator must
+ # be used (for methods it works out of the box).
code = func.func_code
return code.co_varnames[:code.co_argcount]
raise