ยปCore Development>Code coverage>Lib/dummy_threading.py

Python code coverage for Lib/dummy_threading.py

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