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
|
# Test that __set_name__ can access and mutate its owner argument.
def skip_if_no_descriptors():
class Descriptor:
def __get__(self, obj, cls):
return
class TestClass:
Forward = Descriptor()
a = TestClass()
try:
a.__class__
except AttributeError:
# Target doesn't support __class__.
print("SKIP")
raise SystemExit
b = a.Forward
if "Descriptor" in repr(b.__class__):
# Target doesn't support descriptors.
print("SKIP")
raise SystemExit
skip_if_no_descriptors()
# Test basic accesses and mutations.
class GetSibling:
def __set_name__(self, owner, name):
print(getattr(owner, name + "_sib"))
class GetSiblingTest:
desc = GetSibling()
desc_sib = 111
t110 = GetSiblingTest()
class SetSibling:
def __set_name__(self, owner, name):
setattr(owner, name + "_sib", 121)
class SetSiblingTest:
desc = SetSibling()
t120 = SetSiblingTest()
print(t120.desc_sib)
class DelSibling:
def __set_name__(self, owner, name):
delattr(owner, name + "_sib")
class DelSiblingTest:
desc = DelSibling()
desc_sib = 131
t130 = DelSiblingTest()
try:
print(t130.desc_sib)
except AttributeError:
print("AttributeError")
class GetSelf:
x = 211
def __set_name__(self, owner, name):
print(getattr(owner, name).x)
class GetSelfTest:
desc = GetSelf()
t210 = GetSelfTest()
class SetSelf:
def __set_name__(self, owner, name):
setattr(owner, name, 221)
class SetSelfTest:
desc = SetSelf()
t220 = SetSelfTest()
print(t220.desc)
class DelSelf:
def __set_name__(self, owner, name):
delattr(owner, name)
class DelSelfTest:
desc = DelSelf()
t230 = DelSelfTest()
try:
print(t230.desc)
except AttributeError:
print("AttributeError")
# Test exception behavior.
class Raise:
def __set_name__(self, owner, name):
raise Exception()
try:
class RaiseTest:
desc = Raise()
except Exception as e: # CPython raises RuntimeError, MicroPython propagates the original exception
print("Exception")
# Ensure removed/overwritten class members still get __set_name__ called.
class SetSpecific:
def __init__(self, sib_name, sib_replace):
self.sib_name = sib_name
self.sib_replace = sib_replace
def __set_name__(self, owner, name):
setattr(owner, self.sib_name, self.sib_replace)
class SetReplaceTest:
a = SetSpecific("b", 312) # one of these is changed first
b = SetSpecific("a", 311)
t310 = SetReplaceTest()
print(t310.a)
print(t310.b)
class DelSpecific:
def __init__(self, sib_name):
self.sib_name = sib_name
def __set_name__(self, owner, name):
delattr(owner, self.sib_name)
class DelReplaceTest:
a = DelSpecific("b") # one of these is removed first
b = DelSpecific("a")
t320 = DelReplaceTest()
try:
print(t320.a)
except AttributeError:
print("AttributeError")
try:
print(t320.b)
except AttributeError:
print("AttributeError")
|