By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

Howto? popen4("python -u script.py") redirected to a gui window

P: n/a
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.

Based on what I've read so far, I've figured that I need to do
something like:
f_in, f_out = popen4("python -u script.py")
where I have extracted the text from the editor and put it in
"script.py". (However, see below).

What I don't understand is how to link f_in, f_out somehow to a
different window which can be used to interact with the user.

I need a solution that will work on *nix, Windows and MacOS.

Although I am not familar with Tkinter, I most likely could figure out
what to do from an example that would use it.

Wish#1: It would be nice not to have to create the intermediate file
"script.py" and pass directly the content of the editor window to
popen4(). I know about the "-c" option for Python, but I can't see
how it would work if the script contains quotes or double quotes.

Wish#2: It would be *really nice* if, somehow, request for input [which
should arise only from raw_input() ... and, perhaps input()] by Python
could be redirected to an appropriate wxPython dialog instead of just a
window. I might be able to figure that one on my own, if I can get
enough information to get the basic redirection to a window working.

André

Jan 28 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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é

Jan 28 '06 #2

P: n/a
Sorry about the single-author thread; I have been googling for answers
and trying out for 3 nights... I just want to record to save others the
trouble.
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]

I used "exec" instead of popen and solved it. I had tried originally
with exec but then read that exec could not handle "raw_input", so I
gave up too quickly.

Here are the main parts of the solution.
===============
.. def RunProgram(self, event):
.. user_code = self.GetText()
.. user_code = parser.FixLineEnding(user_code)
.. #redirect stdout/sdterr by redefining them
.. self.parent.log.redirect()
.. myGlobals = globals()
.. myGlobals['raw_input'] = self.myRawInput
.. myGlobals['input'] = self.myInput
.. exec user_code in myGlobals
.. self.parent.log.redirect('reset')

.. def myRawInput(self, text):
.. dlg = wx.TextEntryDialog(self, text,
.. 'Handling raw_input()', '')
.. if dlg.ShowModal() == wx.ID_OK:
.. user_response = dlg.GetValue() + '\n'
.. dlg.Destroy()
.. return user_response
..
.. def myInput(self, text):
.. dlg = wx.TextEntryDialog(self, text,
.. 'Handling input()', '')
.. if dlg.ShowModal() == wx.ID_OK:
.. user_response = dlg.GetValue() + '\n'
.. dlg.Destroy()
.. return eval(user_response)
===
..class LogWindow(wx.TextCtrl):
.. def __init__ (self, parent):
.. wx.TextCtrl.__init__(self, parent, -1,
.. style=wx.TE_MULTILINE|wx.TE_READONLY)
.. def redirect(self, option=''):
.. if option == "reset":
.. sys.stdout = sys.__stdout__
.. sys.stderr = sys.__stderr__
.. else:
.. sys.stdout = self
.. sys.stderr = self

Jan 28 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.