コード例 #1
0
    def throw(self, *args):
        """Immediatelly switch into the greenlet and raise an exception in it.

        Should only be called from the HUB, otherwise the current greenlet is left unscheduled forever.
        To raise an exception in a safely manner from any greenlet, use :meth:`kill`.

        If a greenlet was started but never switched to yet, then also
        a) cancel the event that will start it
        b) fire the notifications as if an exception was raised in a greenlet
        """
        if self._start_event is None:
            self._start_event = _dummy_event
        else:
            self._start_event.stop()
        try:
            greenlet.throw(self, *args)
        finally:
            if self._exception is _NONE and self.dead:
                # the greenlet was never switched to before and it will never be, _report_error was not called
                # the result was not set and the links weren't notified. let's do it here.
                # checking that self.dead is true is essential, because throw() does not necessarily kill the greenlet
                # (if the exception raised by throw() is caught somewhere inside the greenlet).
                if len(args) == 1:
                    arg = args[0]
                    #if isinstance(arg, type):
                    if type(arg) is type(Exception):
                        args = (arg, arg(), None)
                    else:
                        args = (type(arg), arg, None)
                elif not args:
                    args = (GreenletExit, GreenletExit(), None)
                self._report_error(args)
コード例 #2
0
ファイル: greenlet.py プロジェクト: bigmlcom/pylibs
    def throw(self, *args):
        """Immediatelly switch into the greenlet and raise an exception in it.

        Should only be called from the HUB, otherwise the current greenlet is left unscheduled forever.
        To raise an exception in a safely manner from any greenlet, use :meth:`kill`.
        """
        if self._start_event is not None:
            self._start_event.cancel()
            self._start_event = None
        if not self.dead:
            if self:
                return greenlet.throw(self, *args)
            else:
                # special case for when greenlet is not yet started, because _report_error is not executed
                if len(args)==1:
                    self._exception = args[0]
                elif not args:
                    self._exception = GreenletExit()
                else:
                    self._exception = args[1]
                try:
                    try:
                        # Even though the greenlet is not yet started, we calling throw() here
                        # so that its 'dead' attribute becomes True
                        return greenlet.throw(self, *args)
                    except:
                        # since this function is called from the Hub, which is the parent of *greenlet*
                        # the above statement will re-reraise here whatever we've thrown in it
                        # this traceback is useless, so we silent it
                        pass
                finally:
                    if self._links and self._notifier is None:
                        self._notifier = core.active_event(self._notify_links)
コード例 #3
0
ファイル: greenlet.py プロジェクト: hokhyk/gevent
 def __handle_death_before_start(self, *args):
     # args is (t, v, tb) or simply t or v
     if self._exc_info is Greenlet._exc_info and self.dead:
         # the greenlet was never switched to before and it will never be, _report_error was not called
         # the result was not set and the links weren't notified. let's do it here.
         # checking that self.dead is true is essential, because throw() does not necessarily kill the greenlet
         # (if the exception raised by throw() is caught somewhere inside the greenlet).
         if len(args) == 1:
             arg = args[0]
             #if isinstance(arg, type):
             if type(arg) is type(Exception):
                 args = (arg, arg(), None)
             else:
                 args = (type(arg), arg, None)
         elif not args:
             args = (GreenletExit, GreenletExit(), None)
         self._report_error(args)