| 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 |
|
| 14 |
holding_thread = False |
| 15 |
holding_threading = False |
| 16 |
holding__threading_local = False |
| 17 |
|
| 18 |
try: |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
if 'thread' in sys_modules: |
| 23 |
held_thread = sys_modules['thread'] |
| 24 |
holding_thread = True |
| 25 |
|
| 26 |
|
| 27 |
sys_modules['thread'] = sys_modules['dummy_thread'] |
| 28 |
|
| 29 |
if 'threading' in sys_modules: |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
held_threading = sys_modules['threading'] |
| 34 |
holding_threading = True |
| 35 |
del sys_modules['threading'] |
| 36 |
|
| 37 |
if '_threading_local' in sys_modules: |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
held__threading_local = sys_modules['_threading_local'] |
| 42 |
holding__threading_local = True |
| 43 |
del sys_modules['_threading_local'] |
| 44 |
|
| 45 |
import threading |
| 46 |
|
| 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 |
|
| 56 |
|
| 57 |
if holding_threading: |
| 58 |
sys_modules['threading'] = held_threading |
| 59 |
del held_threading |
| 60 |
del holding_threading |
| 61 |
|
| 62 |
|
| 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 |
|
| 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 |