python / python/cpython

Preexec hook for REPL

未關閉
#122,622 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

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

描述

Feature or enhancement

Proposal:

Background

In VS Code we override PS1 to enable shell integration, this currently gives VS Code understanding of where the commands are and what they are. This provides a few features:

Command decorations with visualization of exit/error status:

image

Command navigation (cmd/ctrl+up/down):

image

Run recent (ctrl+alt+r):

image

To give an example of what's possible with shell integration, here's our experimental VS Code-native PowerShell intellisense:

image

And multi-line sticky scroll:

image

Request

Something we needed to work around for Python was the lack of a pre-execution hook, we would normally print \x1b[633;C\a at the end of a command just before it's run such that VS Code knows where that marker is. Currently we have to stick that at the start of the command input though which can degrade the experience, especially when VS Code attempts to figure out that command has been run manually.

To visualization of the problem, see the A, B, C, D markers in our python REPL:

image

Compare this to PowerShell which positions them correctly:

image

Shell integration script included in VS Code's python extension: https://github.com/microsoft/vscode-python/blob/main/python_files/pythonrc.py

I'm not a Python dev so I'm not sure what such an API would look like, but we essentially need a hook to run arbitrary code immediately before the REPL runs the command, something like this:

class MyPreexec:
    def __str__(self):
      return "preexec"

sys.preexec = MyPreexec()

cc @anthonykim1 @brettcannon

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-144724
  • gh-157241

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

先檢視所連結的 VS Code Python extension 中 python_files/pythonrc.py 的 Python REPL 執行路徑與 shell integration 範例。閱讀所連結的 PR gh-144724 與 gh-157241,以了解現有工作與相關決策。當 REPL integrations 可使用有文件說明的 pre-execution hook,且其行為已由相關測試涵蓋時,即視為完成。

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

評估

技術堆疊
python
領域
cli
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
需要釐清
新手友好度
25/100

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

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