summaryrefslogtreecommitdiff
path: root/tests/run/closure_self.pyx
blob: 0150bee405231aa9233824a34bf693cc126c634c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# mode: run
# tag: closures
cdef class Test:
    cdef int x

cdef class SelfInClosure(object):
    cdef Test _t
    cdef int x

    def plain(self):
        """
        >>> o = SelfInClosure()
        >>> o.plain()
        1
        """
        self.x = 1
        return self.x

    def closure_method(self):
        """
        >>> o = SelfInClosure()
        >>> o.closure_method()() == o
        True
        """
        def nested():
            return self
        return nested

    def closure_method_cdef_attr(self, Test t):
        """
        >>> o = SelfInClosure()
        >>> o.closure_method_cdef_attr(Test())()
        (1, 2)
        """
        t.x = 2
        self._t = t
        self.x = 1
        def nested():
            return self.x, t.x
        return nested

    def call_closure_method_cdef_attr_c(self, Test t):
        """
        >>> o = SelfInClosure()
        >>> o.call_closure_method_cdef_attr_c(Test())()
        (1, 2)
        """
        return self.closure_method_cdef_attr_c(t)

    cdef closure_method_cdef_attr_c(self, Test t):
        t.x = 2
        self._t = t
        self.x = 1
        def nested():
            return self.x, t.x
        return nested