blob: ed50520ab322ada9a701e500c00091e0a93de521 [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
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000019# A dummy value
20TIMEOUT_MAX = 2**31
Guido van Rossumad50ca92002-12-30 22:30:22 +000021
Antoine Pitroudb1bad22010-11-05 19:58:28 +000022# 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 (??).
26
Guido van Rossumad50ca92002-12-30 22:30:22 +000027class error(Exception):
Georg Brandl2067bfd2008-05-25 13:05:15 +000028 """Dummy implementation of _thread.error."""
Guido van Rossumad50ca92002-12-30 22:30:22 +000029
30 def __init__(self, *args):
31 self.args = args
32
33def start_new_thread(function, args, kwargs={}):
Georg Brandl2067bfd2008-05-25 13:05:15 +000034 """Dummy implementation of _thread.start_new_thread().
Guido van Rossumad50ca92002-12-30 22:30:22 +000035
36 Compatibility is maintained by making sure that ``args`` is a
37 tuple and ``kwargs`` is a dictionary. If an exception is raised
Georg Brandl2067bfd2008-05-25 13:05:15 +000038 and it is SystemExit (which can be done by _thread.exit()) it is
Guido van Rossumad50ca92002-12-30 22:30:22 +000039 caught and nothing is done; all other exceptions are printed out
40 by using traceback.print_exc().
41
Brett Cannon4e64d782003-06-13 23:44:35 +000042 If the executed function calls interrupt_main the KeyboardInterrupt will be
43 raised when the function returns.
44
Guido van Rossumad50ca92002-12-30 22:30:22 +000045 """
46 if type(args) != type(tuple()):
47 raise TypeError("2nd arg must be a tuple")
48 if type(kwargs) != type(dict()):
49 raise TypeError("3rd arg must be a dict")
Brett Cannon91012fe2003-06-13 23:56:32 +000050 global _main
51 _main = False
Guido van Rossumad50ca92002-12-30 22:30:22 +000052 try:
53 function(*args, **kwargs)
54 except SystemExit:
55 pass
56 except:
Antoine Pitroudb1bad22010-11-05 19:58:28 +000057 import traceback
58 traceback.print_exc()
Brett Cannon91012fe2003-06-13 23:56:32 +000059 _main = True
60 global _interrupt
Brett Cannon4e64d782003-06-13 23:44:35 +000061 if _interrupt:
Brett Cannon4e64d782003-06-13 23:44:35 +000062 _interrupt = False
63 raise KeyboardInterrupt
Guido van Rossumad50ca92002-12-30 22:30:22 +000064
65def exit():
Georg Brandl2067bfd2008-05-25 13:05:15 +000066 """Dummy implementation of _thread.exit()."""
Guido van Rossumad50ca92002-12-30 22:30:22 +000067 raise SystemExit
68
69def get_ident():
Georg Brandl2067bfd2008-05-25 13:05:15 +000070 """Dummy implementation of _thread.get_ident().
Guido van Rossumad50ca92002-12-30 22:30:22 +000071
Georg Brandl2067bfd2008-05-25 13:05:15 +000072 Since this module should only be used when _threadmodule is not
Guido van Rossumad50ca92002-12-30 22:30:22 +000073 available, it is safe to assume that the current process is the
74 only thread. Thus a constant can be safely returned.
75 """
76 return -1
77
78def allocate_lock():
Georg Brandl2067bfd2008-05-25 13:05:15 +000079 """Dummy implementation of _thread.allocate_lock()."""
Guido van Rossumad50ca92002-12-30 22:30:22 +000080 return LockType()
81
Thomas Wouters0e3f5912006-08-11 14:57:12 +000082def stack_size(size=None):
Georg Brandl2067bfd2008-05-25 13:05:15 +000083 """Dummy implementation of _thread.stack_size()."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +000084 if size is not None:
85 raise error("setting thread stack size not supported")
86 return 0
87
Guido van Rossumad50ca92002-12-30 22:30:22 +000088class LockType(object):
Georg Brandl2067bfd2008-05-25 13:05:15 +000089 """Class implementing dummy implementation of _thread.LockType.
Tim Peters2c60f7a2003-01-29 03:49:43 +000090
Guido van Rossumad50ca92002-12-30 22:30:22 +000091 Compatibility is maintained by maintaining self.locked_status
92 which is a boolean that stores the state of the lock. Pickling of
Georg Brandl2067bfd2008-05-25 13:05:15 +000093 the lock, though, should not be done since if the _thread module is
Guido van Rossumad50ca92002-12-30 22:30:22 +000094 then used with an unpickled ``lock()`` from here problems could
95 occur from this class not having atomic methods.
96
97 """
98
99 def __init__(self):
100 self.locked_status = False
Tim Peters2c60f7a2003-01-29 03:49:43 +0000101
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000102 def acquire(self, waitflag=None, timeout=-1):
Guido van Rossumad50ca92002-12-30 22:30:22 +0000103 """Dummy implementation of acquire().
104
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.
111
112 """
Brett Cannon40c8f232008-07-13 01:19:15 +0000113 if waitflag is None or waitflag:
Guido van Rossumad50ca92002-12-30 22:30:22 +0000114 self.locked_status = True
Brett Cannon40c8f232008-07-13 01:19:15 +0000115 return True
116 else:
Guido van Rossumad50ca92002-12-30 22:30:22 +0000117 if not self.locked_status:
118 self.locked_status = True
119 return True
120 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000121 if timeout > 0:
Antoine Pitroudb1bad22010-11-05 19:58:28 +0000122 import time
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000123 time.sleep(timeout)
Guido van Rossumad50ca92002-12-30 22:30:22 +0000124 return False
Guido van Rossumad50ca92002-12-30 22:30:22 +0000125
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000126 __enter__ = acquire
127
128 def __exit__(self, typ, val, tb):
129 self.release()
130
Guido van Rossumad50ca92002-12-30 22:30:22 +0000131 def release(self):
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:
136 raise error
137 self.locked_status = False
138 return True
139
140 def locked(self):
141 return self.locked_status
Brett Cannon4e64d782003-06-13 23:44:35 +0000142
Brett Cannon91012fe2003-06-13 23:56:32 +0000143# Used to signal that interrupt_main was called in a "thread"
Brett Cannon4e64d782003-06-13 23:44:35 +0000144_interrupt = False
Brett Cannon91012fe2003-06-13 23:56:32 +0000145# True when not executing in a "thread"
146_main = True
Brett Cannon4e64d782003-06-13 23:44:35 +0000147
148def interrupt_main():
149 """Set _interrupt flag to True to have start_new_thread raise
150 KeyboardInterrupt upon exiting."""
Brett Cannon91012fe2003-06-13 23:56:32 +0000151 if _main:
152 raise KeyboardInterrupt
153 else:
154 global _interrupt
155 _interrupt = True