From b9902b83c910482bf31d39fa4d46c8739aad6689 Mon Sep 17 00:00:00 2001 From: su226 Date: Wed, 14 Oct 2026 19:19:08 +0800 Subject: [PATCH 1/2] :bug: fix traceback.print_exception --- arclet/entari/logger.py | 49 ++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/arclet/entari/logger.py b/arclet/entari/logger.py index 7d38460..0ace305 100644 --- a/arclet/entari/logger.py +++ b/arclet/entari/logger.py @@ -5,6 +5,7 @@ import logging import sys import traceback +from enum import Enum from pathlib import Path from types import TracebackType from typing import TYPE_CHECKING, cast @@ -183,15 +184,32 @@ def _hidden_upsteam(record: Record): logger.configure(patcher=_hidden_upsteam) -def loguru_exc_callback(cls: type[BaseException], val: BaseException, tb: TracebackType | None, *_, **__): - """loguru 异常回调 +class _Sentinel(Enum): + SENTINEL = "SENTINEL" + + +def _parse_value_tb(exc, value, tb): + if (value is _Sentinel.SENTINEL) != (tb is _Sentinel.SENTINEL): + raise ValueError("Both or neither of value and tb must be given") + if value is tb is _Sentinel.SENTINEL: + if exc is not None: + if isinstance(exc, BaseException): + return exc, exc.__traceback__ + raise TypeError(f"Exception expected for value, {type(exc).__name__} found") + else: + return None, None + return value, tb + + +_print_exception = traceback.print_exception - Args: - cls (Type[Exception]): 异常类 - val (Exception): 异常的实际值 - tb (TracebackType): 回溯消息 - """ - log.error.opt(exception=(cls, val, tb)).error("Exception:") + +def loguru_print_exception(exc: type[BaseException] | BaseException | None, /, value: BaseException | None | _Sentinel = _Sentinel.SENTINEL, tb: TracebackType | None | _Sentinel = _Sentinel.SENTINEL, *args, colorize: bool = True, **kwargs): + value, tb = _parse_value_tb(exc, value, tb) + if colorize: + log.error.opt(exception=(type(value), value, tb), colors=colorize).error("Exception:") + else: + _print_exception(exc, value, tb, *args, **kwargs) def loguru_print_trace(te: Trace): @@ -216,9 +234,20 @@ class FakeTraceback: log.error.opt(exception=(te.exc_type, te.exc_value, cast(TracebackType, FakeTraceback()))).error("Exception:") -traceback.print_exception = loguru_exc_callback +def loguru_excepthook(cls: type[BaseException], val: BaseException, tb: TracebackType | None): + """loguru 异常回调 + + Args: + cls (Type[Exception]): 异常类 + val (Exception): 异常的实际值 + tb (TracebackType): 回溯消息 + """ + log.error.opt(exception=(cls, val, tb)).error("Exception:") + + +traceback.print_exception = loguru_print_exception ExceptionHandler.print_trace = loguru_print_trace -sys.excepthook = loguru_exc_callback +sys.excepthook = loguru_excepthook def apply_log_save( From c39b3e438d7e5e6ab358bb26f62df3d412d864fa Mon Sep 17 00:00:00 2001 From: su226 Date: Wed, 14 Oct 2026 22:23:26 +0800 Subject: [PATCH 2/2] :art: run formatter --- arclet/entari/logger.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/arclet/entari/logger.py b/arclet/entari/logger.py index 0ace305..a78c4fc 100644 --- a/arclet/entari/logger.py +++ b/arclet/entari/logger.py @@ -204,7 +204,15 @@ def _parse_value_tb(exc, value, tb): _print_exception = traceback.print_exception -def loguru_print_exception(exc: type[BaseException] | BaseException | None, /, value: BaseException | None | _Sentinel = _Sentinel.SENTINEL, tb: TracebackType | None | _Sentinel = _Sentinel.SENTINEL, *args, colorize: bool = True, **kwargs): +def loguru_print_exception( + exc: type[BaseException] | BaseException | None, + /, + value: BaseException | None | _Sentinel = _Sentinel.SENTINEL, + tb: TracebackType | None | _Sentinel = _Sentinel.SENTINEL, + *args, + colorize: bool = True, + **kwargs, +): value, tb = _parse_value_tb(exc, value, tb) if colorize: log.error.opt(exception=(type(value), value, tb), colors=colorize).error("Exception:")