summaryrefslogtreecommitdiff
path: root/tests/run/refcount_in_meth.pyx
blob: af62127791ea7ea416d4cc29177e7555825104bb (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python

__doc__=u"""
>>> t = RefCountInMeth()
>>> t.chk_meth()
True
>>> t.chk_nogil()
True
>>> t.chk_meth_if()
True
>>> t.chk_nogil_if()
True
"""

cimport cython
from cpython.ref cimport PyObject

@cython.always_allow_keywords(False)
def get_refcount(obj):
    return (<PyObject*>obj).ob_refcnt

cdef class RefCountInMeth(object):
    cdef double value

    def __cinit__(self):
        self.value = 1.5

    cdef double c_get_value(self) nogil:
        return self.value

    cdef double c_get_value_if(self) nogil:
        cdef double v
        if 9>4:
            v = 2.3
        return self.value

    cdef int c_meth(self):
        cdef int v

        v = get_refcount(self)
        return v

    cdef int c_meth_if(self):
        cdef int v
        if 5>6:
            v = 7
        v = get_refcount(self)
        return v

    def chk_meth(self):
        cdef int a,b

        a = get_refcount(self)
        b = self.c_meth()
        return a==b

    def chk_meth_if(self):
        cdef int a,b

        a = get_refcount(self)
        b = self.c_meth_if()
        return a==b

    def chk_nogil(self):
        cdef double v
        v = self.c_get_value()
        return v==self.value

    def chk_nogil_if(self):
        cdef double v
        v = self.c_get_value_if()
        return v==self.value