this repo has no description
1#!/usr/bin/env python3
2# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
3"""Weak-reference support module."""
4
5from builtins import _index
6
7from _builtins import (
8 _builtin,
9 _property,
10 _type,
11 _unimplemented,
12 _weakref_callback,
13 _weakref_check,
14 _weakref_guard,
15 _weakref_referent,
16)
17
18
19__all__ = ["CallableProxyType", "ProxyType", "ReferenceType", "ref", "proxy"]
20
21
22def _proxy_check(object):
23 _builtin()
24
25
26def _proxy_referent(proxy_obj):
27 referent = object.__getattribute__(proxy_obj, "ref_obj")()
28 if referent is None:
29 raise ReferenceError("weakly-referenced object no longer exists")
30 return referent
31
32
33def _proxy_unwrap(object):
34 return _proxy_referent(object) if _proxy_check(object) else object
35
36
37def _remove_dead_weakref(data, key):
38 data.pop(key, None)
39
40
41def _weakref_hash(self):
42 _builtin()
43
44
45def _weakref_set_hash(self, hash):
46 _builtin()
47
48
49def getweakrefs(object):
50 _unimplemented()
51
52
53def getweakrefcount(object):
54 _unimplemented()
55
56
57def proxy(object, callback=None):
58 reference = ref(object, callback)
59 if callable(object):
60 return weakcallableproxy(reference)
61 return weakproxy(reference)
62
63
64class weakref(bootstrap=True):
65 def __call__(self):
66 _builtin()
67
68 __callback__ = _property(_weakref_callback)
69
70 def __eq__(self, other):
71 _weakref_guard(self)
72 if not _weakref_check(other):
73 return NotImplemented
74 self_referent = _weakref_referent(self)
75 other_referent = _weakref_referent(other)
76 if self_referent is None or other_referent is None:
77 return self is other
78 return self_referent == other_referent
79
80 def __ge__(self, other):
81 _weakref_guard(self)
82 return NotImplemented
83
84 def __gt__(self, other):
85 _weakref_guard(self)
86 return NotImplemented
87
88 def __le__(self, other):
89 _weakref_guard(self)
90 return NotImplemented
91
92 def __lt__(self, other):
93 _weakref_guard(self)
94 return NotImplemented
95
96 def __ne__(self, other):
97 _weakref_guard(self)
98 if not _weakref_check(other):
99 return NotImplemented
100 self_referent = _weakref_referent(self)
101 other_referent = _weakref_referent(other)
102 if self_referent is None or other_referent is None:
103 return self is not other
104 return self_referent != other_referent
105
106 @staticmethod
107 def __new__(cls, referent, callback=None):
108 _builtin()
109
110 def __init__(self, referent, callback=None):
111 return None
112
113 def __hash__(self):
114 result = _weakref_hash(self)
115 if result is not None:
116 return result
117 obj = _weakref_referent(self)
118 if obj is None:
119 raise TypeError("weak object has gone away")
120 result = hash(obj)
121 _weakref_set_hash(self, result)
122 return result
123
124
125class weakcallableproxy(bootstrap=True):
126 __hash__ = None
127
128 def __init__(self, ref_obj):
129 object.__setattr__(self, "ref_obj", ref_obj)
130
131 def __abs__(self):
132 return abs(_proxy_unwrap(self))
133
134 def __add__(self, other):
135 return _proxy_unwrap(self) + _proxy_unwrap(other)
136
137 def __and__(self, other):
138 return _proxy_unwrap(self) & _proxy_unwrap(other)
139
140 def __bytes__(self):
141 return bytes(_proxy_unwrap(self))
142
143 @property
144 def __call__(self):
145 return _proxy_unwrap(self)
146
147 def __divmod__(self, other):
148 return divmod(_proxy_unwrap(self), _proxy_unwrap(other))
149
150 def __float__(self):
151 return float(_proxy_unwrap(self))
152
153 def __getattr__(self, other):
154 return getattr(_proxy_unwrap(self), _proxy_unwrap(other))
155
156 def __getitem__(self, other):
157 return _proxy_unwrap(self)[_proxy_unwrap(other)]
158
159 def __iadd__(self, other):
160 referent = _proxy_unwrap(self)
161 referent += _proxy_unwrap(other)
162
163 def __iand__(self, other):
164 referent = _proxy_unwrap(self)
165 referent &= _proxy_unwrap(other)
166
167 def __ifloor_div__(self, other):
168 referent = _proxy_unwrap(self)
169 referent /= _proxy_unwrap(other)
170
171 def __ilshift__(self, other):
172 referent = _proxy_unwrap(self)
173 referent <<= _proxy_unwrap(other)
174
175 def __imod__(self, other):
176 referent = _proxy_unwrap(self)
177 referent %= _proxy_unwrap(other)
178
179 def __imul__(self, other):
180 referent = _proxy_unwrap(self)
181 referent *= _proxy_unwrap(other)
182
183 def __index__(self):
184 return _index(_proxy_unwrap(self))
185
186 def __int__(self):
187 return int(_proxy_unwrap(self))
188
189 def __invert__(self):
190 return ~_proxy_unwrap(self)
191
192 def __ior__(self, other):
193 referent = _proxy_unwrap(self)
194 referent |= _proxy_unwrap(other)
195
196 def __ipow__(self, other):
197 referent = _proxy_unwrap(self)
198 referent **= _proxy_unwrap(other)
199
200 def __irshift__(self, other):
201 referent = _proxy_unwrap(self)
202 referent >>= _proxy_unwrap(other)
203
204 def __isub__(self, other):
205 referent = _proxy_unwrap(self)
206 referent -= _proxy_unwrap(other)
207
208 def __itrue_div__(self, other):
209 referent = _proxy_unwrap(self)
210 referent /= _proxy_unwrap(other)
211
212 def __ixor__(self, other):
213 referent = _proxy_unwrap(self)
214 referent ^= _proxy_unwrap(other)
215
216 def __lshift__(self, other):
217 return _proxy_unwrap(self) << _proxy_unwrap(other)
218
219 def __mod__(self, other):
220 return _proxy_unwrap(self) % _proxy_unwrap(other)
221
222 def __mul__(self, other):
223 return _proxy_unwrap(self) * _proxy_unwrap(other)
224
225 def __neg__(self):
226 return -_proxy_unwrap(self)
227
228 def __or__(self, other):
229 return _proxy_unwrap(self) | _proxy_unwrap(other)
230
231 def __pos__(self):
232 return +_proxy_unwrap(self)
233
234 def __pow__(self, other, modulo=None):
235 return pow(
236 _proxy_unwrap(self),
237 _proxy_unwrap(other),
238 None if modulo is None else _proxy_unwrap(modulo),
239 )
240
241 def __rshift__(self, other):
242 return _proxy_unwrap(self) >> _proxy_unwrap(other)
243
244 def __setattr__(self, name, value):
245 if not _proxy_check(self):
246 raise TypeError(
247 "'__setattr__' for 'weakproxy' objects doesn't apply to a "
248 f"'{_type(self).__name__}' object"
249 )
250 return setattr(_proxy_unwrap(self), name, value)
251
252 def __str__(self):
253 return str(_proxy_unwrap(self))
254
255 def __sub__(self, other):
256 return _proxy_unwrap(self) - _proxy_unwrap(other)
257
258 def __truediv__(self, other):
259 return _proxy_unwrap(self) / _proxy_unwrap(other)
260
261 def __xor__(self, other):
262 return _proxy_unwrap(self) ^ _proxy_unwrap(other)
263
264
265class weakproxy(bootstrap=True):
266 __hash__ = None
267
268 def __init__(self, ref_obj):
269 object.__setattr__(self, "ref_obj", ref_obj)
270
271 def __abs__(self):
272 return abs(_proxy_unwrap(self))
273
274 def __add__(self, other):
275 return _proxy_unwrap(self) + _proxy_unwrap(other)
276
277 def __and__(self, other):
278 return _proxy_unwrap(self) & _proxy_unwrap(other)
279
280 def __bytes__(self):
281 return bytes(_proxy_unwrap(self))
282
283 def __divmod__(self, other):
284 return divmod(_proxy_unwrap(self), _proxy_unwrap(other))
285
286 def __float__(self):
287 return float(_proxy_unwrap(self))
288
289 def __getattr__(self, other):
290 return getattr(_proxy_unwrap(self), _proxy_unwrap(other))
291
292 def __getitem__(self, other):
293 return _proxy_unwrap(self)[_proxy_unwrap(other)]
294
295 def __iadd__(self, other):
296 referent = _proxy_unwrap(self)
297 referent += _proxy_unwrap(other)
298
299 def __iand__(self, other):
300 referent = _proxy_unwrap(self)
301 referent &= _proxy_unwrap(other)
302
303 def __ifloor_div__(self, other):
304 referent = _proxy_unwrap(self)
305 referent /= _proxy_unwrap(other)
306
307 def __ilshift__(self, other):
308 referent = _proxy_unwrap(self)
309 referent <<= _proxy_unwrap(other)
310
311 def __imod__(self, other):
312 referent = _proxy_unwrap(self)
313 referent %= _proxy_unwrap(other)
314
315 def __imul__(self, other):
316 referent = _proxy_unwrap(self)
317 referent *= _proxy_unwrap(other)
318
319 def __index__(self):
320 return _index(_proxy_unwrap(self))
321
322 def __int__(self):
323 return int(_proxy_unwrap(self))
324
325 def __invert__(self):
326 return ~_proxy_unwrap(self)
327
328 def __ior__(self, other):
329 referent = _proxy_unwrap(self)
330 referent |= _proxy_unwrap(other)
331
332 def __ipow__(self, other):
333 referent = _proxy_unwrap(self)
334 referent **= _proxy_unwrap(other)
335
336 def __irshift__(self, other):
337 referent = _proxy_unwrap(self)
338 referent >>= _proxy_unwrap(other)
339
340 def __isub__(self, other):
341 referent = _proxy_unwrap(self)
342 referent -= _proxy_unwrap(other)
343
344 def __itrue_div__(self, other):
345 referent = _proxy_unwrap(self)
346 referent /= _proxy_unwrap(other)
347
348 def __ixor__(self, other):
349 referent = _proxy_unwrap(self)
350 referent ^= _proxy_unwrap(other)
351
352 def __lshift__(self, other):
353 return _proxy_unwrap(self) << _proxy_unwrap(other)
354
355 def __mod__(self, other):
356 return _proxy_unwrap(self) % _proxy_unwrap(other)
357
358 def __mul__(self, other):
359 return _proxy_unwrap(self) * _proxy_unwrap(other)
360
361 def __neg__(self):
362 return -_proxy_unwrap(self)
363
364 def __or__(self, other):
365 return _proxy_unwrap(self) | _proxy_unwrap(other)
366
367 def __pos__(self):
368 return +_proxy_unwrap(self)
369
370 def __pow__(self, other, modulo=None):
371 return pow(
372 _proxy_unwrap(self),
373 _proxy_unwrap(other),
374 None if modulo is None else _proxy_unwrap(modulo),
375 )
376
377 def __rshift__(self, other):
378 return _proxy_unwrap(self) >> _proxy_unwrap(other)
379
380 def __setattr__(self, name, value):
381 if not _proxy_check(self):
382 raise TypeError(
383 "'__setattr__' for 'weakproxy' objects doesn't apply to a "
384 f"'{_type(self).__name__}' object"
385 )
386 return setattr(_proxy_unwrap(self), name, value)
387
388 def __str__(self):
389 return str(_proxy_unwrap(self))
390
391 def __sub__(self, other):
392 return _proxy_unwrap(self) - _proxy_unwrap(other)
393
394 def __truediv__(self, other):
395 return _proxy_unwrap(self) / _proxy_unwrap(other)
396
397 def __xor__(self, other):
398 return _proxy_unwrap(self) ^ _proxy_unwrap(other)
399
400
401CallableProxyType = weakcallableproxy
402ProxyType = weakproxy
403ReferenceType = weakref
404ref = weakref