blob: e03905c203764afad3e80c62be596fbcc6347ade [file] [log] [blame]
Guido van Rossumad50ca92002-12-30 22:30:22 +00001"""Drop-in replacement for the thread module.
2
3Meant to be used as a brain-dead substitute so that threaded code does
4not need to be rewritten for when the thread module is not present.
5
6Suggested usage is::
Tim Peters2c60f7a2003-01-29 03:49:43 +00007
Guido van Rossumad50ca92002-12-30 22:30:22 +00008 try:
Georg Brandl2067bfd2008-05-25 13:05:15 +00009 import _thread
Guido van Rossumad50ca92002-12-30 22:30:22 +000010 except ImportError:
Georg Brandl2067bfd2008-05-25 13:05:15 +000011 import _dummy_thread as _thread
Guido van Rossumad50ca92002-12-30 22:30:22 +000012
13"""
Thomas Wouters9fe394c2007-02-05 01:24:16 +000014# Exports only things specified by thread documentation;
15# skipping obsolete synonyms allocate(), start_new(), exit_thread().
Guido van Rossumad50ca92002-12-30 22:30:22 +000016__all__ = ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock',
Brett Cannon4e64d782003-06-13 23:44:35 +000017 'interrupt_main', 'LockType']
Guido van Rossumad50ca92002-12-30 22:30:22 +000018
19import traceback as _traceback
Thomas Wouters0e3f5912006-08-11 14:57:12 +000020import warnings
Guido van Rossumad50ca92002-12-30 22:30:22 +000021
22class error(Exception):
Georg Brandl2067bfd2008-05-25 13:05:15 +000023 """Dummy implementation of _thread.error."""
Guido van Rossumad50ca92002-12-30 22:30:22 +000024
25 def __init__(self, *args):
26 self.args = args
27
28def start_new_thread(function, args, kwargs={}):
Georg Brandl2067bfd2008-05-25 13:05:15 +000029 """Dummy implementation of _thread.start_new_thread().
Guido van Rossumad50ca92002-12-30 22:30:22 +000030
31 Compatibility is maintained by making sure that ``args`` is a
32 tuple and ``kwargs`` is a dictionary. If an exception is raised
Georg Brandl2067bfd2008-05-25 13:05:15 +000033 and it is SystemExit (which can be done by _thread.exit()) it is
Guido van Rossumad50ca92002-12-30 22:30:22 +000034 caught and nothing is done; all other exceptions are printed out
35 by using traceback.print_exc().
36
Brett Cannon4e64d782003-06-13 23:44:35 +000037 If the executed function calls interrupt_main the KeyboardInterrupt will be
38 raised when the function returns.
39
Guido van Rossumad50ca92002-12-30 22:30:22 +000040 """
41 if type(args) != type(tuple()):
42 raise TypeError("2nd arg must be a tuple")
43 if type(kwargs) != type(dict()):
44 raise TypeError("3rd arg must be a dict")
Brett Cannon91012fe2003-06-13 23:56:32 +000045 global _main
46 _main = False
Guido van Rossumad50ca92002-12-30 22:30:22 +000047 try:
48 function(*args, **kwargs)
49 except SystemExit:
50 pass
51 except:
52 _traceback.print_exc()
Brett Cannon91012fe2003-06-13 23:56:32 +000053 _main = True
54 global _interrupt
Brett Cannon4e64d782003-06-13 23:44:35 +000055 if _interrupt:
Brett Cannon4e64d782003-06-13 23:44:35 +000056 _interrupt = False
57 raise KeyboardInterrupt
Guido van Rossumad50ca92002-12-30 22:30:22 +000058
59def exit():
Georg Brandl2067bfd2008-05-25 13:05:15 +000060 """Dummy implementation of _thread.exit()."""
Guido van Rossumad50ca92002-12-30 22:30:22 +000061 raise SystemExit
62
63def get_ident():
Georg Brandl2067bfd2008-05-25 13:05:15 +000064 """Dummy implementation of _thread.get_ident().
Guido van Rossumad50ca92002-12-30 22:30:22 +000065
Georg Brandl2067bfd2008-05-25 13:05:15 +000066 Since this module should only be used when _threadmodule is not
Guido van Rossumad50ca92002-12-30 22:30:22 +000067 available, it is safe to assume that the current process is the
68 only thread. Thus a constant can be safely returned.
69 """
70 return -1
71
72def allocate_lock():
Georg Brandl2067bfd2008-05-25 13:05:15 +000073 """Dummy implementation of _thread.allocate_lock()."""
Guido van Rossumad50ca92002-12-30 22:30:22 +000074 return LockType()
75
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076def stack_size(size=None):
Georg Brandl2067bfd2008-05-25 13:05:15 +000077 """Dummy implementation of _thread.stack_size()."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078 if size is not None:
79 raise error("setting thread stack size not supported")
80 return 0
81
Guido van Rossumad50ca92002-12-30 22:30:22 +000082class LockType(object):
Georg Brandl2067bfd2008-05-25 13:05:15 +000083 """Class implementing dummy implementation of _thread.LockType.
Tim Peters2c60f7a2003-01-29 03:49:43 +000084
Guido van Rossumad50ca92002-12-30 22:30:22 +000085 Compatibility is maintained by maintaining self.locked_status
86 which is a boolean that stores the state of the lock. Pickling of
Georg Brandl2067bfd2008-05-25 13:05:15 +000087 the lock, though, should not be done since if the _thread module is
Guido van Rossumad50ca92002-12-30 22:30:22 +000088 then used with an unpickled ``lock()`` from here problems could
89 occur from this class not having atomic methods.
90
91 """
92
93 def __init__(self):
94 self.locked_status = False
Tim Peters2c60f7a2003-01-29 03:49:43 +000095
Guido van Rossumad50ca92002-12-30 22:30:22 +000096 def acquire(self, waitflag=None):
97 """Dummy implementation of acquire().
98
99 For blocking calls, self.locked_status is automatically set to
100 True and returned appropriately based on value of
101 ``waitflag``. If it is non-blocking, then the value is
102 actually checked and not set if it is already acquired. This
103 is all done so that threading.Condition's assert statements
104 aren't triggered and throw a little fit.
105
106 """
Brett Cannon40c8f232008-07-13 01:19:15 +0000107 if waitflag is None or waitflag:
Guido van Rossumad50ca92002-12-30 22:30:22 +0000108 self.locked_status = True
Brett Cannon40c8f232008-07-13 01:19:15 +0000109 return True
110 else:
Guido van Rossumad50ca92002-12-30 22:30:22 +0000111 if not self.locked_status:
112 self.locked_status = True
113 return True
114 else:
115 return False
Guido van Rossumad50ca92002-12-30 22:30:22 +0000116
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000117 __enter__ = acquire
118
119 def __exit__(self, typ, val, tb):
120 self.release()
121
Guido van Rossumad50ca92002-12-30 22:30:22 +0000122 def release(self):
123 """Release the dummy lock."""
124 # XXX Perhaps shouldn't actually bother to test? Could lead
125 # to problems for complex, threaded code.
126 if not self.locked_status:
127 raise error
128 self.locked_status = False
129 return True
130
131 def locked(self):
132 return self.locked_status
Brett Cannon4e64d782003-06-13 23:44:35 +0000133
Brett Cannon91012fe2003-06-13 23:56:32 +0000134# Used to signal that interrupt_main was called in a "thread"
Brett Cannon4e64d782003-06-13 23:44:35 +0000135_interrupt = False
Brett Cannon91012fe2003-06-13 23:56:32 +0000136# True when not executing in a "thread"
137_main = True
Brett Cannon4e64d782003-06-13 23:44:35 +0000138
139def interrupt_main():
140 """Set _interrupt flag to True to have start_new_thread raise
141 KeyboardInterrupt upon exiting."""
Brett Cannon91012fe2003-06-13 23:56:32 +0000142 if _main:
143 raise KeyboardInterrupt
144 else:
145 global _interrupt
146 _interrupt = True