Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / lib / python2.4 / dummy_threading.py
CommitLineData
920dae64
AT
1"""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
6regardless of whether ``thread`` was available which is not desired.
7
8:Author: Brett Cannon
9:Contact: brett@python.org
10
11XXX: Try to get rid of ``_dummy_threading``.
12
13"""
14from sys import modules as sys_modules
15
16import dummy_thread
17
18# Declaring now so as to not have to nest ``try``s to get proper clean-up.
19holding_thread = False
20holding_threading = False
21holding__threading_local = False
22
23try:
24 # Could have checked if ``thread`` was not in sys.modules and gone
25 # a different route, but decided to mirror technique used with
26 # ``threading`` below.
27 if 'thread' in sys_modules:
28 held_thread = sys_modules['thread']
29 holding_thread = True
30 # Must have some module named ``thread`` that implements its API
31 # in order to initially import ``threading``.
32 sys_modules['thread'] = sys_modules['dummy_thread']
33
34 if 'threading' in sys_modules:
35 # If ``threading`` is already imported, might as well prevent
36 # trying to import it more than needed by saving it if it is
37 # already imported before deleting it.
38 held_threading = sys_modules['threading']
39 holding_threading = True
40 del sys_modules['threading']
41
42 if '_threading_local' in sys_modules:
43 # If ``_threading_local`` is already imported, might as well prevent
44 # trying to import it more than needed by saving it if it is
45 # already imported before deleting it.
46 held__threading_local = sys_modules['_threading_local']
47 holding__threading_local = True
48 del sys_modules['_threading_local']
49
50 import threading
51 # Need a copy of the code kept somewhere...
52 sys_modules['_dummy_threading'] = sys_modules['threading']
53 del sys_modules['threading']
54 sys_modules['_dummy__threading_local'] = sys_modules['_threading_local']
55 del sys_modules['_threading_local']
56 from _dummy_threading import *
57 from _dummy_threading import __all__
58
59finally:
60 # Put back ``threading`` if we overwrote earlier
61
62 if holding_threading:
63 sys_modules['threading'] = held_threading
64 del held_threading
65 del holding_threading
66
67 # Put back ``_threading_local`` if we overwrote earlier
68
69 if holding__threading_local:
70 sys_modules['_threading_local'] = held__threading_local
71 del held__threading_local
72 del holding__threading_local
73
74 # Put back ``thread`` if we overwrote, else del the entry we made
75 if holding_thread:
76 sys_modules['thread'] = held_thread
77 del held_thread
78 else:
79 del sys_modules['thread']
80 del holding_thread
81
82 del dummy_thread
83 del sys_modules