简体   繁体   English

Python 诅咒 ERR 管道

[英]Python curses ERR pipeline

I would like to reproduce the result of a curses python script when a key is pressed for a pipeline.当按下管道键时,我想重现 curses python 脚本的结果。

Let's consider the following python script:让我们考虑以下 python 脚本:

import curses

class Sf:
    def __init__(self):

        self.window = curses.initscr()
        curses.start_color()
        curses.use_default_colors()
        curses.noecho()
        curses.curs_set(False)
        self.window.keypad(True)
        self.window.addstr(0, 1, "Test")
        while True:
            key = self.window.getch()
            if key == ord("q"):
                self.window.keypad(False)
                curses.nocbreak()
                curses.echo()
                curses.endwin()
                break

sf = Sf()

To inject the key press I am using subprocess.run :要注入按键,我正在使用subprocess.run

>>> from subprocess import run
>>> run(["python", "example.py"], input="q", encoding="ASCII", capture_output=True, env={"TERM": "xterm-256color"})
CompletedProcess(args=['python', 'example.py'], returncode=1, stdout='\x1b[?1049h\x1b[22;0;0t\x1b[1;24r\x1b(B\x1b[m\x1b[4l\x1b[?7h\x1b[39;49m\x1b[?25l\x1b[?1h\x1b=\x1b[39;49m\x1b(B\x1b[m\x1b[H\x1b[2J Test\x1b[?1l\x1b>', stderr='Traceback (most recent call last):\n  File "example.py", line 26, in <module>\n    sf = Sf()\n  File "example.py", line 21, in __init__\n    curses.nocbreak()\n_curses.error: nocbreak() returned ERR\n'

Any idea about the curses error?关于诅咒错误的任何想法?

Thanks谢谢

curses reads from terminals; curses 从终端读取; a pipe is not a tty. pipe 不是 tty。

The manual page for nocbreak says nocbreak的手册页说

Normally, the tty driver buffers typed characters until a newline or carriage return is typed.通常, tty驱动程序会缓冲输入的字符,直到输入换行符或回车符。 The cbreak routine disables line buffering and erase/kill character-processing (interrupt and flow control characters are unaffected), making characters typed by the user immediately available to the program. cbreak例程禁用行缓冲和擦除/终止字符处理(中断和流控制字符不受影响),使用户键入的字符立即可供程序使用。 The nocbreak routine returns the terminal to normal (cooked) mode. nocbreak例程将终端返回到正常(熟)模式。

The error message refers to the code returned by nocbreak , which happens because the mode-change doesn't work for pipes.错误消息指的是nocbreak返回的代码,这是因为模式更改不适用于管道。 So ncurses returns ERR .所以 ncurses 返回ERR

Pexpect is what I was looking for. Pexpect 是我一直在寻找的。

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

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