diff options
author | Robert Bradshaw <robertwb@gmail.com> | 2016-11-15 22:42:03 -0800 |
---|---|---|
committer | Robert Bradshaw <robertwb@gmail.com> | 2016-11-15 22:45:13 -0800 |
commit | 6dd263aa3d2d6d80f2d7b96a640a27538fdaa0b2 (patch) | |
tree | c3322b431249ae1eb5563083aa8b7caa3648e437 | |
parent | 5796b9b09d64ebb5ff9d4e1af609f06b589e60d0 (diff) | |
download | cython-6dd263aa3d2d6d80f2d7b96a640a27538fdaa0b2.tar.gz |
Don't infer unbond function types for bound methods.
This fixes #551.
-rw-r--r-- | Cython/Compiler/ExprNodes.py | 4 | ||||
-rw-r--r-- | tests/run/type_inference.pyx | 23 |
2 files changed, 27 insertions, 0 deletions
diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 8bfb53836..85fc39aa5 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -6309,6 +6309,10 @@ class AttributeNode(ExprNode): # builtin types cannot be inferred as C functions as # that would prevent their use as bound methods return py_object_type + elif self.entry and self.entry.is_cmethod: + # special case: bound methods should not be inferred + # as their unbound method types + return py_object_type return self.type def analyse_target_declaration(self, env): diff --git a/tests/run/type_inference.pyx b/tests/run/type_inference.pyx index ab74108f1..cf10eedb2 100644 --- a/tests/run/type_inference.pyx +++ b/tests/run/type_inference.pyx @@ -722,3 +722,26 @@ cdef class InferInProperties: d = enum_y c = d return typeof(a), typeof(b), typeof(c), typeof(d) + +cdef class WithMethods: + cdef int offset + def __init__(self, offset): + self.offset = offset + cpdef int one_arg(self, int x): + return x + self.offset + cpdef int default_arg(self, int x, int y=0): + return x + y + self.offset + +def test_bound_methods(): + """ + >>> test_bound_methods() + """ + o = WithMethods(10) + assert typeof(o) == 'WithMethods', typeof(o) + + one_arg = o.one_arg + assert one_arg(2) == 12, one_arg(2) + + default_arg = o.default_arg + assert default_arg(2) == 12, default_arg(2) + assert default_arg(2, 3) == 15, default_arg(2, 2) |