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
|
# mode: run
# ticket: t593
# tag: property, decorator
"""
>>> am_i_buggy
False
>>> Foo
False
"""
def testme(func):
try:
am_i_buggy
return True
except NameError:
return False
@testme
def am_i_buggy():
pass
def testclass(klass):
try:
Foo
return True
except NameError:
return False
@testclass
class Foo:
pass
def called_deco(a,b,c):
def count(f):
a.append( (b,c) )
return f
return count
L = []
@called_deco(L, 5, c=6)
@called_deco(L, c=3, b=4)
@called_deco(L, 1, 2)
def wrapped_func(x):
"""
>>> L
[(1, 2), (4, 3), (5, 6)]
>>> wrapped_func(99)
99
>>> L
[(1, 2), (4, 3), (5, 6)]
"""
return x
def class_in_closure(x):
"""
>>> C1, c0 = class_in_closure(5)
>>> C1().smeth1()
(5, ())
>>> C1.smeth1(1,2)
(5, (1, 2))
>>> C1.smeth1()
(5, ())
>>> c0.smeth0()
1
>>> c0.__class__.smeth0()
1
"""
class ClosureClass1(object):
@staticmethod
def smeth1(*args):
return x, args
class ClosureClass0(object):
@staticmethod
def smeth0():
return 1
return ClosureClass1, ClosureClass0()
def class_not_in_closure():
"""
>>> c = class_not_in_closure()
>>> c.smeth0()
1
>>> c.__class__.smeth0()
1
"""
class ClosureClass0(object):
@staticmethod
def smeth0():
return 1
return ClosureClass0()
class ODict(dict):
def __init__(self):
dict.__init__(self)
self._order = []
dict.__setitem__(self, '_order', self._order)
def __setitem__(self, key, value):
dict.__setitem__(self, key, value)
self._order.append(key)
class Base(type):
@staticmethod
def __prepare__(*args, **kwargs):
return ODict()
class Bar(metaclass=Base):
"""
>>> [n for n in Bar._order if n not in {"__qualname__", "__annotations__"}]
['__module__', '__doc__', 'bar']
"""
@property
def bar(self):
return 0
|