File: debug_helper.py

package info (click to toggle)
python-friendly-traceback 0.7.62%2Bgit20240811.d7dbff6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,264 kB
  • sloc: python: 21,500; makefile: 4
file content (44 lines) | stat: -rw-r--r-- 1,092 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Debug helper

The purpose of this file is to help during development.

The idea is to silence internal exceptions raised by Friendly
itself for most users by redirecting them here, and have them
printed only when debugging mode is activated.
"""

import inspect
import sys
from typing import Optional

from .ft_gettext import current_lang

_ = current_lang.translate

# DEBUG is set to True when running with pytest.
# It can also be set to True from __main__ or when
# using the debug() command in the console.

DEBUG = False
SHOW_DEBUG_HELPER = False


def log_error(exc: Optional[BaseException] = None) -> None:
    if DEBUG:  # pragma: no cover
        if exc is not None:
            print(repr(exc))
            frame = inspect.currentframe().f_back
            print(f"{frame.f_code.co_filename}, line: {frame.f_lineno}")
        sys.exit()


def log(*args: str) -> None:
    if DEBUG:
        for arg in args:
            print(arg)


def handle_internal_error(arg: str) -> None:
    print(_("Fatal error - aborting"), arg)
    print(_("Please report this issue."))
    sys.exit()