11from weakref import ref
22
3- class _weak_callable :
43
5- def __init__ (self ,obj ,func ):
4+ class _WeakCallable (object ):
5+
6+ def __init__ (self , obj , func ):
67 self ._obj = obj
78 self ._meth = func
89
9- def __call__ (self ,* args ,** kws ):
10+ def __call__ (self , * args , ** kws ):
1011 if self ._obj is not None :
11- return self ._meth (self ._obj ,* args ,** kws )
12+ return self ._meth (self ._obj , * args , ** kws )
1213 else :
13- return self ._meth (* args ,** kws )
14+ return self ._meth (* args , ** kws )
1415
15- def __getattr__ (self ,attr ):
16+ def __getattr__ (self , attr ):
1617 if attr == 'im_self' :
1718 return self ._obj
1819 if attr == 'im_func' :
1920 return self ._meth
2021 raise AttributeError (attr )
2122
22- class WeakMethod :
23+
24+ class WeakMethod (object ):
2325 """ Wraps a function or, more importantly, a bound method, in
2426 a way that allows a bound method's object to be GC'd, while
2527 providing the same interface as a normal weak reference. """
2628
27- def __init__ (self ,fn ):
29+ def __init__ (self , fn ):
2830 try :
2931 self ._obj = ref (fn .im_self )
3032 self ._meth = fn .im_func
@@ -34,8 +36,9 @@ def __init__(self,fn):
3436 self ._meth = fn
3537
3638 def __call__ (self ):
37- if self ._dead (): return None
38- return _weak_callable (self ._obj (),self ._meth )
39+ if self ._dead ():
40+ return None
41+ return _WeakCallable (self ._obj (), self ._meth )
3942
4043 def _dead (self ):
4144 return self ._obj is not None and self ._obj () is None
0 commit comments