python / python/cpython

REPL exits with traceback when str(sys.ps1) raises

未關閉
#130,698 7 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib topic-repl type-bug
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

Subject says it all. This is on the latest HEAD of main.

~/cpython$ g pull
Already up to date.
~/cpython$ make
Checked 112 modules (34 built-in, 78 shared, 0 n/a on macosx-14.6-x86_64, 0 disabled, 0 missing, 0 failed on import)
./python.exe -E ./Tools/build/generate-build-details.py `cat pybuilddir.txt`/build-details.json
~/cpython$ ./python.exe 
Python 3.14.0a5+ (heads/main:ab11c097052, Feb 28 2025, 08:56:47) [Clang 16.0.0 (clang-1600.0.26.6)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> class C:
...     def __str__(self): 1/0
...     
>>> sys.ps1 = C()
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/guido/cpython/Lib/_pyrepl/__main__.py", line 6, in <module>
    __pyrepl_interactive_console()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/guido/cpython/Lib/_pyrepl/main.py", line 59, in interactive_console
    run_multiline_interactive_console(console)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "/Users/guido/cpython/Lib/_pyrepl/simple_interact.py", line 143, in run_multiline_interactive_console
    statement = multiline_input(more_lines, ps1, ps2)
  File "/Users/guido/cpython/Lib/_pyrepl/readline.py", line 389, in multiline_input
    return reader.readline()
           ~~~~~~~~~~~~~~~^^
  File "/Users/guido/cpython/Lib/_pyrepl/reader.py", line 796, in readline
    self.prepare()
    ~~~~~~~~~~~~^^
  File "/Users/guido/cpython/Lib/_pyrepl/historical_reader.py", line 306, in prepare
    super().prepare()
    ~~~~~~~~~~~~~~~^^
  File "/Users/guido/cpython/Lib/_pyrepl/reader.py", line 643, in prepare
    self.calc_screen()
    ~~~~~~~~~~~~~~~~^^
  File "/Users/guido/cpython/Lib/_pyrepl/completing_reader.py", line 261, in calc_screen
    screen = super().calc_screen()
  File "/Users/guido/cpython/Lib/_pyrepl/reader.py", line 371, in calc_screen
    prompt = self.get_prompt(ln, ll >= pos >= 0)
  File "/Users/guido/cpython/Lib/_pyrepl/historical_reader.py", line 326, in get_prompt
    return super().get_prompt(lineno, cursor_on_line)
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/guido/cpython/Lib/_pyrepl/reader.py", line 552, in get_prompt
    prompt = f"{ANSIColors.BOLD_MAGENTA}{prompt}{ANSIColors.RESET}"
                                        ^^^^^^^^
  File "<python-input-1>", line 2, in __str__
    def __str__(self): 1/0
                       ~^~
ZeroDivisionError: division by zero
~/cpython$ 

Probably @pablogsal should look into this.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-131110

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

使用報告中顯示的互動式命令重現該失敗,然後從 Lib/_pyrepl/reader.py 開始,沿著 traceback 中提到的相關 _pyrepl 檔案追蹤 get_prompt。在開始之前檢查連結的 PR gh-131110;當 str(sys.ps1) 引發的例外不再使 REPL 伴隨 traceback 結束時,即表示完成。

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

評估

技術堆疊
python
領域
cli
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
25/100

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

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