Skip to content

Commit 2ecf186

Browse files
committed
Use builtin operators in weakref.proxy to match CPython error behavior
1 parent ec3dee5 commit 2ecf186

File tree

1 file changed

+92
-120
lines changed

1 file changed

+92
-120
lines changed

graalpython/lib-graalpython/_weakref.py

Lines changed: 92 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -37,250 +37,222 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40-
from _descriptor import descriptor
41-
4240
class CallableProxyType(object):
4341
pass
4442

43+
4544
# weak refs are used to allow circular data structures to be collected.
4645
# Java's GC has no problem with these, so we can make this a simple proxy.
4746
class ProxyType(object):
4847
def __init__(self, other):
4948
object.__setattr__(self, "value", other)
5049

51-
def __getattribute__(self, key):
52-
return object.__getattribute__(self, "value").__getattribute__(key)
50+
def __getattr__(self, key):
51+
return getattr(object.__getattribute__(self, "value"), key)
5352

5453
def __setattr__(self, key, value):
55-
return object.__getattribute__(self, "value").__setattr__(key, value)
54+
setattr(object.__getattribute__(self, "value"), key, value)
5655

5756
def __delattr__(self, key):
58-
return object.__getattribute__(self, "value").__delattr__(key)
57+
return delattr(object.__getattribute__(self, "value"), key)
5958

6059
def __getitem__(self, key):
61-
return object.__getattribute__(self, "value").__getitem__(key)
60+
return object.__getattribute__(self, "value")[key]
6261

6362
def __setitem__(self, key, value):
64-
return object.__getattribute__(self, "value").__setitem__(key, value)
65-
66-
def __delitem__(self, other):
67-
return object.__getattribute__(self, "value").__delitem__(other)
63+
object.__getattribute__(self, "value")[key] = value
6864

69-
def __ceil__(self):
70-
return object.__getattribute__(self, "value").__ceil__()
71-
72-
def __floor__(self):
73-
return object.__getattribute__(self, "value").__floor__()
65+
def __delitem__(self, key):
66+
del object.__getattribute__(self, "value")[key]
7467

7568
def __repr__(self):
7669
return f"<weakproxy at {id(self)} to {type(self).__name__} at {id(object.__getattribute__(self, 'value'))}>"
7770

7871
def __str__(self):
79-
return object.__getattribute__(self, "value").__str__()
72+
return str(object.__getattribute__(self, "value"))
8073

8174
def __bytes__(self):
8275
return object.__getattribute__(self, "value").__bytes__()
8376

84-
def __format__(self, format_spec):
85-
return object.__getattribute__(self, "value").__format__(self, format_spec)
86-
8777
def __lt__(self, other):
88-
return object.__getattribute__(self, "value").__lt__(other)
78+
return object.__getattribute__(self, "value") < other
8979

9080
def __le__(self, other):
91-
return object.__getattribute__(self, "value").__le__(other)
81+
return object.__getattribute__(self, "value") <= other
9282

9383
def __eq__(self, other):
94-
return object.__getattribute__(self, "value").__eq__(other)
84+
return object.__getattribute__(self, "value") == other
9585

9686
def __ne__(self, other):
97-
return object.__getattribute__(self, "value").__ne__(other)
87+
return object.__getattribute__(self, "value") != other
9888

9989
def __gt__(self, other):
100-
return object.__getattribute__(self, "value").__gt__(other)
90+
return object.__getattribute__(self, "value") > other
10191

10292
def __ge__(self, other):
103-
return object.__getattribute__(self, "value").__ge__(other)
93+
return object.__getattribute__(self, "value") >= other
10494

10595
def __hash__(self):
106-
return object.__getattribute__(self, "value").__hash__()
96+
return hash(object.__getattribute__(self, "value"))
10797

10898
def __bool__(self):
109-
return object.__getattribute__(self, "value").__bool__()
99+
return bool(object.__getattribute__(self, "value"))
110100

111101
def __call__(self, *args):
112-
return object.__getattribute__(self, "value").__call__(*args)
102+
return object.__getattribute__(self, "value")(*args)
113103

114104
def __len__(self):
115-
return object.__getattribute__(self, "value").__len__()
105+
return len(object.__getattribute__(self, "value"))
116106

117107
def __iter__(self):
118-
return object.__getattribute__(self, "value").__iter__()
108+
return iter(object.__getattribute__(self, "value"))
119109

120110
def __next__(self):
121-
return object.__getattribute__(self, "value").__next__()
111+
return next(object.__getattribute__(self, "value"))
122112

123113
def __reversed__(self):
124114
return object.__getattribute__(self, "value").__reversed__()
125115

126116
def __contains__(self, obj):
127-
return object.__getattribute__(self, "value").__contains__(obj)
117+
return obj in object.__getattribute__(self, "value")
128118

129119
def __add__(self, other):
130-
return object.__getattribute__(self, "value").__add__(other)
120+
return object.__getattribute__(self, "value") + other
131121

132122
def __sub__(self, other):
133-
return object.__getattribute__(self, "value").__sub__(other)
123+
return object.__getattribute__(self, "value") - other
134124

135125
def __mul__(self, other):
136-
return object.__getattribute__(self, "value").__mul__(other)
126+
return object.__getattribute__(self, "value") * other
137127

138128
def __matmul__(self, other):
139-
return object.__getattribute__(self, "value").__matmul__(other)
129+
return object.__getattribute__(self, "value") @ other
140130

141131
def __truediv__(self, other):
142-
return object.__getattribute__(self, "value").__truediv__(other)
143-
144-
def __trunc__(self):
145-
return object.__getattribute__(self, "value").__trunc__()
132+
return object.__getattribute__(self, "value") / other
146133

147134
def __floordiv__(self, other):
148-
return object.__getattribute__(self, "value").__floordiv__(other)
135+
return object.__getattribute__(self, "value") // other
149136

150137
def __mod__(self, other):
151-
return object.__getattribute__(self, "value").__mod__(other)
138+
return object.__getattribute__(self, "value") % other
152139

153140
def __divmod__(self, other):
154-
return object.__getattribute__(self, "value").__divmod__(other)
141+
return divmod(object.__getattribute__(self, "value"), other)
155142

156143
def __pow__(self, exp, mod=None):
157-
if mod is None:
158-
return object.__getattribute__(self, "value").__pow__(exp)
159-
return object.__getattribute__(self, "value").__pow__(exp, mod)
144+
return pow(object.__getattribute__(self, "value"), exp, mod)
160145

161146
def __lshift__(self, other):
162-
return object.__getattribute__(self, "value").__lshift__(other)
147+
return object.__getattribute__(self, "value") << other
163148

164149
def __rshift__(self, other):
165-
return object.__getattribute__(self, "value").__rshift__(other)
150+
return object.__getattribute__(self, "value") >> other
166151

167152
def __and__(self, other):
168-
return object.__getattribute__(self, "value").__and__(other)
153+
return object.__getattribute__(self, "value") & other
169154

170155
def __xor__(self, other):
171-
return object.__getattribute__(self, "value").__xor__(other)
156+
return object.__getattribute__(self, "value") ^ other
172157

173158
def __or__(self, other):
174-
return object.__getattribute__(self, "value").__or__(other)
175-
176-
def __radd__(self, other):
177-
return object.__getattribute__(self, "value").__radd__(other)
178-
179-
def __rsub__(self, other):
180-
return object.__getattribute__(self, "value").__rsub__(other)
181-
182-
def __rmul__(self, other):
183-
return object.__getattribute__(self, "value").__rmul__(other)
184-
185-
def __rmatmul__(self, other):
186-
return object.__getattribute__(self, "value").__rmatmul__(other)
187-
188-
def __rtruediv__(self, other):
189-
return object.__getattribute__(self, "value").__rtruediv__(other)
190-
191-
def __rfloordiv__(self, other):
192-
return object.__getattribute__(self, "value").__rfloordiv__(other)
193-
194-
def __rmod__(self, other):
195-
return object.__getattribute__(self, "value").__rmod__(other)
196-
197-
def __rdivmod__(self, other):
198-
return object.__getattribute__(self, "value").__rdivmod__(other)
199-
200-
def __rpow__(self, exp, mod=None):
201-
if mod is None:
202-
return object.__getattribute__(self, "value").__rpow__(exp)
203-
return object.__getattribute__(self, "value").__rpow__(exp, mod)
204-
205-
def __rlshift__(self, other):
206-
return object.__getattribute__(self, "value").__rlshift__(other)
207-
208-
def __rand__(self, other):
209-
return object.__getattribute__(self, "value").__rand__(other)
210-
211-
def __rxor__(self, other):
212-
return object.__getattribute__(self, "value").__rxor__(other)
213-
214-
def __ror__(self, other):
215-
return object.__getattribute__(self, "value").__ror__(other)
159+
return object.__getattribute__(self, "value") | other
216160

217161
def __iadd__(self, other):
218-
return object.__getattribute__(self, "value").__iadd__(other)
162+
value = object.__getattribute__(self, "value")
163+
value += other
164+
return value
219165

220166
def __isub__(self, other):
221-
return object.__getattribute__(self, "value").__isub__(other)
167+
value = object.__getattribute__(self, "value")
168+
value -= other
169+
return value
222170

223171
def __imul__(self, other):
224-
return object.__getattribute__(self, "value").__imul__(other)
172+
value = object.__getattribute__(self, "value")
173+
value *= other
174+
return value
225175

226176
def __imatmul__(self, other):
227-
return object.__getattribute__(self, "value").__imatmul__(other)
177+
value = object.__getattribute__(self, "value")
178+
value @= other
179+
return value
228180

229181
def __itruediv__(self, other):
230-
return object.__getattribute__(self, "value").__itruediv__(other)
182+
value = object.__getattribute__(self, "value")
183+
value /= other
184+
return value
231185

232186
def __ifloordiv__(self, other):
233-
return object.__getattribute__(self, "value").__ifloordiv__(other)
187+
value = object.__getattribute__(self, "value")
188+
value //= other
189+
return value
234190

235191
def __imod__(self, other):
236-
return object.__getattribute__(self, "value").__imod__(other)
192+
value = object.__getattribute__(self, "value")
193+
value %= other
194+
return value
237195

238-
def __ipow__(self, exp, mod=None):
239-
if mod is None:
240-
return object.__getattribute__(self, "value").__ipow__(exp)
241-
return object.__getattribute__(self, "value").__ipow__(exp, mod)
196+
def __ipow__(self, exp):
197+
value = object.__getattribute__(self, "value")
198+
value **= exp
199+
return value
242200

243201
def __ilshift__(self, other):
244-
return object.__getattribute__(self, "value").__ilshift__(other)
202+
value = object.__getattribute__(self, "value")
203+
value <<= other
204+
return value
245205

246206
def __irshift__(self, other):
247-
return object.__getattribute__(self, "value").__irshift__(other)
207+
value = object.__getattribute__(self, "value")
208+
value >>= other
209+
return value
248210

249211
def __iand__(self, other):
250-
return object.__getattribute__(self, "value").__iand__(other)
212+
value = object.__getattribute__(self, "value")
213+
value &= other
214+
return value
251215

252216
def __ixor__(self, other):
253-
return object.__getattribute__(self, "value").__ixor__(other)
217+
value = object.__getattribute__(self, "value")
218+
value ^= other
219+
return value
254220

255221
def __ior__(self, other):
256-
return object.__getattribute__(self, "value").__ior__(other)
222+
value = object.__getattribute__(self, "value")
223+
value |= other
224+
return value
257225

258226
def __neg__(self):
259-
return object.__getattribute__(self, "value").__neg__()
227+
return -object.__getattribute__(self, "value")
260228

261229
def __pos__(self):
262-
return object.__getattribute__(self, "value").__pos__()
230+
return +object.__getattribute__(self, "value")
263231

264232
def __abs__(self):
265-
return object.__getattribute__(self, "value").__abs__()
233+
return abs(object.__getattribute__(self, "value"))
266234

267235
def __invert__(self):
268-
return object.__getattribute__(self, "value").__invert__()
269-
270-
def __complex__(self):
271-
return object.__getattribute__(self, "value").__complex__()
236+
return ~object.__getattribute__(self, "value")
272237

273238
def __int__(self):
274-
return object.__getattribute__(self, "value").__int__()
239+
return int(object.__getattribute__(self, "value"))
275240

276241
def __float__(self):
277-
return object.__getattribute__(self, "value").__float__()
242+
return float(object.__getattribute__(self, "value"))
278243

279244
def __round__(self):
280-
return object.__getattribute__(self, "value").__round__()
245+
return round(object.__getattribute__(self, "value"))
281246

282247
def __index__(self):
283-
return object.__getattribute__(self, "value").__index__()
248+
v = object.__getattribute__(self, "value")
249+
if not hasattr(v, "__index__"):
250+
raise TypeError("'%s' object cannot be interpreted as an integer" % type(v))
251+
result = v.__index__()
252+
result_type = type(result)
253+
if not isinstance(result, int):
254+
raise TypeError("__index__ returned non-int (type %s)" % result_type)
255+
return result
284256

285257

286258
ref = ReferenceType

0 commit comments

Comments
 (0)