繁体   English   中英

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

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

我正在开发一个使用IPython的嵌入式脱壳功能的脚本,根据要求,它必须将所有数据从stdin / stdout记录到文件中。 因此,我决定为它们编写包装器。 但是,在关闭流后,我的嵌入式IPython Shell失去了自动完成和历史记录功能,当我按下箭头按钮时会输出如下内容:

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

我猜想包装器以某种方式阻止了IPython识别用于UP,DOWN,LEFT和RIGHT箭头( ESC[#A ESC[#BESC[#C ESC[#BESC[#CESC[#D )的ANSI转义序列。

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

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上对这个问题的回应:

问题在于,特别是在提示工具箱中,stdout不仅仅是一个流,因为在屏幕上按字面意义绘制/擦除/重新绘制,并且由于它使用了eventloop和异步补全,因此您不能认为stdin / out / err就足够了知道当前状态。

因此,您(可能)需要挂接到hint_toolkit并覆盖vt100-input / output和/或Windows的。

我们很可能需要额外的挂钩来在启动时在IPython中进行设置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM