包装 stdin/stdout 导致 IPython 失去自动完成和历史功能

Wrapping stdin/stdout causes IPython to lose auto completion and history features

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

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

我猜测包装器以某种方式阻止 IPython 识别用于向上、向下、向左和向右箭头的 ANSI 转义序列(ESC[#AESC[#BESC[#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.