*Almost working* >:-( popen4("python -u script.py") redirected to a gui window

André andre.roberge at gmail.com
Fri Jan 27 23:48:30 EST 2006


Earlier this evening, I wrote:
> Short description: Using a wxPython based app, I would like to take a
> python script in an editor window, invoke the python interpreter and
> use another window as stdin/stdout/stderr.
>
[snip]
Ok, I got almost everything working ... except the stdin redirection.
I'm stuck with simple (!?) IOError that I don't understand.

[leading "." added to each line below to preserve indentation]
===Simple script that I try to execute===
.def hello():
.    print "Hello world!"
.
.hello()
.test = raw_input("Give me a command: ")
.hello()
.print test

===Sample output====
Hello world!
Traceback (most recent call last):
  File "E:\Python\rurple0.9.0.1\rur_py\editor.py", line 303, in
RunProgram
    f_i.write(user_response)
IOError: [Errno 22] Invalid argument

===method that executes the script===
.    def RunProgram(self, event):
.        user_code = self.GetText()
.        user_code = parser.FixLineEnding(user_code)
.
.        # redefine raw_input() if present so we can call a dialog
.        if user_code.find(' raw_input(') != -1 or \
.           user_code.find('=raw_input(') != -1:
.            user_code = user_code.replace(' raw_input(', '
my_raw_input(')
.            user_code = user_code.replace('=raw_input(',
'=my_raw_input(')
.            raw_input_warning = '# # take care of raw_input # #'
.            my_raw_input = """def my_raw_input(prompt=''):
.                print '%s'+prompt\n"""%raw_input_warning
.            user_code = my_raw_input + user_code
.
.        # create temporary file to store the code
.        filename = '_last_run.py'
.        f = open(filename, 'w')
.        f.write(user_code)
.        f.close()
.
.        #redirect stdout/sdterr
.        self.parent.log.redirect()
.        f_i, f_o = os.popen4("python -u %s"%filename)
.        output_lines = f_o.readlines()
 .       length = len(output_lines)
.        while length > 0:
.            line = output_lines.pop(0)
.            if line.find(raw_input_warning) != -1:
.                stripped_line = line.replace(raw_input_warning, '')
.                dlg = wx.TextEntryDialog(self, stripped_line[:-1],
.                                         'Handling raw_input()', '')
.                if dlg.ShowModal() == wx.ID_OK:
.                    user_response = dlg.GetValue() #+ '\n'
.                dlg.Destroy()
.                f_i.write(user_response)
.            else:
.                print line,
.            output_lines += f_o.readlines()
.            length = len(output_lines)
.
.        self.parent.log.redirect('reset')
======================================

The wx.TextEntryDialog gets called properly.
After I enter the text in it ("user_response"),
I get the quoted error message.

Suggestions anyone?

André




More information about the Python-list mailing list