Beispiel #1
0
class DebugConsoleStdIn(BaseStdIn):

    overrides(BaseStdIn.readline)

    def readline(self, *args, **kwargs):
        sys.stderr.write(
            'Warning: Reading from stdin is still not supported in this console.\n'
        )
        return '\n'
class DebugConsole(InteractiveConsole, BaseInterpreterInterface):
    """Wrapper around code.InteractiveConsole, in order to send 
    errors and outputs to the debug console
    """
    
    overrides(BaseInterpreterInterface.createStdIn)
    def createStdIn(self):
        return DebugConsoleStdIn() #For now, raw_input is not supported in this console.


    overrides(InteractiveConsole.push)
    def push(self, line, frame):
        """Change built-in stdout and stderr methods by the 
        new custom StdMessage.
        execute the InteractiveConsole.push.
        Change the stdout and stderr back be the original built-ins
        
        Return boolean (True if more input is required else False), 
        output_messages and input_messages
        """
        more = False
        original_stdout = sys.stdout
        original_stderr = sys.stderr
        try:
            try:
                self.frame = frame
                out = sys.stdout = IOBuf()
                err = sys.stderr = IOBuf()
                more, _need_input = self.addExec(line)
            except Exception:
                exc = GetExceptionTracebackStr()
                err.buflist.append("Internal Error: %s" % (exc,))
        finally:
            #Remove frame references.
            self.frame = None
            frame = None
            sys.stdout = original_stdout
            sys.stderr = original_stderr            

        return more, out.buflist, err.buflist
    
    
    overrides(BaseInterpreterInterface.doAddExec)
    def doAddExec(self, line):
        return InteractiveConsole.push(self, line)
    

    overrides(InteractiveConsole.runcode)
    def runcode(self, code):
        """Execute a code object.

        When an exception occurs, self.showtraceback() is called to
        display a traceback.  All exceptions are caught except
        SystemExit, which is reraised.

        A note about KeyboardInterrupt: this exception may occur
        elsewhere in this code, and may not always be caught.  The
        caller should be prepared to deal with it.

        """
        try:
            Exec(code, self.frame.f_globals, self.frame.f_locals)
            pydevd_save_locals.save_locals(self.frame)
        except SystemExit:
            raise
        except:
            self.showtraceback()