warn_explicit() discards the source line obtained from module_globals
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
The only purpose of the module_globals argument of warnings.warn_explicit() is to get the source line of the warning from the module loader when the file cannot be read. The C implementation computes it in get_source_line(), but then throws it away.
call_show_warning() passes None in the line slot of WarningMessage and ignores its own sourceline argument:
msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
filename, lineno_obj, Py_None, Py_None,
source ? source : Py_None, module,
NULL);
sourceline is only used in the fallback show_warning() path, taken when warnings._showwarnmsg is not available.
As a result the source line is lost, while the pure Python implementation, which seeds linecache instead, displays it:
$ cat > spam.py <<EOF
import warnings
def f():
warnings.warn_explicit('eggs', UserWarning, 'bar', 1, module_globals=globals())
EOF
$ ./python -c 'import spam; spam.f()'
bar:1: UserWarning: eggs
$ ./python -c 'import sys; sys.modules["_warnings"] = None; import spam; spam.f()'
bar:1: UserWarning: eggs
import warnings
The None was added in 914cde89d4c together with the source argument, so the C implementation has never passed the source line.
Linked PRs
- gh-155320
- gh-155824
- gh-155825
- gh-155826
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 C 函式 get_source_line() 和 call_show_warning() 開始,issue 指出它們會計算原始碼行,然後將其丟棄。重現 issue 中的 warn_explicit() 範例,並驗證原始碼行在 WarningMessage 路徑以及備援的 show_warning() 路徑中都能保留。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- c, python
- 領域
- backend
- Issue 類型
- 缺陷
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100