File: pep3135_class_cell.py

package info (click to toggle)
cython 3.1.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,932 kB
  • sloc: python: 92,172; ansic: 19,275; cpp: 1,407; xml: 1,031; javascript: 511; makefile: 373; sh: 223; sed: 11
file content (291 lines) | stat: -rw-r--r-- 5,255 bytes parent folder | download
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# cython: language_level=3
# mode: run
# tag: pep3135, pure3.0

import cython

class C(object):
    """
    >>> obj = C()
    >>> obj.method_1()
    2
    >>> obj.method_2()
    3
    >>> obj.method_3()
    ['__class__', 'self']
    >>> obj.method_4()
    ['self']
    >>> list(obj.generator())
    ['C']

    >>> C.l() == C
    True
    """

    @classmethod
    def class_method(cls):
        return 2

    @staticmethod
    def static_method():
        return 3

    def method_1(self):
        return __class__.class_method()

    def method_2(self):
        return __class__.static_method()

    def method_3(self):
        __class__
        return sorted(list(locals().keys()))

    def method_4(self):
        return sorted(list(locals().keys()))

    def generator(self):
        yield __class__.__name__

    l = lambda: __class__


@cython.cclass
class CC(object):
    """
    >>> obj = CC()
    >>> obj.method_1()
    2
    >>> obj.method_2()
    3
    >>> obj.method_3()
    ['__class__', 'self']
    >>> obj.method_4()
    ['self']
    >>> list(obj.generator())
    ['CC']

    >>> CC.l() == CC
    True
    """

    @classmethod
    def class_method(cls):
        return 2

    @staticmethod
    def static_method():
        return 3

    def method_1(self):
        return __class__.class_method()

    def method_2(self):
        return __class__.static_method()

    def method_3(self):
        __class__
        return sorted(list(locals().keys()))

    def method_4(self):
        return sorted(list(locals().keys()))

    def generator(self):
        yield __class__.__name__

    l = lambda: __class__


class D:
    """
    >>> obj = D()
    >>> obj.method(1)
    1
    >>> obj.method(0)  # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ...
    UnboundLocalError: ... '__class__' ...
    """
    def method(self, x):
        if x: __class__ = x
        print(__class__)


@cython.cclass
class CD:
    """
    >>> obj = CD()
    >>> obj.method(1)
    1
    >>> obj.method(0)  # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ...
    UnboundLocalError: ... '__class__' ...
    """
    def method(self, x):
        if x: __class__ = x
        print(__class__)


class E:
    """
    >>> obj = E()
    >>> obj.method()().__name__
    'E'
    >>> obj.method2()()().__name__
    'E'
    """
    def method(self):
        def inner(): return __class__
        return inner

    def method2(self):
        def inner():
            def inner_inner():
                return __class__
            return inner_inner
        return inner


@cython.cclass
class CE:
    """
    >>> obj = CE()
    >>> obj.method()().__name__
    'CE'
    >>> obj.method2()()().__name__
    'CE'
    """
    def method(self):
        def inner(): return __class__
        return inner

    def method2(self):
        def inner():
            def inner_inner():
                return __class__
            return inner_inner
        return inner


class G:
    """
    >>> obj = G()
    >>> obj.method().__name__
    'H'
    """
    def method(self):
        class H:
            def inner(self):
                return __class__
        return H().inner()


class I:
    """
    >>> obj = I()
    >>> obj.method()()().__name__
    'J'
    """
    def method(self):
        def inner():
            class J:
                def inner(self):
                    return __class__
            return J().inner
        return inner


class K:
    def method(self): return __class__


@cython.cclass
class CK:
    def method(self): return __class__


__test__ = {
    "k": """
    >>> OldK = K
    >>> K = None
    >>> OldK().method().__name__
    'K'
    """,
    "ck": """
    >>> OldCK = CK
    >>> CK = None
    >>> OldCK().method().__name__
    'CK'
    """
}

class L:
    """
    >>> OldL().method().__name__
    'L'
    """
    def method(self): return self.__class__

OldL = L
L = None

class M:
    """
    >>> M().method()
    'overwritten'
    """
    def method(self):
        __class__ = 'overwritten'
        return __class__

@cython.cclass
class CM:
    """
    >>> CM().method()
    'overwritten'
    """
    def method(self):
        __class__ = 'overwritten'
        return __class__


class N:
    """
    >>> N().method().__name__
    'N'
    """
    __class__ = 'N'
    def method(self):
        return __class__

if cython.compiled:
    @cython.cclass
    class CDefFuncTest:
        """
        >>> obj = CDefFuncTest()
        >>> obj.call_cfunc1().__name__
        'CDefFuncTest'

        #>>> obj.call_cfunc2()().__name__ - GH 4092
        #'CDefFuncTest'
        >>> obj.call_cfunc3()
        ['__class__', 'self']
        """
        @cython.cfunc
        def cfunc1(self):
            return __class__
        def call_cfunc1(self):
            return self.cfunc1()
        #@cython.cfunc - disabled, GH 4092. This works outside pure Python mode
        #def cfunc2(self):
        #    def inner():
        #        return __class__
        #    return inner
        def call_cfunc2(self):
            return self.cfunc2()
        @cython.cfunc
        def cfunc3(self):
            __class__
            return sorted(list(locals().keys()))
        def call_cfunc3(self):
            return self.cfunc3()