gpt4 book ai didi

python - 包装标准输入/标准输出导致 IPython 失去自动完成和历史功能

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:34 26 4
gpt4 key购买 nike

我正在编写一个脚本,该脚本使用 IPython 的嵌入式脱壳功能,并且作为一项要求,它必须将来自 stdin/stdout 的所有数据记录到一个文件中。出于这个原因,我决定为他们编写包装器;然而,在关闭流后,我的嵌入式 IPython shell 失去了它的自动完成和历史记录功能,当我按下箭头按钮时输出如下内容:

In [1]: ^[[A^[[B^[[A^[[C...

我猜测包装器以某种方式阻止了 IPython 识别用于向上、向下、向左和向右箭头的 ANSI 转义序列(ESC[#AESC[# B, ESC[#C, ESC[#D].

这是演示我的问题的代码:

import sys
from time import strftime
import IPython

# Custom IO class for file logging
class StdinLogger (object):
def __init__(self, wrapped, file):
# double-underscore everything to prevent clashes with names of
# attributes on the wrapped stream object.
self.__wrapped = wrapped
self.__file = file

def __getattr__(self, name):
return getattr(self.__wrapped, name)

def readline(self):
str = self.__wrapped.readline()
self.__file.write(str)
self.__file.flush()
return str


# Custom IO class for file logging
class StdoutLogger (object):
def __init__(self, wrapped, file):
# double-underscore everything to prevent clashes with names of
# attributes on the wrapped stream object.
self.__wrapped = wrapped
self.__file = file

def __getattr__(self, item):
return getattr(self.__wrapped, item)

def write(self, str):
self.__file.write(str)
self.__file.flush()
self.__wrapped.write(str)
self.__wrapped.flush()


f = open("LOG-" + strftime("%Y-%m-%d-%H-%M-%S") + ".txt", 'w')
# Initialize the file logger
sys.stdin = StdinLogger(sys.stdin, f)
sys.stdout = StdoutLogger(sys.stdout, f)


# Embed IPython shell
IPython.embed(banner1="", banner2="")

关于如何解决这个问题有什么想法吗?

提前致谢。

最佳答案

@Carreau(IPython 核心开发人员)在 Github 对这个问题的回应:

The issue is that with prompt toolkit stdout in particular is more than a stream, as you literally draw/erase/redraw on the screen, and as it uses an eventloop and async completions, you cannot consider the stdin/out/err to be enough to know the current state.

So you will (likely) need to hook into the prompt_toolkit and overwrite the vt100-input/output and/or the windows ones.

We most-likely will need extra hooks to set that up in IPython at startup.

关于python - 包装标准输入/标准输出导致 IPython 失去自动完成和历史功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38255591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com