home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / _threading_local.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  3.0 KB  |  109 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. __all__ = [
  5.     'local']
  6.  
  7. class _localbase(object):
  8.     __slots__ = ('_local__key', '_local__args', '_local__lock')
  9.     
  10.     def __new__(cls, *args, **kw):
  11.         self = object.__new__(cls)
  12.         key = ('_local__key', 'thread.local.' + str(id(self)))
  13.         object.__setattr__(self, '_local__key', key)
  14.         object.__setattr__(self, '_local__args', (args, kw))
  15.         object.__setattr__(self, '_local__lock', RLock())
  16.         if (args or kw) and cls.__init__ is object.__init__:
  17.             raise TypeError('Initialization arguments are not supported')
  18.         dict = object.__getattribute__(self, '__dict__')
  19.         current_thread().__dict__[key] = dict
  20.         return self
  21.  
  22.  
  23.  
  24. def _patch(self):
  25.     key = object.__getattribute__(self, '_local__key')
  26.     d = current_thread().__dict__.get(key)
  27.     if d is None:
  28.         d = { }
  29.         current_thread().__dict__[key] = d
  30.         object.__setattr__(self, '__dict__', d)
  31.         cls = type(self)
  32.         if cls.__init__ is not object.__init__:
  33.             (args, kw) = object.__getattribute__(self, '_local__args')
  34.             cls.__init__(self, *args, **kw)
  35.         
  36.     else:
  37.         object.__setattr__(self, '__dict__', d)
  38.  
  39.  
  40. class local(_localbase):
  41.     
  42.     def __getattribute__(self, name):
  43.         lock = object.__getattribute__(self, '_local__lock')
  44.         lock.acquire()
  45.         
  46.         try:
  47.             _patch(self)
  48.             return object.__getattribute__(self, name)
  49.         finally:
  50.             lock.release()
  51.  
  52.  
  53.     
  54.     def __setattr__(self, name, value):
  55.         if name == '__dict__':
  56.             raise AttributeError("%r object attribute '__dict__' is read-only" % self.__class__.__name__)
  57.         lock = object.__getattribute__(self, '_local__lock')
  58.         lock.acquire()
  59.         
  60.         try:
  61.             _patch(self)
  62.             return object.__setattr__(self, name, value)
  63.         finally:
  64.             lock.release()
  65.  
  66.  
  67.     
  68.     def __delattr__(self, name):
  69.         if name == '__dict__':
  70.             raise AttributeError("%r object attribute '__dict__' is read-only" % self.__class__.__name__)
  71.         lock = object.__getattribute__(self, '_local__lock')
  72.         lock.acquire()
  73.         
  74.         try:
  75.             _patch(self)
  76.             return object.__delattr__(self, name)
  77.         finally:
  78.             lock.release()
  79.  
  80.  
  81.     
  82.     def __del__(self):
  83.         import threading
  84.         key = object.__getattribute__(self, '_local__key')
  85.         
  86.         try:
  87.             threads = threading._enumerate()
  88.         except:
  89.             return None
  90.  
  91.         for thread in threads:
  92.             
  93.             try:
  94.                 __dict__ = thread.__dict__
  95.             except AttributeError:
  96.                 continue
  97.  
  98.             if key in __dict__:
  99.                 
  100.                 try:
  101.                     del __dict__[key]
  102.                 except KeyError:
  103.                     pass
  104.                 
  105.  
  106.  
  107.  
  108. from threading import current_thread, RLock
  109.