cython

Форк
0
/
decorators.pyx 
139 строк · 2.4 Кб
1
__doc__ = u"""
2
  >>> f(1,2)
3
  4
4
  >>> f.HERE
5
  1
6

7
  >>> g(1,2)
8
  5
9
  >>> g.HERE
10
  5
11

12
  >>> h(1,2)
13
  6
14
  >>> h.HERE
15
  1
16
  >>> i(4)
17
  3
18
  >>> i.HERE
19
  1
20
  >>> i_called_directly(4)
21
  3
22
  >>> i_called_directly.HERE
23
  1
24
"""
25

26
class wrap:
27
    def __init__(self, func):
28
        self.func = func
29
        self.HERE = 1
30
    def __call__(self, *args, **kwargs):
31
        return self.func(*args, **kwargs)
32

33
def decorate(func):
34
    try:
35
        func.HERE += 1
36
    except AttributeError:
37
        func = wrap(func)
38
    return func
39

40
def decorate2(a,b):
41
    return decorate
42

43
@decorate
44
def f(a,b):
45
    return a+b+1
46

47
@decorate
48
@decorate
49
@decorate
50
@decorate
51
@decorate
52
def g(a,b):
53
    return a+b+2
54

55
@decorate2(1,2)
56
def h(a,b):
57
    return a+b+3
58

59
class A:
60
    def decorate(self, func):
61
        return decorate(func)
62

63

64
a = A()
65
@a.decorate
66
def i(x):
67
    return x - 1
68

69
@A().decorate
70
def i_called_directly(x):
71
    # PEP 614 means this now works
72
    return x - 1
73

74
list_of_decorators = [decorate, decorate2]
75

76
@list_of_decorators[0]
77
def test_index_from_decorator_list0(a, b):
78
    """
79
    PEP 614 means this now works
80
    >>> test_index_from_decorator_list0(1, 2)
81
    4
82
    >>> test_index_from_decorator_list0.HERE
83
    1
84
    """
85
    return a+b+1
86

87
@list_of_decorators[1](1,2)
88
def test_index_from_decorator_list1(a, b):
89
    """
90
    PEP 614 means this now works
91
    >>> test_index_from_decorator_list1(1, 2)
92
    4
93
    >>> test_index_from_decorator_list1.HERE
94
    1
95
    """
96
    return a+b+1
97

98
def append_to_list_decorator(lst):
99
    def do_append_to_list_dec(func):
100
        def new_func():
101
            return lst + func()
102
        return new_func
103
    return do_append_to_list_dec
104

105
def outer(arg1, arg2):
106
    """
107
    ensure decorators are analysed in the correct scope
108
    https://github.com/cython/cython/issues/4367
109
    mainly intended as a compile-time test (but it does run...)
110
    >>> outer(append_to_list_decorator, [1,2,3])
111
    [1, 2, 3, 4]
112
    """
113
    @arg1([x for x in arg2])
114
    def method():
115
        return [4]
116
    return method()
117

118
class HasProperty(object):
119
    """
120
    >>> hp = HasProperty()
121
    >>> hp.value
122
    0
123
    >>> hp.value = 1
124
    >>> hp.value
125
    1
126
    """
127
    def __init__(self) -> None:
128
        self._value = 0
129

130
    @property
131
    def value(self) -> int:
132
        return self._value
133

134
    # https://github.com/cython/cython/issues/4836
135
    # The variable tracker was confusing "value" in the decorator
136
    # for "value" in the argument list
137
    @value.setter
138
    def value(self, value: int):
139
        self._value = value
140

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.