1
"""Drop-in replacement for the thread module.
3
Meant to be used as a brain-dead substitute so that threaded code does
4
not need to be rewritten for when the thread module is not present.
11
import _dummy_thread as _thread
14
# Exports only things specified by thread documentation;
15
# skipping obsolete synonyms allocate(), start_new(), exit_thread().
16
__all__ = ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock',
17
'interrupt_main', 'LockType']
22
# NOTE: this module can be imported early in the extension building process,
23
# and so top level imports of other modules should be avoided. Instead, all
24
# imports are done when needed on a function-by-function basis. Since threads
25
# are disabled, the import lock should not be an issue anyway (??).
29
def start_new_thread(function, args, kwargs={}):
30
"""Dummy implementation of _thread.start_new_thread().
32
Compatibility is maintained by making sure that ``args`` is a
33
tuple and ``kwargs`` is a dictionary. If an exception is raised
34
and it is SystemExit (which can be done by _thread.exit()) it is
35
caught and nothing is done; all other exceptions are printed out
36
by using traceback.print_exc().
38
If the executed function calls interrupt_main the KeyboardInterrupt will be
39
raised when the function returns.
42
if type(args) != type(tuple()):
43
raise TypeError("2nd arg must be a tuple")
44
if type(kwargs) != type(dict()):
45
raise TypeError("3rd arg must be a dict")
49
function(*args, **kwargs)
59
raise KeyboardInterrupt
62
"""Dummy implementation of _thread.exit()."""
66
"""Dummy implementation of _thread.get_ident().
68
Since this module should only be used when _threadmodule is not
69
available, it is safe to assume that the current process is the
70
only thread. Thus a constant can be safely returned.
75
"""Dummy implementation of _thread.allocate_lock()."""
78
def stack_size(size=None):
79
"""Dummy implementation of _thread.stack_size()."""
81
raise error("setting thread stack size not supported")
85
"""Dummy implementation of _thread._set_sentinel()."""
88
class LockType(object):
89
"""Class implementing dummy implementation of _thread.LockType.
91
Compatibility is maintained by maintaining self.locked_status
92
which is a boolean that stores the state of the lock. Pickling of
93
the lock, though, should not be done since if the _thread module is
94
then used with an unpickled ``lock()`` from here problems could
95
occur from this class not having atomic methods.
100
self.locked_status = False
102
def acquire(self, waitflag=None, timeout=-1):
103
"""Dummy implementation of acquire().
105
For blocking calls, self.locked_status is automatically set to
106
True and returned appropriately based on value of
107
``waitflag``. If it is non-blocking, then the value is
108
actually checked and not set if it is already acquired. This
109
is all done so that threading.Condition's assert statements
110
aren't triggered and throw a little fit.
113
if waitflag is None or waitflag:
114
self.locked_status = True
117
if not self.locked_status:
118
self.locked_status = True
128
def __exit__(self, typ, val, tb):
132
"""Release the dummy lock."""
133
# XXX Perhaps shouldn't actually bother to test? Could lead
134
# to problems for complex, threaded code.
135
if not self.locked_status:
137
self.locked_status = False
141
return self.locked_status
143
# Used to signal that interrupt_main was called in a "thread"
145
# True when not executing in a "thread"
148
def interrupt_main():
149
"""Set _interrupt flag to True to have start_new_thread raise
150
KeyboardInterrupt upon exiting."""
152
raise KeyboardInterrupt