Example #1
0
def printing_tb(
    reraise: bool = True,
    file_: Optional[Union[TextIO, LoggerAsFile]] = sys.stderr,
    skip_cur_frame: bool = False,
    num_context_lines: int = 1,
    max_value_str_len: int = 1000,
    max_exc_str_len: int = 10000,
    ellipsis_: str = '...',
    color_scheme: ColorScheme = ColorSchemes.auto,
):
    try:
        yield

    except Exception as e:
        if file_:
            for trace_str in iter_tb_lines(
                    e=e,
                    tb=None,
                    num_skipped_frames=2 if skip_cur_frame else 1,
                    max_value_str_len=max_value_str_len,
                    max_exc_str_len=max_exc_str_len,
                    ellipsis_=ellipsis_,
                    num_context_lines=num_context_lines,
                    color_scheme=choose_color_scheme(color_scheme, file_),
            ):
                file_.write(trace_str)
                file_.write('\n')

            file_.flush()

        if reraise:
            raise e
Example #2
0
    def print_tb(e, tb, num_skipped_frames: int) -> NoReturn:
        for line in iter_tb_lines(
                e=e,
                tb=tb,
                num_context_lines=num_context_lines,
                max_value_str_len=max_value_str_len,
                max_exc_str_len=max_exc_str_len,
                ellipsis_=ellipsis_,
                color_scheme=choose_color_scheme(color_scheme, sys.stderr),
                num_skipped_frames=num_skipped_frames,
        ):
            sys.stderr.write(line)
            sys.stderr.write('\n')

        sys.stderr.flush()
    def excepthook(
            e_cls,  # noqa
            e,
            tb):
        for line in iter_tb_lines(
                e=e,
                tb=tb,
                num_context_lines=num_context_lines,
                max_value_str_len=max_value_str_len,
                max_exc_str_len=max_exc_str_len,
                ellipsis_=ellipsis_,
        ):
            sys.stderr.write(line)
            sys.stderr.write('\n')

        sys.stderr.flush()
def check(name, arg, **kwargs):
    try:
        f(arg)
    except Exception as e:  # noqa
        assert_smart_equals_ref('test_core.{}'.format(name), '\n'.join(core.iter_tb_lines(e, **kwargs)))