Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.3k
Expand file tree
/
Copy path_dummy_thread.py
More file actions
Latest commit
155 lines (125 loc) · 4.76 KB
/
Copy path_dummy_thread.py
File metadata and controls
155 lines (125 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""Drop-in replacement for the thread module.
Meant to be used as a brain-dead substitute so that threaded code does
not need to be rewritten for when the thread module is not present.
Suggested usage is::
try:
import _thread
except ImportError:
import _dummy_thread as _thread
"""
# Exports only things specified by thread documentation;
# skipping obsolete synonyms allocate(), start_new(), exit_thread().
__all__= ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock',
'interrupt_main', 'LockType']
# A dummy value
TIMEOUT_MAX=2**31
# NOTE: this module can be imported early in the extension building process,
# and so top level imports of other modules should be avoided. Instead, all
# imports are done when needed on a function-by-function basis. Since threads
# are disabled, the import lock should not be an issue anyway (??).
classerror(Exception):
"""Dummy implementation of _thread.error."""
def__init__(self, *args):
self.args=args
defstart_new_thread(function, args, kwargs={}):
"""Dummy implementation of _thread.start_new_thread().
Compatibility is maintained by making sure that ``args`` is a
tuple and ``kwargs`` is a dictionary. If an exception is raised
and it is SystemExit (which can be done by _thread.exit()) it is
caught and nothing is done; all other exceptions are printed out
by using traceback.print_exc().
If the executed function calls interrupt_main the KeyboardInterrupt will be
raised when the function returns.
"""
iftype(args) !=type(tuple()):
raiseTypeError("2nd arg must be a tuple")
iftype(kwargs) !=type(dict()):
raiseTypeError("3rd arg must be a dict")
global_main
_main=False
try:
function(*args, **kwargs)
exceptSystemExit:
pass
except:
importtraceback
traceback.print_exc()
_main=True
global_interrupt
if_interrupt:
_interrupt=False
raiseKeyboardInterrupt
defexit():
"""Dummy implementation of _thread.exit()."""
raiseSystemExit
defget_ident():
"""Dummy implementation of _thread.get_ident().
Since this module should only be used when _threadmodule is not
available, it is safe to assume that the current process is the
only thread. Thus a constant can be safely returned.
"""
return-1
defallocate_lock():
"""Dummy implementation of _thread.allocate_lock()."""
returnLockType()
defstack_size(size=None):
"""Dummy implementation of _thread.stack_size()."""
ifsizeisnotNone:
raiseerror("setting thread stack size not supported")
return0
classLockType(object):
"""Class implementing dummy implementation of _thread.LockType.
Compatibility is maintained by maintaining self.locked_status
which is a boolean that stores the state of the lock. Pickling of
the lock, though, should not be done since if the _thread module is
then used with an unpickled ``lock()`` from here problems could
occur from this class not having atomic methods.
"""
def__init__(self):
self.locked_status=False
defacquire(self, waitflag=None, timeout=-1):
"""Dummy implementation of acquire().
For blocking calls, self.locked_status is automatically set to
True and returned appropriately based on value of
``waitflag``. If it is non-blocking, then the value is
actually checked and not set if it is already acquired. This
is all done so that threading.Condition's assert statements
aren't triggered and throw a little fit.
"""
ifwaitflagisNoneorwaitflag:
self.locked_status=True
returnTrue
else:
ifnotself.locked_status:
self.locked_status=True
returnTrue
else:
iftimeout>0:
importtime
time.sleep(timeout)
returnFalse
__enter__=acquire
def__exit__(self, typ, val, tb):
self.release()
defrelease(self):
"""Release the dummy lock."""
# XXX Perhaps shouldn't actually bother to test? Could lead
# to problems for complex, threaded code.
ifnotself.locked_status:
raiseerror
self.locked_status=False
returnTrue
deflocked(self):
returnself.locked_status
# Used to signal that interrupt_main was called in a "thread"
_interrupt=False
# True when not executing in a "thread"
_main=True
definterrupt_main():
"""Set _interrupt flag to True to have start_new_thread raise
KeyboardInterrupt upon exiting."""
if_main:
raiseKeyboardInterrupt
else:
global_interrupt
_interrupt=True