Exemplo n.º 1
0
    def _render_legacy(self, io):
        if hasattr(self._exception, "__traceback__"):
            tb = self._exception.__traceback__
        else:
            tb = self._exc_info[2]

        title = "\n<error>{}</error>\n\n<b>{}</b>".format(
            self._exception.__class__.__name__, str(self._exception))

        io.write_line(title)

        if io.is_verbose():
            io.write_line("")
            self._render_traceback(io, tb)
Exemplo n.º 2
0
    def _render_trace(self, io: Union[IO, Output],
                      frames: FrameCollection) -> None:
        stack_frames = FrameCollection()
        for frame in frames:
            if (self._ignore and re.match(self._ignore, frame.filename)
                    and not io.is_debug()):
                continue

            stack_frames.append(frame)

        remaining_frames_length = len(stack_frames) - 1
        if io.is_verbose() and remaining_frames_length:
            self._render_line(io, "<fg=yellow>Stack trace</>:", True)
            max_frame_length = len(str(remaining_frames_length))
            frame_collections = stack_frames.compact()
            i = remaining_frames_length
            for collection in frame_collections:
                if collection.is_repeated():
                    if len(collection) > 1:
                        frames_message = "<fg=yellow>{}</> frames".format(
                            len(collection))
                    else:
                        frames_message = "frame"

                    self._render_line(
                        io,
                        "<fg=blue>{:>{}}</>  Previous {} repeated <fg=blue>{}</> times"
                        .format(
                            "...",
                            max_frame_length,
                            frames_message,
                            collection.repetitions + 1,
                        ),
                        True,
                    )

                    i -= len(collection) * (collection.repetitions + 1)

                for frame in collection:
                    relative_file_path = self._get_relative_file_path(
                        frame.filename)
                    relative_file_path_parts = relative_file_path.split(
                        os.path.sep)
                    relative_file_path = "{}".format(
                        "<fg=default;options=dark>{}</>".format(
                            Formatter.escape(
                                os.sep)).join(relative_file_path_parts[:-1] + [
                                    "<fg=default;options=bold>{}</>".format(
                                        relative_file_path_parts[-1])
                                ]), )

                    self._render_line(
                        io,
                        "<fg=yellow>{:>{}}</>  {}<fg=default;options=dark>:</><b>{}</b> in <fg=cyan>{}</>"
                        .format(
                            i,
                            max_frame_length,
                            relative_file_path,
                            frame.lineno,
                            frame.function,
                        ),
                        True,
                    )

                    if io.is_debug():
                        if (frame, 2, 2) not in self._FRAME_SNIPPET_CACHE:
                            code_lines = Highlighter(
                                supports_utf8=io.supports_utf8()).code_snippet(
                                    frame.file_content,
                                    frame.lineno,
                                )

                            self._FRAME_SNIPPET_CACHE[(frame, 2,
                                                       2)] = code_lines

                        code_lines = self._FRAME_SNIPPET_CACHE[(frame, 2, 2)]

                        for code_line in code_lines:
                            self._render_line(
                                io,
                                "{:>{}}{}".format(" ", max_frame_length,
                                                  code_line),
                                indent=3,
                            )
                    else:
                        highlighter = Highlighter(
                            supports_utf8=io.supports_utf8())
                        try:
                            code_line = highlighter.highlighted_lines(
                                frame.line.strip())[0]
                        except tokenize.TokenError:
                            code_line = frame.line.strip()

                        self._render_line(
                            io,
                            "{:>{}}    {}".format(
                                " ",
                                max_frame_length,
                                code_line,
                            ),
                        )

                    i -= 1
Exemplo n.º 3
0
    def _render_trace(self, io, frames):
        from crashtest.frame_collection import FrameCollection

        stack_frames = FrameCollection()
        for frame in frames:
            if (self._ignore and re.match(self._ignore, frame.filename)
                    and not io.is_debug()):
                continue

            stack_frames.append(frame)

        remaining_frames_length = len(stack_frames) - 1
        if io.is_verbose() and remaining_frames_length:
            self._render_line(io, "<fg=yellow>Stack trace</>:", True)
            max_frame_length = len(str(remaining_frames_length))
            frame_collections = stack_frames.compact()
            i = remaining_frames_length
            for collection in frame_collections:
                if collection.is_repeated():
                    if len(collection) > 1:
                        frames_message = "<fg=yellow>{}</> frames".format(
                            len(collection))
                    else:
                        frames_message = "frame"

                    self._render_line(
                        io,
                        "<fg=blue>{:>{}}</>  Previous {} repeated <fg=blue>{}</> times"
                        .format(
                            "...",
                            max_frame_length,
                            frames_message,
                            collection.repetitions,
                        ),
                        True,
                    )

                    i -= len(collection) * collection.repetitions + len(
                        collection)

                for frame in collection:
                    self._render_line(
                        io,
                        "<fg=yellow>{:>{}}</>  <fg=default;options=bold>{}</>:<b>{}</b> in <fg=cyan>{}</>"
                        .format(
                            i,
                            max_frame_length,
                            self._get_relative_file_path(frame.filename),
                            frame.lineno,
                            frame.function,
                        ),
                        True,
                    )

                    if io.is_debug():
                        if (frame, 2, 2) not in self._FRAME_SNIPPET_CACHE:
                            code_lines = self._higlighter.code_snippet(
                                frame.file_content,
                                frame.lineno,
                            )

                            self._FRAME_SNIPPET_CACHE[(frame, 2,
                                                       2)] = code_lines

                        code_lines = self._FRAME_SNIPPET_CACHE[(frame, 2, 2)]

                        for code_line in code_lines:
                            self._render_line(
                                io,
                                "{:>{}}{}".format(" ", max_frame_length,
                                                  code_line),
                                indent=1,
                            )
                    else:
                        self._render_line(
                            io,
                            "{:>{}}  {}".format(" ", max_frame_length,
                                                frame.line.strip()),
                        )

                    i -= 1