annotate Lib/_dummy_thread.py @ 70817:dd775b1561ba

Fix markup.
author Georg Brandl <georg@python.org>
date Tue, 14 Jun 2011 21:09:55 +0200
parents 2b21fcf3d9a9
children 8d28d44f3a9a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Drop-in replacement for the thread module.
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 Meant to be used as a brain-dead substitute so that threaded code does
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 not need to be rewritten for when the thread module is not present.
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 Suggested usage is::
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26890
diff changeset
7
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 try:
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 40742
diff changeset
9 import _thread
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 except ImportError:
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 40742
diff changeset
11 import _dummy_thread as _thread
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 """
40742
f1701ba2452b Merged revisions 53538-53622 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
14 # Exports only things specified by thread documentation;
f1701ba2452b Merged revisions 53538-53622 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
15 # skipping obsolete synonyms allocate(), start_new(), exit_thread().
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 __all__ = ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock',
29446
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
17 'interrupt_main', 'LockType']
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
60598
01d1fd775d16 Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents: 48730
diff changeset
19 # A dummy value
01d1fd775d16 Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents: 48730
diff changeset
20 TIMEOUT_MAX = 2**31
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
65987
385d239bcbfa Fix bootstrap issues when building without threads
Antoine Pitrou <solipsis@pitrou.net>
parents: 60598
diff changeset
22 # NOTE: this module can be imported early in the extension building process,
385d239bcbfa Fix bootstrap issues when building without threads
Antoine Pitrou <solipsis@pitrou.net>
parents: 60598
diff changeset
23 # and so top level imports of other modules should be avoided. Instead, all
385d239bcbfa Fix bootstrap issues when building without threads
Antoine Pitrou <solipsis@pitrou.net>
parents: 60598
diff changeset
24 # imports are done when needed on a function-by-function basis. Since threads
385d239bcbfa Fix bootstrap issues when building without threads
Antoine Pitrou <solipsis@pitrou.net>
parents: 60598
diff changeset
25 # are disabled, the import lock should not be an issue anyway (??).
385d239bcbfa Fix bootstrap issues when building without threads
Antoine Pitrou <solipsis@pitrou.net>
parents: 60598
diff changeset
26
68199
b82c3e593db6 Followup to issue #11140 and r88682: also patch _dummy_thread.
Antoine Pitrou <solipsis@pitrou.net>
parents: 65987
diff changeset
27 error = RuntimeError
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 def start_new_thread(function, args, kwargs={}):
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 40742
diff changeset
30 """Dummy implementation of _thread.start_new_thread().
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 Compatibility is maintained by making sure that ``args`` is a
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 tuple and ``kwargs`` is a dictionary. If an exception is raised
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 40742
diff changeset
34 and it is SystemExit (which can be done by _thread.exit()) it is
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 caught and nothing is done; all other exceptions are printed out
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 by using traceback.print_exc().
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
29446
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
38 If the executed function calls interrupt_main the KeyboardInterrupt will be
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
39 raised when the function returns.
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
40
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 """
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 if type(args) != type(tuple()):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 raise TypeError("2nd arg must be a tuple")
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 if type(kwargs) != type(dict()):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 raise TypeError("3rd arg must be a dict")
29447
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
46 global _main
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
47 _main = False
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 try:
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 function(*args, **kwargs)
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 except SystemExit:
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 pass
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 except:
65987
385d239bcbfa Fix bootstrap issues when building without threads
Antoine Pitrou <solipsis@pitrou.net>
parents: 60598
diff changeset
53 import traceback
385d239bcbfa Fix bootstrap issues when building without threads
Antoine Pitrou <solipsis@pitrou.net>
parents: 60598
diff changeset
54 traceback.print_exc()
29447
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
55 _main = True
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
56 global _interrupt
29446
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
57 if _interrupt:
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
58 _interrupt = False
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
59 raise KeyboardInterrupt
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 def exit():
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 40742
diff changeset
62 """Dummy implementation of _thread.exit()."""
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 raise SystemExit
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 def get_ident():
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 40742
diff changeset
66 """Dummy implementation of _thread.get_ident().
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 40742
diff changeset
68 Since this module should only be used when _threadmodule is not
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 available, it is safe to assume that the current process is the
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 only thread. Thus a constant can be safely returned.
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 """
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 return -1
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 def allocate_lock():
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 40742
diff changeset
75 """Dummy implementation of _thread.allocate_lock()."""
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 return LockType()
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
78 def stack_size(size=None):
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 40742
diff changeset
79 """Dummy implementation of _thread.stack_size()."""
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
80 if size is not None:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
81 raise error("setting thread stack size not supported")
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
82 return 0
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
83
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 class LockType(object):
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 40742
diff changeset
85 """Class implementing dummy implementation of _thread.LockType.
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26890
diff changeset
86
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 Compatibility is maintained by maintaining self.locked_status
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 which is a boolean that stores the state of the lock. Pickling of
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 40742
diff changeset
89 the lock, though, should not be done since if the _thread module is
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 then used with an unpickled ``lock()`` from here problems could
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 occur from this class not having atomic methods.
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 """
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 def __init__(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 self.locked_status = False
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26890
diff changeset
97
60598
01d1fd775d16 Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents: 48730
diff changeset
98 def acquire(self, waitflag=None, timeout=-1):
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 """Dummy implementation of acquire().
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 For blocking calls, self.locked_status is automatically set to
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 True and returned appropriately based on value of
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 ``waitflag``. If it is non-blocking, then the value is
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 actually checked and not set if it is already acquired. This
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 is all done so that threading.Condition's assert statements
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 aren't triggered and throw a little fit.
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
107
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 """
48453
5f2431de63d0 Merged revisions 64903 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents: 47266
diff changeset
109 if waitflag is None or waitflag:
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 self.locked_status = True
48453
5f2431de63d0 Merged revisions 64903 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents: 47266
diff changeset
111 return True
5f2431de63d0 Merged revisions 64903 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents: 47266
diff changeset
112 else:
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 if not self.locked_status:
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 self.locked_status = True
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 return True
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 else:
60598
01d1fd775d16 Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents: 48730
diff changeset
117 if timeout > 0:
65987
385d239bcbfa Fix bootstrap issues when building without threads
Antoine Pitrou <solipsis@pitrou.net>
parents: 60598
diff changeset
118 import time
60598
01d1fd775d16 Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents: 48730
diff changeset
119 time.sleep(timeout)
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 return False
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 29447
diff changeset
122 __enter__ = acquire
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 29447
diff changeset
123
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 29447
diff changeset
124 def __exit__(self, typ, val, tb):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 29447
diff changeset
125 self.release()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 29447
diff changeset
126
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 def release(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 """Release the dummy lock."""
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 # XXX Perhaps shouldn't actually bother to test? Could lead
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 # to problems for complex, threaded code.
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 if not self.locked_status:
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 raise error
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 self.locked_status = False
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 return True
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
135
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 def locked(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 return self.locked_status
29446
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
138
29447
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
139 # Used to signal that interrupt_main was called in a "thread"
29446
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
140 _interrupt = False
29447
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
141 # True when not executing in a "thread"
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
142 _main = True
29446
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
143
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
144 def interrupt_main():
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
145 """Set _interrupt flag to True to have start_new_thread raise
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
146 KeyboardInterrupt upon exiting."""
29447
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
147 if _main:
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
148 raise KeyboardInterrupt
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
149 else:
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
150 global _interrupt
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
151 _interrupt = True