Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def just_raise(*a, **kw):
if throw_args:
reraise(throw_args[0], throw_args[1], throw_args[2])
else:
raise greenlet.GreenletExit()
g.run = just_raise
if isinstance(g, GreenThread):
# it's a GreenThread object, so we want to call its main method to take advantage of
# the notification
try:
g.main(just_raise, (), {})
except:
pass
current = greenlet.getcurrent()
if current is not hub:
# arrange to wake the caller back up immediately
hub.schedule_call_now(current.switch)
g.throw(*throw_args)
def read_block(self, mode, pid, timeout=0.5):
current = greenlet.getcurrent()
s = self.read_waiters[mode, pid]
s.add(current)
def timeout_cb():
s.remove(current)
current.throw(TimeoutError('Read timeout exceeded'))
_t = ioloop.add_timeout(time.time() + timeout, timeout_cb)
frame = current.parent.switch()
ioloop.remove_timeout(_t)
return frame
def on_task_result(self, task_id, result):
assert get_current_greenlet() is self._server_greenlet
if task_id not in self._greenlets:
# ignore for dropped tasks
return
self._greenlets[task_id].switch(result)
def _is_global_greenlet():
return not hasattr(get_current_greenlet(), 'cc_greenlet')
def perform_value(obj, ctx, value):
child = greenlet.getcurrent()
if not child.parent:
obj.error('unhandled effect %s' % (value.repr(ctx)), ctx=ctx)
try:
result = child.parent.switch(value)
except ProgramError as e:
# Reset exception info to come from this stack, rather than the stack of
# the handler
e.stack_trace = ctx.get_stack_trace()
e.info = obj.info
raise e
return result
def _coroutine_getmain():
try:
return _tls.main_coroutine
except AttributeError:
main = coroutine()
main._is_started = -1
main._greenlet = greenlet.getcurrent()
_tls.main_coroutine = main
return _tls.main_coroutine
The fileno argument is the file number of the file of interest. The other
arguments are either callbacks or None. If there is a callback for read
or write, the hub sets things up so that when the file descriptor is
ready to be read or written, the callback is called.
The exc callback is called when the socket represented by the file
descriptor is closed. The intent is that the the exc callbacks should
only be present when either a read or write callback is also present,
so the exc callback happens instead of the respective read or write
callback.
"""
self.readers[fileno] = read or self.readers.get(fileno)
self.writers[fileno] = write or self.writers.get(fileno)
self.excs[fileno] = exc or self.excs.get(fileno)
self.waiters_by_greenlet[greenlet.getcurrent()] = fileno
def waitfor(self, msg_id, retval=None):
#FIXME: add some kind of timeout
gr = greenlet.getcurrent()
self.handle(msg_id, gr)
return gr.parent.switch(retval)
def __init__(self, interp=None, request_refresh=lambda: None):
"""
interp is an interpreter object to use. By default a new one is
created.
request_refresh is a function that will be called each time the running
code asks for a refresh - to, for example, update the screen.
"""
self.interp = interp or code.InteractiveInterpreter()
self.source = None
self.main_context = greenlet.getcurrent()
self.code_context = None
self.request_refresh = request_refresh
# waiting for response from main thread
self.code_is_waiting = False
# sigint happened while in main thread
self.sigint_happened_in_main_context = False
self.orig_sigint_handler = None
def _setup(self):
self._has_setup = True
self.cur = None
self.timings = {}
self.current_tasklet = greenlet.getcurrent()
self.thread_id = thread.get_ident()
self.simulate_call("profiler")