home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / util / primitives / refs.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  3.5 KB  |  127 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from weakref import ref, ref as weakref_ref
  5. import new
  6. import traceback
  7.  
  8. def better_ref(method, cb = None, obj = None):
  9.     if obj is None:
  10.         return bound_ref(method, cb)
  11.     return unbound_ref(obj, method, cb)
  12.  
  13.  
  14. class ref_base(object):
  15.     
  16.     def maybe_call(self, *a, **k):
  17.         
  18.         try:
  19.             o = self()
  20.             if o is not None:
  21.                 return o(*a, **k)
  22.         except Exception:
  23.             traceback.print_exc()
  24.  
  25.  
  26.  
  27.  
  28. class bound_ref(ref_base):
  29.     
  30.     def __init__(self, method, cb = None):
  31.         funcinfo = funcinfo
  32.         import util.introspect
  33.         self.object = weakref_ref(method.im_self, cb)
  34.         self.func = weakref_ref(method.im_func)
  35.         self.cls = weakref_ref(method.im_class)
  36.  
  37.     
  38.     def __call__(self):
  39.         obj = self.object()
  40.         if obj is None:
  41.             return None
  42.         func = self.func()
  43.         if func is None:
  44.             return None
  45.         cls = self.cls()
  46.         if cls is None:
  47.             return None
  48.         return new.instancemethod(func, obj, cls)
  49.  
  50.  
  51.  
  52. class unbound_ref(ref_base):
  53.     
  54.     def __init__(self, obj, method, cb = None):
  55.         self.object = weakref_ref(obj, cb)
  56.         self.func = weakref_ref(method)
  57.         
  58.         try:
  59.             unbound_cbs = obj._unbound_cbs
  60.         except AttributeError:
  61.             obj._unbound_cbs = unbound_cbs = { }
  62.  
  63.         unbound_cbs[self] = method
  64.  
  65.     
  66.     def __call__(self):
  67.         
  68.         try:
  69.             obj = self.object()
  70.             func = self.func()
  71.         except AttributeError:
  72.             pass
  73.  
  74.         if obj is not None and func is not None:
  75.             return func
  76.  
  77.     
  78.     def destroy(self):
  79.         obj = self.object()
  80.         if obj is not None:
  81.             
  82.             try:
  83.                 obj._unbound_cbs.pop(self, None)
  84.             except AttributeError:
  85.                 pass
  86.             except:
  87.                 None<EXCEPTION MATCH>AttributeError
  88.             
  89.  
  90.         None<EXCEPTION MATCH>AttributeError
  91.         del self.object
  92.         del self.func
  93.  
  94.     
  95.     def __repr__(self):
  96.         
  97.         try:
  98.             funcinfo = funcinfo
  99.             import util
  100.             f_info = funcinfo(self.func())
  101.         except Exception:
  102.             e = None
  103.             f_info = '(unknown)'
  104.  
  105.         
  106.         try:
  107.             o_info = self.object()
  108.         except Exception:
  109.             e = None
  110.             o_info = '(unknown)'
  111.  
  112.         return '<unbound_ref to %s, obj is %r>' % (f_info, o_info)
  113.  
  114.  
  115. better_ref_types = (bound_ref, unbound_ref)
  116.  
  117. class stupidref(ref):
  118.     
  119.     def __getattr__(self, attr):
  120.         return getattr(self(), attr)
  121.  
  122.  
  123. if __name__ == '__main__':
  124.     import doctest
  125.     doctest.testmod(verbose = True)
  126.  
  127.