annotate Lib/_dummy_thread.py @ 66807:d28126ebd60d

Entries for datetime, callable, and collections.Counter.
author Raymond Hettinger <python@rcn.com>
date Thu, 09 Dec 2010 16:41:54 +0000
parents 385d239bcbfa
children b82c3e593db6
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
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 class error(Exception):
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 40742
diff changeset
28 """Dummy implementation of _thread.error."""
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 def __init__(self, *args):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 self.args = args
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 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
34 """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
35
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 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
37 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
38 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
39 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
40 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
41
29446
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
42 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
43 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
44
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 """
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 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
47 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
48 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
49 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
50 global _main
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
51 _main = False
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 try:
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 function(*args, **kwargs)
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 except SystemExit:
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 pass
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 except:
65987
385d239bcbfa Fix bootstrap issues when building without threads
Antoine Pitrou <solipsis@pitrou.net>
parents: 60598
diff changeset
57 import traceback
385d239bcbfa Fix bootstrap issues when building without threads
Antoine Pitrou <solipsis@pitrou.net>
parents: 60598
diff changeset
58 traceback.print_exc()
29447
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
59 _main = True
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
60 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
61 if _interrupt:
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
62 _interrupt = False
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
63 raise KeyboardInterrupt
26890
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 exit():
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.exit()."""
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 raise SystemExit
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 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
70 """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
71
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 40742
diff changeset
72 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
73 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
74 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
75 """
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 return -1
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 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
79 """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
80 return LockType()
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
82 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
83 """Dummy implementation of _thread.stack_size()."""
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
84 if size is not None:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
85 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
86 return 0
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
87
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 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
89 """Class implementing dummy implementation of _thread.LockType.
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26890
diff changeset
90
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 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
92 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
93 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
94 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
95 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
96
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 """
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 def __init__(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 self.locked_status = False
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26890
diff changeset
101
60598
01d1fd775d16 Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents: 48730
diff changeset
102 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
103 """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
104
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 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
106 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
107 ``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
108 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
109 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
110 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
111
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 """
48453
5f2431de63d0 Merged revisions 64903 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents: 47266
diff changeset
113 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
114 self.locked_status = True
48453
5f2431de63d0 Merged revisions 64903 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents: 47266
diff changeset
115 return True
5f2431de63d0 Merged revisions 64903 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents: 47266
diff changeset
116 else:
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 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
118 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
119 return True
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 else:
60598
01d1fd775d16 Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents: 48730
diff changeset
121 if timeout > 0:
65987
385d239bcbfa Fix bootstrap issues when building without threads
Antoine Pitrou <solipsis@pitrou.net>
parents: 60598
diff changeset
122 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
123 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
124 return False
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
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
126 __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
127
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 29447
diff changeset
128 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
129 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
130
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 def release(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 """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
133 # 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
134 # 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
135 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
136 raise error
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 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
138 return True
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 def locked(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 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
142
29447
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
143 # 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
144 _interrupt = False
29447
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
145 # 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
146 _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
147
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 27507
diff changeset
148 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
149 """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
150 KeyboardInterrupt upon exiting."""
29447
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
151 if _main:
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
152 raise KeyboardInterrupt
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
153 else:
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
154 global _interrupt
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
155 _interrupt = True