wrong/ambiguous type descriptions for `LogRecord.exc_info`

未關閉
#161,918 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
52/100
Issue 類型
文件
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
python
領域
documentation

研究方向

Start with the LogRecord.exc_info and Formatter.formatException() entries in the logging documentation, then compare their descriptions with the behavior and typeshed references linked in the issue. Clarify which values these APIs allow and how the documented exception tuple handles missing exception or traceback values. Done means the documentation accurately describes the relevant cases; the issue leaves open whether code or typeshed changes are also needed.

由索引模型根據 Issue 內容生成。

描述

docs
Documentation

Hey.

The documentation of LogRecord describes the exc_info attribute as:

exc_info (tuple[type[BaseException], BaseException, types.TracebackType] | None) – An exception tuple with the current exception information, as returned by sys.exc_info(), or None if no exception information is available.

However, records created through the normal logger methods can contain values that don’t match this type:

import logging

class H(logging.Handler):
    def emit(self, record):
        print(repr(record.exc_info))

logger = logging.getLogger()
logger.addHandler(H())
logger.propagate = False

logger.error("b", exc_info=True)           # outside of an except-block
logger.error("c", exc_info=ValueError())   # exception that was never raised

which gives as of CPython 3.14.10:

(None, None, None)
(<class 'ValueError'>, ValueError(), None)

A very recent commit (938144efad4659f7aa66bf9ef362fd3c602c4bbc) already fixed the case of a false truth value, which as per “If exc_info does not evaluate as false, …” is IMO allowed.

Now:

  1. Outside an except-block it would be (None, None, None).
  2. If an exception instance is passed, the tuple is built from its traceback, which is None for exceptions that were never raised, while the documented type requires types.TracebackType.

I think typeshed already documents it properly:
https://github.com/python/typeshed/blob/80f7beabfbb71a634cc3dbca8e418a5da790142c/stdlib/logging/__init__.pyi#L63

https://github.com/python/typeshed/blob/80f7beabfbb71a634cc3dbca8e418a5da790142c/stdlib/logging/__init__.pyi#L318

So far I'd say this is a documentation issue.

Now I can create valid LogRecord-objects that don’t go through _log() and its normalisation:

>>> import logging
>>> l = logging.getLogger()
>>> r = logging.makeLogRecord({"exc_info": True})
>>> r.exc_info
True
>>> r = l.makeRecord("z", 40, "f", 1, "m", None, exc_info=False)
>>> r.exc_info
False
>>>

For makeLogRecord and makeRecord… it’s IMO not really clear what values these allow (de jure) for exc_info.
If they’d follow the spirit of “If exc_info does not evaluate as false, …” then the the above type description would be even more wrong, as then exc_info could be any object.

This would then IMO also make the types on typeshed wrong, cause while logging.Formatter.format() itself would call formatException() only if exc_info has a true truth value:
https://github.com/python/cpython/blob/ecefd40671f340d543c214b80744d4301d249f37/Lib/logging/__init__.py#L716-L720

that may be still any object, there's nothing for the false case and both format() and formatException() are public, so any subclass may call formatException() without the if record.exc_info:.

Last but not least:
formatException() is more or less documented to an even more restrictive type:

Formats the specified exception information (a standard exception tuple as returned by sys.exc_info()) as a string.

So not sure,... at least some documentation seems to be not exact... but maybe there also needs to be code changes if the normalisation of exc_info should happen not only via LogRecord objects created through _log()... and if not, typeshed might need some updates to account for values that may be given.

Cheers,
Chris

主要語言
Python
星號
78.2k
分支
36.6k
平均合併
19 小時 57 分鐘
30 天內合併 PR
630

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。