PyXR

c:\python24\lib \ dummy_threading.py



0001 """Faux ``threading`` version using ``dummy_thread`` instead of ``thread``.
0002 
0003 The module ``_dummy_threading`` is added to ``sys.modules`` in order
0004 to not have ``threading`` considered imported.  Had ``threading`` been
0005 directly imported it would have made all subsequent imports succeed
0006 regardless of whether ``thread`` was available which is not desired.
0007 
0008 :Author: Brett Cannon
0009 :Contact: brett@python.org
0010 
0011 XXX: Try to get rid of ``_dummy_threading``.
0012 
0013 """
0014 from sys import modules as sys_modules
0015 
0016 import dummy_thread
0017 
0018 # Declaring now so as to not have to nest ``try``s to get proper clean-up.
0019 holding_thread = False
0020 holding_threading = False
0021 holding__threading_local = False
0022 
0023 try:
0024     # Could have checked if ``thread`` was not in sys.modules and gone
0025     # a different route, but decided to mirror technique used with
0026     # ``threading`` below.
0027     if 'thread' in sys_modules:
0028         held_thread = sys_modules['thread']
0029         holding_thread = True
0030     # Must have some module named ``thread`` that implements its API
0031     # in order to initially import ``threading``.
0032     sys_modules['thread'] = sys_modules['dummy_thread']
0033 
0034     if 'threading' in sys_modules:
0035         # If ``threading`` is already imported, might as well prevent
0036         # trying to import it more than needed by saving it if it is
0037         # already imported before deleting it.
0038         held_threading = sys_modules['threading']
0039         holding_threading = True
0040         del sys_modules['threading']
0041 
0042     if '_threading_local' in sys_modules:
0043         # If ``_threading_local`` is already imported, might as well prevent
0044         # trying to import it more than needed by saving it if it is
0045         # already imported before deleting it.
0046         held__threading_local = sys_modules['_threading_local']
0047         holding__threading_local = True
0048         del sys_modules['_threading_local']
0049 
0050     import threading
0051     # Need a copy of the code kept somewhere...
0052     sys_modules['_dummy_threading'] = sys_modules['threading']
0053     del sys_modules['threading']
0054     sys_modules['_dummy__threading_local'] = sys_modules['_threading_local']
0055     del sys_modules['_threading_local']
0056     from _dummy_threading import *
0057     from _dummy_threading import __all__
0058 
0059 finally:
0060     # Put back ``threading`` if we overwrote earlier
0061 
0062     if holding_threading:
0063         sys_modules['threading'] = held_threading
0064         del held_threading
0065     del holding_threading
0066 
0067     # Put back ``_threading_local`` if we overwrote earlier
0068 
0069     if holding__threading_local:
0070         sys_modules['_threading_local'] = held__threading_local
0071         del held__threading_local
0072     del holding__threading_local
0073 
0074     # Put back ``thread`` if we overwrote, else del the entry we made
0075     if holding_thread:
0076         sys_modules['thread'] = held_thread
0077         del held_thread
0078     else:
0079         del sys_modules['thread']
0080     del holding_thread
0081 
0082     del dummy_thread
0083     del sys_modules
0084 

Generated by PyXR 0.9.4
SourceForge.net Logo