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