小编Ale*_*lex的帖子

Python readline模块在导入期间打印转义字符

我在readlineFedora 17中使用带有Python 2.7.3 的模块.我对Ubuntu 12.10没有这个问题.

在此期间import readline,将显示转义字符.

$ python -c 'import readline' |less
ESC[?1034h(END)
Run Code Online (Sandbox Code Playgroud)

通常当我得到这样的意外输出时,我会使用stdout/stderr重定向到虚拟文件描述符来处理它(例如下面的例子).但这一次,这种方法不起作用.

import sys

class DummyOutput(object):
    def write(self, string):
        pass

class suppress_output(object):
    """Context suppressing stdout/stderr output.
    """
    def __init__(self):
        pass
    def __enter__(self):
        sys.stdout = DummyOutput()
        sys.stderr = DummyOutput()
    def __exit__(self, *_):
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__

if __name__ == '__main__':
    print 'Begin'
    with suppress_output():
        # Those two print statements have no effect
        # but *import readline* prints an escape char …
Run Code Online (Sandbox Code Playgroud)

python import stdout readline io-redirection

8
推荐指数
1
解决办法
2353
查看次数

标签 统计

import ×1

io-redirection ×1

python ×1

readline ×1

stdout ×1