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