Get weakref constructor appropriate for `obj`. `obj` may be a bound method. Bound method objects must be special-cased because they're usually garbage collected immediately, even if the instance they're bound to persists. Returns: a (weakref constructor, main object) tuple. `w
(obj)
| 30 | |
| 31 | |
| 32 | def _boundmethod_safe_weakref(obj): |
| 33 | """Get weakref constructor appropriate for `obj`. `obj` may be a bound method. |
| 34 | |
| 35 | Bound method objects must be special-cased because they're usually garbage |
| 36 | collected immediately, even if the instance they're bound to persists. |
| 37 | |
| 38 | Returns: |
| 39 | a (weakref constructor, main object) tuple. `weakref constructor` is |
| 40 | either :class:`weakref.ref` or :class:`weakref.WeakMethod`. `main |
| 41 | object` is the instance that `obj` is bound to if it is a bound method; |
| 42 | otherwise `main object` is simply `obj. |
| 43 | """ |
| 44 | try: |
| 45 | obj.__func__ |
| 46 | obj.__self__ |
| 47 | # Bound method |
| 48 | return WeakMethod, obj.__self__ |
| 49 | except AttributeError: |
| 50 | # Not a bound method |
| 51 | return weakref.ref, obj |
| 52 | |
| 53 | |
| 54 | def _make_lookup_key(receiver, sender, dispatch_uid): |