summaryrefslogtreecommitdiff
path: root/tests/run/function_self.py
blob: 938810491ee73b1792d3d2f8c81ea233c2cfb4ae (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# mode: run
# tag: pure2.7

# cython: binding=True

import cython
import sys

def regular(x):
    """
    >>> hasattr(regular, "__self__")
    False
    >>> nested = regular(10)
    >>> hasattr(nested, "__self__")
    False
    """
    def nested(y):
        return x+y
    return nested

@cython.locals(x=cython.floating)
def fused(x):
    """
    >>> nested = fused(10.)
    >>> hasattr(nested, "__self__")
    False

    #>>> hasattr(fused, "__self__")  # FIXME this fails for fused functions
    #False
    # but this is OK:
    >>> fused.__self__  #doctest: +ELLIPSIS
    Traceback (most recent call last):
        ...
    AttributeError: 'function' object has no attribute '__self__'...
    """
    def nested_in_fused(y):
        return x+y
    return nested_in_fused

# FIXME - doesn't currently work at all
#def get_nested_fused(x):
#    @cython.locals(x=cython.floating)
#    def nested_fused(y):
#        return x+y
#    return nested_fused

class C:
    """
    >>> c = C()
    >>> c.regular.__self__ is c
    True
    >>> c.fused.__self__ is c
    True
    """
    def regular(self):
        pass

    @cython.locals(x=cython.floating)
    def fused(self, x):
        return x

__doc__ = ""
if sys.version_info[0] > 2 or cython.compiled:
    __doc__ += """
    >>> hasattr(C.regular, "__self__")  # __self__==None on pure-python 2
    False

    # returns None on pure-python 2
    >>> C.fused.__self__  #doctest: +ELLIPSIS
    Traceback (most recent call last):
        ...
    AttributeError: 'function' object has no attribute '__self__'...
    """

if cython.compiled:
    __doc__ = """
    >>> fused['double'].__self__   #doctest: +ELLIPSIS
    Traceback (most recent call last):
        ...
    AttributeError: 'function' object has no attribute '__self__'...

    >>> C.fused['double'].__self__   #doctest: +ELLIPSIS
    Traceback (most recent call last):
        ...
    AttributeError: 'function' object has no attribute '__self__'...

    >>> c = C()
    >>> c.fused['double'].__self__ is c   #doctest: +ELLIPSIS
    True

    # The PR that changed __self__ also changed how __doc__ is set up slightly
    >>> fused['double'].__doc__ == fused.__doc__ and isinstance(fused.__doc__, str)
    True
    """