annotate Lib/_dummy_thread.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents d52b68edbca6
children
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
84442
7769c4d72806 Issue #18200: Back out usage of ModuleNotFoundError (8d28d44f3a9a)
Brett Cannon <brett@python.org>
parents: 84114
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: 38084
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: 38084
diff changeset
80 if size is not None:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38084
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: 38084
diff changeset
82 return 0
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38084
diff changeset
83
85598
d52b68edbca6 Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84442
diff changeset
84 def _set_sentinel():
d52b68edbca6 Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84442
diff changeset
85 """Dummy implementation of _thread._set_sentinel()."""
d52b68edbca6 Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84442
diff changeset
86 return LockType()
d52b68edbca6 Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84442
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
37255
f4ca03b87160 dummy_thread needs to support PEP 343 too.
Phillip J. Eby <pje@telecommunity.com>
parents: 29447
diff changeset
126 __enter__ = acquire
f4ca03b87160 dummy_thread needs to support PEP 343 too.
Phillip J. Eby <pje@telecommunity.com>
parents: 29447
diff changeset
127
f4ca03b87160 dummy_thread needs to support PEP 343 too.
Phillip J. Eby <pje@telecommunity.com>
parents: 29447
diff changeset
128 def __exit__(self, typ, val, tb):
f4ca03b87160 dummy_thread needs to support PEP 343 too.
Phillip J. Eby <pje@telecommunity.com>
parents: 29447
diff changeset
129 self.release()
f4ca03b87160 dummy_thread needs to support PEP 343 too.
Phillip J. Eby <pje@telecommunity.com>
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