/usr/lib/python3/dist-packages/kivy/weakmethod.py is in python3-kivy 1.9.0-3build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | '''
Weak Method
===========
The :class:`WeakMethod` is used by the :class:`~kivy.clock.Clock` class to
allow references to a bound method that permits the associated object to
be garbage collected. Please refer to
`examples/core/clock_method.py` for more information.
This WeakMethod class is taken from the recipe
http://code.activestate.com/recipes/81253/, based on the nicodemus version.
Many thanks nicodemus!
'''
import weakref
import sys
if sys.version > '3':
class WeakMethod:
'''Implementation of a
`weakref <http://en.wikipedia.org/wiki/Weak_reference>`_
for functions and bound methods.
'''
def __init__(self, method):
self.method = None
self.method_name = None
try:
if method.__self__ is not None:
self.method_name = method.__func__.__name__
self.proxy = weakref.proxy(method.__self__)
else:
self.method = method
self.proxy = None
except AttributeError:
self.method = method
self.proxy = None
def __call__(self):
'''Return a new bound-method like the original, or the
original function if it was just a function or unbound
method.
Returns None if the original object doesn't exist.
'''
try:
if self.proxy:
return getattr(self.proxy, self.method_name)
except ReferenceError:
pass
return self.method
def is_dead(self):
'''Returns True if the referenced callable was a bound method and
the instance no longer exists. Otherwise, return False.
'''
return self.proxy is not None and not bool(dir(self.proxy))
def __repr__(self):
return '<WeakMethod proxy={} method={} method_name={}>'.format(
self.proxy, self.method, self.method_name)
else:
import new
class WeakMethod(object):
'''Implementation of a
`weakref <http://en.wikipedia.org/wiki/Weak_reference>`_
for functions and bound methods.
'''
def __init__(self, method):
try:
if method.__self__ is not None:
# bound method
self._obj = weakref.ref(method.im_self)
else:
# unbound method
self._obj = None
self._func = method.im_func
self._class = method.im_class
except AttributeError:
# not a method
self._obj = None
self._func = method
self._class = None
def __call__(self):
'''Return a new bound-method like the original, or the
original function if it was just a function or unbound
method.
Returns None if the original object doesn't exist.
'''
if self.is_dead():
return None
if self._obj is not None:
return new.instancemethod(self._func, self._obj(), self._class)
else:
# we don't have an instance: return just the function
return self._func
def is_dead(self):
'''Returns True if the referenced callable was a bound method and
the instance no longer exists. Otherwise, return False.
'''
return self._obj is not None and self._obj() is None
def __eq__(self, other):
try:
return type(self) is type(other) and self() == other()
except:
return False
def __ne__(self, other):
return not self == other
|