Skip to content

API Reference

dttb

DateTime TraceBack

Callback = Callable[[CallbackArgs], Any] module-attribute

CallbackArgs

Bases: NamedTuple

Arguments passed to the callback function when an uncaught exception occurs.

Attributes:

Name Type Description
now datetime

The timestamp when the exception was caught.

exc_type Type[BaseException]

The type of the exception.

exc_value Optional[BaseException]

The exception instance, or None if not available.

exc_traceback Optional[TracebackType]

The traceback object, or None if not available.

thread Optional[Thread]

The thread in which the exception occurred, or None if it occurred in the main thread.

Source code in src/dttb/__init__.py
class CallbackArgs(NamedTuple):
    """Arguments passed to the callback function when an uncaught exception occurs.

    Attributes:
        now: The timestamp when the exception was caught.
        exc_type: The type of the exception.
        exc_value: The exception instance, or `None` if not available.
        exc_traceback: The traceback object, or `None` if not available.
        thread: The thread in which the exception occurred, or `None` if it occurred in
            the main thread.
    """

    now: dt.datetime
    exc_type: Type[BaseException]
    exc_value: Optional[BaseException]
    exc_traceback: Optional[TracebackType]
    thread: Optional[threading.Thread]

apply(*, tz=None, callback=None)

Applies attaching datetime to exception traceback.

This also supports logging and threading if involved.

Parameters:

Name Type Description Default
tz Optional[tzinfo]

An optional datetime.tzinfo object used to determine the timezone of the timestamp. If None or not given, the local timezone is used.

None
callback Optional[Callback]

An optional Callback object that is called with a CallbackArgs object as its only argument whenever an uncaught exception occurs.

None
Source code in src/dttb/__init__.py
def apply(
    *,
    tz: Optional[dt.tzinfo] = None,
    callback: Optional[Callback] = None,
) -> None:
    """Applies attaching datetime to exception traceback.

    This also supports logging and threading if involved.

    Args:
        tz: An optional `datetime.tzinfo` object used to determine the timezone of the
            timestamp. If `None` or not given, the local timezone is used.
        callback: An optional `Callback` object that is called with a `CallbackArgs`
            object as its only argument whenever an uncaught exception occurs.
    """
    sys.excepthook = _dttb_sys_excepthook(
        tz=tz,
        callback=callback,
    )(_sys_excepthook)
    threading.excepthook = _dttb_threading_excepthook(
        tz=tz,
        callback=callback,
    )(_threading_excepthook)

reset()

Resets to the default exception traceback.

Source code in src/dttb/__init__.py
def reset() -> None:
    """Resets to the default exception traceback."""
    sys.excepthook = _sys_excepthook
    threading.excepthook = _threading_excepthook