Builtin input does not always correctly handle errors from `PyOS_Readline`
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
Assertion failure sending SIGINT while calling input
import os, signal, threading
send = threading.Event()
def interrupter():
while True:
send.wait()
os.kill(os.getpid(), signal.SIGINT)
threading.Thread(target=interrupter).start()
send.set()
while True:
try:
input(">")
except KeyboardInterrupt:
pass
Note, this is a modified version of the example code in #112585.
Python (tested v3.12.10, v3.13.3, v3.14.0b1, and current HEAD), built with assertions enabled:
~/src/cpython/ $ ./python crash.py
>>>>>>>>>>>python: Objects/call.c:342: _PyObject_Call: Assertion `!_PyErr_Occurred(tstate)' failed.
Aborted (core dumped)
IIUC the issue is builtin_input_impl in Python/bltinmodule.c calls PyOS_Readline but assumes it cannot set an exception:
s = PyOS_Readline(stdin, stdout, promptstr);
if (s == NULL) {
PyErr_CheckSignals();
if (!PyErr_Occurred())
PyErr_SetNone(PyExc_KeyboardInterrupt);
If PyOS_Readline sets an exception (e.g. as in this case by running an interrupt signal handler itself), as opposed to being interrupted by a signal and returning NULL without setting the exception, and another signal with a handler is pending when PyErr_CheckSignals() is called, the handler will be called with the prior exception already set, and hence the assertion fails.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
No response
Linked PRs
- gh-134645
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 Python/bltinmodule.c 中的 builtin_input_impl 開始,使用提供的訊號與輸入範例重現該 assertion。追蹤 PyOS_Readline 與 PyErr_CheckSignals 如何處理現有的例外;完成的標準是重現過程不再因 _PyObject_Call assertion 而中止,並保留預期的中斷行為。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- operating-systems
- Issue 類型
- 缺陷
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 35/100