Python StringIO—有选择地将数据放入stdin

Python StringIO—有选择地将数据放入stdin,python,stdin,stringio,Python,Stdin,Stringio,我们使用的是一些编译过的python代码,但我们没有源代码。代码提示用户输入,我们正在尝试自动完成这一部分 基本上是询问用户名、密码,然后根据特定情况问一些不同的问题。我不知道编译后的函数是使用原始输入、输入还是其他什么 我已经能够使用StringIO替换带有用户名和密码的stdin,我可以用我自己的类替换stdout,并找出将出现的提示,但是当我根据从stdout读取的内容有选择地将数据放入stdin时,我被难住了 import sys import re from StringIO impo

我们使用的是一些编译过的python代码,但我们没有源代码。代码提示用户输入,我们正在尝试自动完成这一部分

基本上是询问用户名、密码,然后根据特定情况问一些不同的问题。我不知道编译后的函数是使用原始输入、输入还是其他什么

我已经能够使用StringIO替换带有用户名和密码的stdin,我可以用我自己的类替换stdout,并找出将出现的提示,但是当我根据从stdout读取的内容有选择地将数据放入stdin时,我被难住了

import sys
import re
from StringIO import StringIO

def test():
    overwrite = raw_input("The file exists, overwrite? ")
    notify = raw_input("This file is marked for notifies.  Notify?")
    sys.stdout.write("Overwrite: %s, Notify: %s" % (overwrite,notify))

class Catcher(object):
    def __init__(self):
        pass

    def write(self, msg):
        if re.search("The file exists, overwrite", msg):
            # put data into stdin 
        if re.search("The file is marked for notification", msg):
            # put data into stdin

sys.stdout = Catcher()
test()
我不能只预加载一个StringIO对象,因为问题可能会因环境而异,但我需要自动输入stdin,因为他们正试图将其放入一个自动构建系统中,所以他们将通过命令行提供默认值来回答出现的任何问题

如果在调用编译后的函数之前将stdin设置为一个空的StringIO对象,那么它只会在EOF中出错——不知道如何让它等待输入

大概是这样的:

import sys
import re
from StringIO import StringIO

def test():
    overwrite = raw_input("The file exists, overwrite? ")
    notify = raw_input("This file is marked for notifies.  Notify?")
    sys.__stdout__.write("Overwrite: %s, Notify: %s" % (overwrite,notify))

class Catcher(object):
    def __init__(self, stdin):
        self.stdin = stdin

    def write(self, msg):
        if re.search("The file exists, overwrite", msg):
            self.stdin.write('yes\n')
        if re.search("The file is marked for notification", msg):
            self.stdin.write('no\n')

sys.stdin = StringIO()
sys.stdout = Catcher(sys.stdin)
test()
产生:

Traceback (most recent call last):
  File "./teststdin.py", line 25, in <module>
    test()
  File "./teststdin.py", line 8, in test
    overwrite = raw_input("The file exists, overwrite? ")
EOFError: EOF when reading a line
回溯(最近一次呼叫最后一次):
文件“/teststdin.py”,第25行,在
测试()
文件“/teststdin.py”,测试中的第8行
覆盖=原始输入(“文件存在,覆盖吗?”)
EOF:读取一行时的EOF

有什么想法吗?

这里有一个完全避免
StringIO
的解决方案:

import sys
import re

class Catcher(object):
    def __init__(self, handler):
        self.handler = handler
        self.inputs = []

    def __enter__(self):
        self.__stdin  = sys.stdin
        self.__stdout = sys.stdout
        sys.stdin = self
        sys.stdout = self

    def __exit__(self, type, value, traceback):
        sys.stdin  = self.__stdin
        sys.stdout = self.__stdout

    def write(self, value):
        result = self.handler(value)
        if result:
            self.inputs = [result] + self.inputs

    def readline(self):
        return self.inputs.pop()
用作:

def test():
    overwrite = raw_input("The file exists, overwrite? ")
    notify = raw_input("This file is marked for notifies.  Notify?")
    sys.__stdout__.write("Overwrite: %s, Notify: %s" % (overwrite,notify))


@Catcher
def exist_notify(msg):
    if re.search("The file exists, overwrite", msg):
        return 'yes'
    if re.search("This file is marked for notifies", msg):
        return 'no'

with exist_notify:
    test()

下面是一个完全避免
StringIO
的解决方案:

import sys
import re

class Catcher(object):
    def __init__(self, handler):
        self.handler = handler
        self.inputs = []

    def __enter__(self):
        self.__stdin  = sys.stdin
        self.__stdout = sys.stdout
        sys.stdin = self
        sys.stdout = self

    def __exit__(self, type, value, traceback):
        sys.stdin  = self.__stdin
        sys.stdout = self.__stdout

    def write(self, value):
        result = self.handler(value)
        if result:
            self.inputs = [result] + self.inputs

    def readline(self):
        return self.inputs.pop()
用作:

def test():
    overwrite = raw_input("The file exists, overwrite? ")
    notify = raw_input("This file is marked for notifies.  Notify?")
    sys.__stdout__.write("Overwrite: %s, Notify: %s" % (overwrite,notify))


@Catcher
def exist_notify(msg):
    if re.search("The file exists, overwrite", msg):
        return 'yes'
    if re.search("This file is marked for notifies", msg):
        return 'no'

with exist_notify:
    test()

如果要从刚刚写入的
StringIO
读取,必须先将其倒回开始写入的位置
另外,您的第二次搜索测试了错误的字符串

这应该起作用:

import sys
import re
from StringIO import StringIO

def test():
    overwrite = raw_input("The file exists, overwrite? ")
    notify = raw_input("This file is marked for notifies.  Notify?")
    sys.__stdout__.write("Overwrite: %s, Notify: %s" % (overwrite,notify))

class Catcher(object):
    def __init__(self, stdin):
        self.stdin = stdin

    def write(self, msg):
        if re.search("The file exists, overwrite?", msg):
            self.stdin.truncate(0)
            self.stdin.write('yes\n')
            self.stdin.seek(0)
        if re.search("This file is marked for notifies.  Notify?", msg):
            self.stdin.truncate(0)
            self.stdin.write('no\n')
            self.stdin.seek(0)

sys.stdin = StringIO()
sys.stdout = Catcher(sys.stdin)
test()

如果要从刚刚写入的
StringIO
读取,必须先将其倒回开始写入的位置
另外,您的第二次搜索测试了错误的字符串

这应该起作用:

import sys
import re
from StringIO import StringIO

def test():
    overwrite = raw_input("The file exists, overwrite? ")
    notify = raw_input("This file is marked for notifies.  Notify?")
    sys.__stdout__.write("Overwrite: %s, Notify: %s" % (overwrite,notify))

class Catcher(object):
    def __init__(self, stdin):
        self.stdin = stdin

    def write(self, msg):
        if re.search("The file exists, overwrite?", msg):
            self.stdin.truncate(0)
            self.stdin.write('yes\n')
            self.stdin.seek(0)
        if re.search("This file is marked for notifies.  Notify?", msg):
            self.stdin.truncate(0)
            self.stdin.write('no\n')
            self.stdin.seek(0)

sys.stdin = StringIO()
sys.stdout = Catcher(sys.stdin)
test()

更简单的测试用例:
sys.stdin=StringIO();sys.stdin.write(“Hello world\n”);打印原始输入();sys.stdin.write(“Hello world\n”);打印原始输入()
谢谢Mata。当我尝试在StringIO上执行stdin.write时,我刚刚开始计算指针被设置到行尾,并且不得不将其设置回原来的位置-截断非常有用!谢谢你,玛塔。当我尝试在StringIO上执行stdin.write时,我刚刚开始计算指针被设置到行尾,并且不得不将其设置回原来的位置-截断非常有用!