Home | History | Annotate | Download | only in python2.7
      1 """Faux ``threading`` version using ``dummy_thread`` instead of ``thread``.
      2 
      3 The module ``_dummy_threading`` is added to ``sys.modules`` in order
      4 to not have ``threading`` considered imported.  Had ``threading`` been
      5 directly imported it would have made all subsequent imports succeed
      6 regardless of whether ``thread`` was available which is not desired.
      7 
      8 """
      9 from sys import modules as sys_modules
     10 
     11 import dummy_thread
     12 
     13 # Declaring now so as to not have to nest ``try``s to get proper clean-up.
     14 holding_thread = False
     15 holding_threading = False
     16 holding__threading_local = False
     17 
     18 try:
     19     # Could have checked if ``thread`` was not in sys.modules and gone
     20     # a different route, but decided to mirror technique used with
     21     # ``threading`` below.
     22     if 'thread' in sys_modules:
     23         held_thread = sys_modules['thread']
     24         holding_thread = True
     25     # Must have some module named ``thread`` that implements its API
     26     # in order to initially import ``threading``.
     27     sys_modules['thread'] = sys_modules['dummy_thread']
     28 
     29     if 'threading' in sys_modules:
     30         # If ``threading`` is already imported, might as well prevent
     31         # trying to import it more than needed by saving it if it is
     32         # already imported before deleting it.
     33         held_threading = sys_modules['threading']
     34         holding_threading = True
     35         del sys_modules['threading']
     36 
     37     if '_threading_local' in sys_modules:
     38         # If ``_threading_local`` is already imported, might as well prevent
     39         # trying to import it more than needed by saving it if it is
     40         # already imported before deleting it.
     41         held__threading_local = sys_modules['_threading_local']
     42         holding__threading_local = True
     43         del sys_modules['_threading_local']
     44 
     45     import threading
     46     # Need a copy of the code kept somewhere...
     47     sys_modules['_dummy_threading'] = sys_modules['threading']
     48     del sys_modules['threading']
     49     sys_modules['_dummy__threading_local'] = sys_modules['_threading_local']
     50     del sys_modules['_threading_local']
     51     from _dummy_threading import *
     52     from _dummy_threading import __all__
     53 
     54 finally:
     55     # Put back ``threading`` if we overwrote earlier
     56 
     57     if holding_threading:
     58         sys_modules['threading'] = held_threading
     59         del held_threading
     60     del holding_threading
     61 
     62     # Put back ``_threading_local`` if we overwrote earlier
     63 
     64     if holding__threading_local:
     65         sys_modules['_threading_local'] = held__threading_local
     66         del held__threading_local
     67     del holding__threading_local
     68 
     69     # Put back ``thread`` if we overwrote, else del the entry we made
     70     if holding_thread:
     71         sys_modules['thread'] = held_thread
     72         del held_thread
     73     else:
     74         del sys_modules['thread']
     75     del holding_thread
     76 
     77     del dummy_thread
     78     del sys_modules
     79