__all__ = ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock',
'interrupt_main', 'LockType', 'RLock',
'_count']
TIMEOUT_MAX = 2**31
error = RuntimeError
def start_new_thread(function, args, kwargs={}):
if type(args) != type(tuple()):
raise TypeError("2nd arg must be a tuple")
if type(kwargs) != type(dict()):
raise TypeError("3rd arg must be a dict")
global _main
_main = False
try:
function(*args, **kwargs)
except SystemExit:
pass
except:
import traceback
traceback.print_exc()
_main = True
global _interrupt
if _interrupt:
_interrupt = False
raise KeyboardInterrupt
def exit():
raise SystemExit
def get_ident():
return -1
def allocate_lock():
return LockType()
def stack_size(size=None):
if size is not None:
raise error("setting thread stack size not supported")
return 0
def _set_sentinel():
return LockType()
def _count():
return 0
class LockType(object):
def __init__(self):
self.locked_status = False
def acquire(self, waitflag=None, timeout=-1):
if waitflag is None or waitflag:
self.locked_status = True
return True
else:
if not self.locked_status:
self.locked_status = True
return True
else:
if timeout > 0:
import time
time.sleep(timeout)
return False
__enter__ = acquire
def __exit__(self, typ, val, tb):
self.release()
def release(self):
if not self.locked_status:
raise error
self.locked_status = False
return True
def locked(self):
return self.locked_status
def _at_fork_reinit(self):
self.locked_status = False
def __repr__(self):
return "<%s %s.%s object at %s>" % (
"locked" if self.locked_status else "unlocked",
self.__class__.__module__,
self.__class__.__qualname__,
hex(id(self))
)
_interrupt = False
_main = True
def interrupt_main():
if _main:
raise KeyboardInterrupt
else:
global _interrupt
_interrupt = True
class RLock:
def __init__(self):
self.locked_count = 0
def acquire(self, waitflag=None, timeout=-1):
self.locked_count += 1
return True
__enter__ = acquire
def __exit__(self, typ, val, tb):
self.release()
def release(self):
if not self.locked_count:
raise error
self.locked_count -= 1
return True
def locked(self):
return self.locked_status != 0
def __repr__(self):
return "<%s %s.%s object owner=%s count=%s at %s>" % (
"locked" if self.locked_count else "unlocked",
self.__class__.__module__,
self.__class__.__qualname__,
get_ident() if self.locked_count else 0,
self.locked_count,
hex(id(self))
)