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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • André

    #1

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

    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é

  • André

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

    Earlier this evening, I wrote:[color=blue]
    > 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.
    >[/color]
    [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\rurp le0.9.0.1\rur_p y\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.FixLineE nding(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.repla ce(' raw_input(', '
    my_raw_input(')
    .. user_code = user_code.repla ce('=raw_input( ',
    '=my_raw_input( ')
    .. raw_input_warni ng = '# # take care of raw_input # #'
    .. my_raw_input = """def my_raw_input(pr ompt=''):
    .. print '%s'+prompt\n"" "%raw_input_war ning
    .. 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_co de)
    .. f.close()
    ..
    .. #redirect stdout/sdterr
    .. self.parent.log .redirect()
    .. f_i, f_o = os.popen4("pyth on -u %s"%filename)
    .. output_lines = f_o.readlines()
    . length = len(output_line s)
    .. while length > 0:
    .. line = output_lines.po p(0)
    .. if line.find(raw_i nput_warning) != -1:
    .. stripped_line = line.replace(ra w_input_warning , '')
    .. dlg = wx.TextEntryDia log(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_line s)
    ..
    .. self.parent.log .redirect('rese t')
    =============== =============== ========

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

    Suggestions anyone?

    André

    Comment

    • André

      #3
      Solved: popen4("py thon -u script.py" ) redirected to a gui window

      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.
      [color=blue]
      > Earlier this evening, I wrote:[color=green]
      > > 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.[/color][/color]
      [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.FixLineE nding(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('rese t')

      .. def myRawInput(self , text):
      .. dlg = wx.TextEntryDia log(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.TextEntryDia log(self, text,
      .. 'Handling input()', '')
      .. if dlg.ShowModal() == wx.ID_OK:
      .. user_response = dlg.GetValue() + '\n'
      .. dlg.Destroy()
      .. return eval(user_respo nse)
      ===
      ..class LogWindow(wx.Te xtCtrl):
      .. def __init__ (self, parent):
      .. wx.TextCtrl.__i nit__(self, parent, -1,
      .. style=wx.TE_MUL TILINE|wx.TE_RE ADONLY)
      .. def redirect(self, option=''):
      .. if option == "reset":
      .. sys.stdout = sys.__stdout__
      .. sys.stderr = sys.__stderr__
      .. else:
      .. sys.stdout = self
      .. sys.stderr = self

      Comment

      Working...