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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. import itertools
  6. import functools
  7. import logging
  8. import os
  9. import sys
  10. import traceback
  11. import threading
  12. import time
  13. log = logging.getLogger('util.primitives.synch')
  14.  
  15. def lock(f):
  16.     
  17.     def wrapper1(instance, *args, **kw):
  18.         if not hasattr(instance, '_lock'):
  19.             
  20.             try:
  21.                 instance._lock = threading.RLock()
  22.             except AttributeError:
  23.                 raise NotImplementedError, '%s needs a _lock slot' % instance.__class__.__name__
  24.             except:
  25.                 None<EXCEPTION MATCH>AttributeError
  26.             
  27.  
  28.         None<EXCEPTION MATCH>AttributeError
  29.         instance._lock.__enter__()
  30.         
  31.         try:
  32.             val = f(instance, *args, **kw)
  33.         finally:
  34.             pass
  35.  
  36.         return val
  37.  
  38.     wrapper1 = (functools.wraps(f),)(wrapper1)
  39.     return wrapper1
  40.  
  41.  
  42. class RepeatCheck(object):
  43.     
  44.     def __init__(self, idfunc = None):
  45.         self.ids = sentinel
  46.         if idfunc is None:
  47.             idfunc = id
  48.         
  49.         self.id = idfunc
  50.  
  51.     
  52.     def __call__(self, *x):
  53.         if x == tuple():
  54.             self.ids = sentinel
  55.             return None
  56.         if len(x) != 1:
  57.             raise TypeError('takes one argument')
  58.         len(x) != 1
  59.         
  60.         try:
  61.             newids = [ self.id(a) for a in x ]
  62.         except TypeError:
  63.             x == tuple()
  64.             x == tuple()
  65.             newids = [
  66.                 self.id(a)]
  67.         except:
  68.             x == tuple()
  69.  
  70.         changed = newids != self.ids
  71.         self.ids = newids
  72.         return changed
  73.  
  74.  
  75.  
  76. def repeat_guard(func):
  77.     guard = RepeatCheck()
  78.     
  79.     def wrapper(src, attr, old, new):
  80.         if guard(src):
  81.             return None
  82.         return func(src, attr, old, new)
  83.  
  84.     return wrapper
  85.  
  86.  
  87. class HangingThreadDaemon(threading.Thread):
  88.     ids = itertools.count()
  89.     
  90.     def __init__(self, wait = 3, sysexit = False):
  91.         threading.Thread.__init__(self, name = 'HangingThreadDaemon %d' % self.ids.next())
  92.         self.wait = wait
  93.         self.sysexit = sysexit
  94.         self.setDaemon(True)
  95.  
  96.     
  97.     def run(self):
  98.         time.sleep(self.wait)
  99.         threads = list(threading.enumerate())
  100.         if threads:
  101.             print 'Remaining non-daemon threads:'
  102.             for thread in threads:
  103.                 if not thread.isDaemon():
  104.                     print ' ', thread
  105.                     continue
  106.             
  107.         
  108.         collect_garbage_and_report()
  109.         if self.sysexit:
  110.             
  111.             try:
  112.                 import common.commandline as cc
  113.                 cc.where()
  114.             except Exception:
  115.                 traceback.print_exc()
  116.  
  117.             print >>sys.stderr, 'forcing shutdown...'
  118.             os._exit(1)
  119.         
  120.  
  121.  
  122.  
  123. def collect_garbage_and_report():
  124.     import gc
  125.     garbage_count = gc.collect()
  126.     if garbage_count > 0:
  127.         log.info('Garbage collected. ' + str(garbage_count) + ' unreachable objects')
  128.         if garbage_count:
  129.             log.info('Garbage left (only first 20 listed): %r', gc.garbage[:20])
  130.         
  131.     
  132.  
  133. if __name__ == '__main__':
  134.     import doctest
  135.     doctest.testmod(verbose = True)
  136.  
  137.