From: Ben Darnell Date: Tue, 15 Sep 2015 00:29:53 +0000 (-0400) Subject: Clarify comment about `@cython.binding`. X-Git-Tag: v4.3.0b1~46 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=631187aec623f68e1d3d4731082704901b648a58;p=thirdparty%2Ftornado.git Clarify comment about `@cython.binding`. It's only necessary for static functions, not methods. --- diff --git a/maint/test/cython/cythonapp.pyx b/maint/test/cython/cythonapp.pyx index 0539a2289..54d20661e 100644 --- a/maint/test/cython/cythonapp.pyx +++ b/maint/test/cython/cythonapp.pyx @@ -16,7 +16,14 @@ def decorated_coroutine(): 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) diff --git a/maint/test/cython/cythonapp_test.py b/maint/test/cython/cythonapp_test.py index 975194ebd..c3312de41 100644 --- a/maint/test/cython/cythonapp_test.py +++ b/maint/test/cython/cythonapp_test.py @@ -25,10 +25,18 @@ class CythonCoroutineTest(AsyncTestCase): 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], {})) diff --git a/tornado/util.py b/tornado/util.py index ce5362dc0..a67ddf50d 100644 --- a/tornado/util.py +++ b/tornado/util.py @@ -301,10 +301,11 @@ class ArgReplacer(object): 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