blob: 1bb7eee338af2458ce35a55dc946d2057f9a5d80 [file] [log] [blame]
Guido van Rossumad50ca92002-12-30 22:30:22 +00001"""Faux ``threading`` version using ``dummy_thread`` instead of ``thread``.
2
3The module ``_dummy_threading`` is added to ``sys.modules`` in order
4to not have ``threading`` considered imported. Had ``threading`` been
5directly imported it would have made all subsequent imports succeed
Georg Brandl2067bfd2008-05-25 13:05:15 +00006regardless of whether ``_thread`` was available which is not desired.
Guido van Rossumad50ca92002-12-30 22:30:22 +00007
Guido van Rossumad50ca92002-12-30 22:30:22 +00008"""
9from sys import modules as sys_modules
10
Georg Brandl2067bfd2008-05-25 13:05:15 +000011import _dummy_thread
Guido van Rossumad50ca92002-12-30 22:30:22 +000012
13# Declaring now so as to not have to nest ``try``s to get proper clean-up.
14holding_thread = False
15holding_threading = False
Jim Fultond15dc062004-07-14 19:11:50 +000016holding__threading_local = False
Guido van Rossumad50ca92002-12-30 22:30:22 +000017
18try:
Georg Brandl2067bfd2008-05-25 13:05:15 +000019 # Could have checked if ``_thread`` was not in sys.modules and gone
Guido van Rossumad50ca92002-12-30 22:30:22 +000020 # a different route, but decided to mirror technique used with
21 # ``threading`` below.
Georg Brandl2067bfd2008-05-25 13:05:15 +000022 if '_thread' in sys_modules:
23 held_thread = sys_modules['_thread']
Guido van Rossumad50ca92002-12-30 22:30:22 +000024 holding_thread = True
Georg Brandl2067bfd2008-05-25 13:05:15 +000025 # Must have some module named ``_thread`` that implements its API
Guido van Rossumad50ca92002-12-30 22:30:22 +000026 # in order to initially import ``threading``.
Georg Brandl2067bfd2008-05-25 13:05:15 +000027 sys_modules['_thread'] = sys_modules['_dummy_thread']
Guido van Rossumad50ca92002-12-30 22:30:22 +000028
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']
Jim Fultond15dc062004-07-14 19:11:50 +000036
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']
Tim Peters182b5ac2004-07-18 06:16:08 +000044
Guido van Rossumad50ca92002-12-30 22:30:22 +000045 import threading
46 # Need a copy of the code kept somewhere...
47 sys_modules['_dummy_threading'] = sys_modules['threading']
48 del sys_modules['threading']
Jim Fultond15dc062004-07-14 19:11:50 +000049 sys_modules['_dummy__threading_local'] = sys_modules['_threading_local']
50 del sys_modules['_threading_local']
Guido van Rossumad50ca92002-12-30 22:30:22 +000051 from _dummy_threading import *
52 from _dummy_threading import __all__
53
54finally:
55 # Put back ``threading`` if we overwrote earlier
Jim Fultond15dc062004-07-14 19:11:50 +000056
Guido van Rossumad50ca92002-12-30 22:30:22 +000057 if holding_threading:
58 sys_modules['threading'] = held_threading
59 del held_threading
60 del holding_threading
61
Jim Fultond15dc062004-07-14 19:11:50 +000062 # 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
Guido van Rossumad50ca92002-12-30 22:30:22 +000069 # Put back ``thread`` if we overwrote, else del the entry we made
70 if holding_thread:
Georg Brandl2067bfd2008-05-25 13:05:15 +000071 sys_modules['_thread'] = held_thread
Guido van Rossumad50ca92002-12-30 22:30:22 +000072 del held_thread
73 else:
Georg Brandl2067bfd2008-05-25 13:05:15 +000074 del sys_modules['_thread']
Guido van Rossumad50ca92002-12-30 22:30:22 +000075 del holding_thread
76
Georg Brandl2067bfd2008-05-25 13:05:15 +000077 del _dummy_thread
Guido van Rossumad50ca92002-12-30 22:30:22 +000078 del sys_modules