home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / util / primitives / synchronization.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  4.1 KB  |  134 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  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.         elif len(x) != 1:
  57.             raise TypeError('takes one argument')
  58.         
  59.         
  60.         try:
  61.             newids = [ self.id(a) for a in x ]
  62.         except TypeError:
  63.             newids = [
  64.                 self.id(a)]
  65.  
  66.         changed = newids != self.ids
  67.         self.ids = newids
  68.         return changed
  69.  
  70.  
  71.  
  72. def repeat_guard(func):
  73.     guard = RepeatCheck()
  74.     
  75.     def wrapper(src, attr, old, new):
  76.         if guard(src):
  77.             return None
  78.         
  79.         return func(src, attr, old, new)
  80.  
  81.     return wrapper
  82.  
  83.  
  84. class HangingThreadDaemon(threading.Thread):
  85.     ids = itertools.count()
  86.     
  87.     def __init__(self, wait = 3, sysexit = False):
  88.         threading.Thread.__init__(self, name = 'HangingThreadDaemon %d' % self.ids.next())
  89.         self.wait = wait
  90.         self.sysexit = sysexit
  91.         self.setDaemon(True)
  92.  
  93.     
  94.     def run(self):
  95.         time.sleep(self.wait)
  96.         threads = list(threading.enumerate())
  97.         if threads:
  98.             print 'Remaining non-daemon threads:'
  99.             for thread in threads:
  100.                 if not thread.isDaemon():
  101.                     print ' ', thread
  102.                     continue
  103.             
  104.         
  105.         collect_garbage_and_report()
  106.         if self.sysexit:
  107.             
  108.             try:
  109.                 import common.commandline as cc
  110.                 cc.where()
  111.             except Exception:
  112.                 traceback.print_exc()
  113.  
  114.             print >>sys.stderr, 'forcing shutdown...'
  115.             os._exit(1)
  116.         
  117.  
  118.  
  119.  
  120. def collect_garbage_and_report():
  121.     import gc
  122.     garbage_count = gc.collect()
  123.     if garbage_count > 0:
  124.         log.info('Garbage collected. ' + str(garbage_count) + ' unreachable objects')
  125.         if garbage_count:
  126.             log.info('Garbage left (only first 20 listed): %r', gc.garbage[:20])
  127.         
  128.     
  129.  
  130. if __name__ == '__main__':
  131.     import doctest
  132.     doctest.testmod(verbose = True)
  133.  
  134.