如何将Tab键发送到python子进程的stdin [英] how to send tab-key to python subprocess's stdin

查看:113
本文介绍了如何将Tab键发送到python子进程的stdin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我有一个Python子进程,该子进程连接到类似shell的应用程序,该应用程序使用readline库处理输入,并且该应用程序具有TAB完整的例程来执行命令输入,就像重击.生成子进程,如下所示:

Background: I have a Python subprocess that connects to a shell-like application, which uses the readline library to handle input, and that app has a TAB-complete routine for command input, just like bash. The child process is spawned, like so:

def get_cli_subprocess_handle():
    return subprocess.Popen(
                            '/bin/myshell',
                            shell=False,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT,
                            )

一切都很好,除了制表符完整.每当我的Python程序将制表符'\t'传递给子进程时,我在STDIN中获得5个空格,而不是触发readline库的制表完成例程. :(

Everything works great, except tab-complete. Whenever my Python program passes the tab character, '\t' to the subprocess, I get 5 spaces in the STDIN, instead of triggering the readline library's tab-complete routine. :(

问题:我可以发送什么到子流程的STDIN来触发孩子的制表符完成功能?可能会问另一种方式:如何发送TAB 密钥 ,而不是TAB 字符 甚至有可能吗?

Question: What can I send to the subprocess's STDIN to trigger the child's tab-complete function? Maybe asked another way: How do I send the TAB key as opposed to the TAB character, if that is even possible?

相关但未答复且出轨:

围绕python批处理过程的触发选项卡完成readline

推荐答案

类似于shell的应用程序可能会区分连接到stdin的终端和连接到stdin的管道.许多Unix实用程序这样做只是为了优化其缓冲(行与块),而类似shell的实用程序可能会禁用批输入(即PIPE)上的命令完成功能,以避免意外的结果.命令完成实际上是一种交互式功能,需要终端输入.

The shell like application is probably differentiating between a terminal being connected to stdin and a pipe being connected to it. Many Unix utilities do just that to optimise their buffering (line vs. block) and shell-like utilities are likely to disable command completion facilities on batch input (i.e. PIPE) to avoid unexpected results. Command completion is really an interactive feature which requires a terminal input.

检出pty模块,并尝试使用主/从对作为子流程的管道.

Check out the pty module and try using a master/slave pair as the pipe for your subprocess.

这篇关于如何将Tab键发送到python子进程的stdin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆