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

Python code coverage for Lib/dummy_thread.py

#countcontent
1n/a"""Drop-in replacement for the thread module.
2n/a
3n/aMeant to be used as a brain-dead substitute so that threaded code does
4n/anot need to be rewritten for when the thread module is not present.
5n/a
6n/aSuggested usage is::
7n/a
8n/a try:
9n/a import thread
10n/a except ImportError:
11n/a import dummy_thread as thread
12n/a
131"""
14n/a# Exports only things specified by thread documentation;
15n/a# skipping obsolete synonyms allocate(), start_new(), exit_thread().
161__all__ = ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock',
171 'interrupt_main', 'LockType']
18n/a
191import traceback as _traceback
20n/a
212class error(Exception):
221 """Dummy implementation of thread.error."""
23n/a
241 def __init__(self, *args):
251 self.args = args
26n/a
271def start_new_thread(function, args, kwargs={}):
28n/a """Dummy implementation of thread.start_new_thread().
29n/a
30n/a Compatibility is maintained by making sure that ``args`` is a
31n/a tuple and ``kwargs`` is a dictionary. If an exception is raised
32n/a and it is SystemExit (which can be done by thread.exit()) it is
33n/a caught and nothing is done; all other exceptions are printed out
34n/a by using traceback.print_exc().
35n/a
36n/a If the executed function calls interrupt_main the KeyboardInterrupt will be
37n/a raised when the function returns.
38n/a
39n/a """
4020 if type(args) != type(tuple()):
410 raise TypeError("2nd arg must be a tuple")
4220 if type(kwargs) != type(dict()):
430 raise TypeError("3rd arg must be a dict")
44n/a global _main
4520 _main = False
4620 try:
4720 function(*args, **kwargs)
480 except SystemExit:
490 pass
500 except:
510 _traceback.print_exc()
5220 _main = True
53n/a global _interrupt
5420 if _interrupt:
551 _interrupt = False
561 raise KeyboardInterrupt
57n/a
581def exit():
59n/a """Dummy implementation of thread.exit()."""
601 raise SystemExit
61n/a
621def get_ident():
63n/a """Dummy implementation of thread.get_ident().
64n/a
65n/a Since this module should only be used when threadmodule is not
66n/a available, it is safe to assume that the current process is the
67n/a only thread. Thus a constant can be safely returned.
68n/a """
69109 return -1
70n/a
711def allocate_lock():
72n/a """Dummy implementation of thread.allocate_lock()."""
7354 return LockType()
74n/a
751def stack_size(size=None):
76n/a """Dummy implementation of thread.stack_size()."""
770 if size is not None:
780 raise error("setting thread stack size not supported")
790 return 0
80n/a
812class LockType(object):
82n/a """Class implementing dummy implementation of thread.LockType.
83n/a
84n/a Compatibility is maintained by maintaining self.locked_status
85n/a which is a boolean that stores the state of the lock. Pickling of
86n/a the lock, though, should not be done since if the thread module is
87n/a then used with an unpickled ``lock()`` from here problems could
88n/a occur from this class not having atomic methods.
89n/a
901 """
91n/a
921 def __init__(self):
9354 self.locked_status = False
94n/a
951 def acquire(self, waitflag=None):
96n/a """Dummy implementation of acquire().
97n/a
98n/a For blocking calls, self.locked_status is automatically set to
99n/a True and returned appropriately based on value of
100n/a ``waitflag``. If it is non-blocking, then the value is
101n/a actually checked and not set if it is already acquired. This
102n/a is all done so that threading.Condition's assert statements
103n/a aren't triggered and throw a little fit.
104n/a
105n/a """
106182 if waitflag is None or waitflag:
107138 self.locked_status = True
108138 return True
109n/a else:
11044 if not self.locked_status:
1112 self.locked_status = True
1122 return True
113n/a else:
11442 return False
115n/a
1161 __enter__ = acquire
117n/a
1181 def __exit__(self, typ, val, tb):
11941 self.release()
120n/a
1211 def release(self):
122n/a """Release the dummy lock."""
123n/a # XXX Perhaps shouldn't actually bother to test? Could lead
124n/a # to problems for complex, threaded code.
125135 if not self.locked_status:
1261 raise error
127134 self.locked_status = False
128134 return True
129n/a
1301 def locked(self):
1313 return self.locked_status
132n/a
133n/a# Used to signal that interrupt_main was called in a "thread"
1341_interrupt = False
135n/a# True when not executing in a "thread"
1361_main = True
137n/a
1381def interrupt_main():
139n/a """Set _interrupt flag to True to have start_new_thread raise
140n/a KeyboardInterrupt upon exiting."""
1412 if _main:
1421 raise KeyboardInterrupt
143n/a else:
144n/a global _interrupt
1451 _interrupt = True