Пример #1
0
def code_table(frame):
    from werkzeug.debug.util import Namespace
    lines = []
    lineno = frame['context_lineno']
    if lineno is not None:
        lineno += 1
        for l in frame['pre_context']:
            lines.append(Namespace(mode='pre', lineno=lineno, code=l))
            lineno += 1

        lines.append(Namespace(mode='cur', lineno=lineno, code=frame['context_line']))
        lineno += 1
        for l in frame['post_context']:
            lines.append(Namespace(mode='post', lineno=lineno, code=l))
            lineno += 1

    else:
        lines.append(Namespace(mode='cur', lineno=1, code='Sourcecode not available'))
    return t_codetable.render(lines=lines)
Пример #2
0
def code_table(frame):
    lines = []
    lineno = frame['context_lineno']
    if lineno is not None:
        lineno += 1
        for l in frame['pre_context']:
            lines.append(Namespace(mode='pre', lineno=lineno, code=l))
            lineno += 1
        lines.append(
            Namespace(mode='cur', lineno=lineno, code=frame['context_line']))
        lineno += 1
        for l in frame['post_context']:
            lines.append(Namespace(mode='post', lineno=lineno, code=l))
            lineno += 1
    else:
        lines.append(
            Namespace(mode='cur', lineno=1, code='Sourcecode not available'))

    return t_codetable(dict(lines=lines))
Пример #3
0
    def create_debug_context(self, environ, exc_info, simple=False):
        exception_type, exception_value, tb = exc_info
        # skip first internal frame
        if not tb.tb_next is None:
            tb = tb.tb_next

        # load frames
        frames = []
        frame_map = {}
        tb_uid = None
        if not environ['wsgi.run_once'] and not environ['wsgi.multiprocess']:
            tb_uid = get_uid()
            frame_map = self.tracebacks[tb_uid] = {}

        plaintb_buffer = ['Traceback (most recent call last):']
        write = plaintb_buffer.append

        # walk through frames and collect information
        while tb is not None:
            if not tb.tb_frame.f_locals.get('__traceback_hide__', False):
                if tb_uid and not simple:
                    frame_uid = get_uid()
                    frame_map[frame_uid] = InteractiveDebugger(
                        self, tb.tb_frame)
                else:
                    frame_uid = None
                frame = get_frame_info(tb, simple=simple)
                frame['frame_uid'] = frame_uid
                frames.append(frame)
                write('  File "%s", line %s, in %s' %
                      (frame['filename'], frame['lineno'], frame['function']))
                if frame['raw_context_line'] is None:
                    write('    <no sourcecode available>')
                else:
                    write('    ' + frame['raw_context_line'])
            tb = tb.tb_next

        # guard for string exceptions
        if isinstance(exception_type, str):
            extypestr = 'string exception'
            exception_value = exception_type
        elif exception_type.__module__ == 'exceptions':
            extypestr = exception_type.__name__
        else:
            extypestr = '%s.%s' % (exception_type.__module__,
                                   exception_type.__name__)

        # finialize plain traceback and write it to stderr
        try:
            if isinstance(exception_value, six.text_type):
                exception_value = exception_value.encode('utf-8')
            else:
                exception_value = str(exception_value)
            exvalstr = ': ' + exception_value
        except:
            exvalstr = ''
        write(extypestr + exvalstr)
        plaintb = '\n'.join(plaintb_buffer)

        if not simple:
            environ['wsgi.errors'].write(plaintb)

        # support for the werkzeug request object or fall back to
        # WSGI environment
        req_vars = []
        if not simple:
            request = environ.get(self.request_key)
            if request is not None:
                for varname in dir(request):
                    if varname.startswith('_'):
                        continue
                    try:
                        value = getattr(request, varname)
                    except Exception as err:
                        value = ExceptionRepr(err)
                    if not hasattr(value, 'im_func'):
                        req_vars.append((varname, value))
            else:
                req_vars.append(('WSGI Environ', environ))

        return Namespace(
            evalex=self.evalex,
            exception_type=extypestr,
            exception_value=exception_value,
            frames=frames,
            last_frame=frames[-1],
            plaintb=plaintb,
            tb_uid=tb_uid,
            frame_map=frame_map,
            req_vars=req_vars,
        )
Пример #4
0
                        continue
                    try:
                        value = getattr(request, varname)
                    except Exception, err:
                        value = ExceptionRepr(err)
                    if not hasattr(value, 'im_func'):
                        req_vars.append((varname, value))
            else:
                req_vars.append(('WSGI Environ', environ))

        return Namespace(
            evalex=self.evalex,
            exception_type=extypestr,
            exception_value=exception_value,
            frames=frames,
            last_frame=frames[-1],
            plaintb=plaintb,
            tb_uid=tb_uid,
            frame_map=frame_map,
            req_vars=req_vars,
        )


class InteractiveDebugger(code.InteractiveInterpreter):
    """
    Subclass of the python interactive interpreter that
    automatically captures stdout and buffers older input.
    """
    def __init__(self, middleware, frame):
        self.middleware = middleware
        self.globals = frame.f_globals