stdin in embedded python

Discussion in 'Python' started by KillSwitch, Nov 1, 2009.

  1. KillSwitch

    KillSwitch Guest

    I have a C++ program, with a GUI, into which I have embedded python. I
    have made several python functions in C++, one of which I use to
    override the normal stdout and stderr so that they print to a text box
    of my GUI. One thing I cannot think of how to do is to redefine stdin
    so that it pauses the program, waits for a user to type input into the
    box, hit enter, and takes input from another text element and sends it
    to python like it was the console.

    I wonder if anyone could help me in trying to do such a thing. To
    simplify, the new stdin should wait for the C++ function to give it a
    value, like it waits for the console.
    KillSwitch, Nov 1, 2009
    #1
    1. Advertising

  2. KillSwitch

    Dave Angel Guest

    KillSwitch wrote:
    > I have a C++ program, with a GUI, into which I have embedded python. I
    > have made several python functions in C++, one of which I use to
    > override the normal stdout and stderr so that they print to a text box
    > of my GUI. One thing I cannot think of how to do is to redefine stdin
    > so that it pauses the program, waits for a user to type input into the
    > box, hit enter, and takes input from another text element and sends it
    > to python like it was the console.
    >
    > I wonder if anyone could help me in trying to do such a thing. To
    > simplify, the new stdin should wait for the C++ function to give it a
    > value, like it waits for the console.
    >
    >

    I suspect you don't really want to redirect stdin, but instead implement
    raw_input(). If you have control over the script, just change it from
    raw_input() to cpp_raw_input(). But if you need to be able to run
    arbitrary scripts, ...

    (untried) - Try changing __builtins__.raw_input to reference your new
    function.

    DaveA
    Dave Angel, Nov 1, 2009
    #2
    1. Advertising

  3. KillSwitch

    KillSwitch Guest

    On Nov 1, 5:34 am, Dave Angel <> wrote:
    > KillSwitch wrote:
    > > I have a C++ program, with a GUI, into which I have embedded python. I
    > > have made several python functions in C++, one of which I use to
    > > override the normal stdout and stderr so that they print to a text box
    > > of my GUI. One thing I cannot think of how to do is to redefine stdin
    > > so that it pauses the program, waits for a user to type input into the
    > > box, hit enter, and takes input from another text element and sends it
    > > to python like it was the console.

    >
    > > I wonder if anyone could help me in trying to do such a thing. To
    > > simplify, the new stdin should wait for the C++ function to give it a
    > > value, like it waits for the console.

    >
    > I suspect you don't really want to redirect stdin, but instead implement
    > raw_input().  If you have control over the script, just change it from
    > raw_input() to cpp_raw_input().  But if  you need to be able to run
    > arbitrary scripts, ...
    >
    > (untried) - Try changing __builtins__.raw_input  to reference your new
    > function.
    >
    > DaveA


    But what would the function do? How would it pause python and wait for
    it to have text to send?
    KillSwitch, Nov 1, 2009
    #3
  4. KillSwitch

    Dave Angel Guest

    Gabriel Genellina wrote:
    > En Sun, 01 Nov 2009 13:34:44 -0300, KillSwitch
    > <> escribió:
    >> On Nov 1, 5:34 am, Dave Angel <> wrote:
    >>> KillSwitch wrote:

    >
    >>> > I have a C++ program, with a GUI, into which I have embedded
    >>> python. I
    >>> > have made several python functions in C++, one of which I use to
    >>> > override the normal stdout and stderr so that they print to a text
    >>> box
    >>> > of my GUI. One thing I cannot think of how to do is to redefine stdin
    >>> > so that it pauses the program, waits for a user to type input into
    >>> the
    >>> > box, hit enter, and takes input from another text element and
    >>> sends it
    >>> > to python like it was the console.
    >>>
    >>> I suspect you don't really want to redirect stdin, but instead
    >>> implement
    >>> raw_input(). [...]Try changing __builtins__.raw_input to reference
    >>> your new
    >>> function.

    >>
    >> But what would the function do? How would it pause python and wait for
    >> it to have text to send?

    >
    > Whatever you want. You don't have to "pause python", Python itself
    > won't resume until your function doesn't return. (You should release
    > the GIL if your C++ function doesn't call back to Python code, to
    > allow other threads to continue, but that's another story).
    > This is a raw_input replacement written in Tkinter; it shows a dialog
    > box instead of reading from stdin:
    >
    > py> from Tkinter import *
    > py> from tkSimpleDialog import askstring
    > py> def my_raw_input(prompt):
    > ... return askstring("Python", prompt)
    > ...
    > py> root = Tk()
    > py> import __builtin__
    > py> __builtin__.raw_input = my_raw_input
    > py>
    > py> raw_input("What's your name?")
    > 'Gabriel'
    >
    >
    > ------------------------------------------------------------------------
    >

    I think I see the OP's problem. He has written a GUI program in C++,
    and is using (embedding) Python functions into it. So presumably those
    functions are being called from events in the C++ event loop.

    If one of those functions tries to call back into C++ code, the event
    loop will never get control, to process the events from the standard UI
    controls.

    So if the input is to be handled as an integral part of the C++ UI,
    there's a distinct problem.

    On the other hand, Gabriel's dialog box should work fine, as long as you
    don' t mind a modal dialog box as a solution. I don't know tkinter's
    askstring, but I suspect it'd work. However, the rest of the C++ GUI
    would be frozen, which could be a problem.

    DaveA
    Dave Angel, Nov 2, 2009
    #4
  5. En Sun, 01 Nov 2009 23:13:10 -0300, Dave Angel <> escribió:
    > Gabriel Genellina wrote:
    >> En Sun, 01 Nov 2009 13:34:44 -0300, KillSwitch
    >> <> escribió:
    >>> On Nov 1, 5:34 am, Dave Angel <> wrote:
    >>>> KillSwitch wrote:

    >>
    >>>> > I have a C++ program, with a GUI, into which I have embedded
    >>>> python. I
    >>>> > have made several python functions in C++, one of which I use to
    >>>> > override the normal stdout and stderr so that they print to a text
    >>>> box
    >>>> > of my GUI. One thing I cannot think of how to do is to redefine
    >>>> stdin
    >>>> > so that it pauses the program, waits for a user to type input into
    >>>> the
    >>>> > box, hit enter, and takes input from another text element and sends
    >>>> it
    >>>> > to python like it was the console.
    >>>>
    >>>> I suspect you don't really want to redirect stdin, but instead
    >>>> implement
    >>>> raw_input().
    >>>
    >>> But what would the function do? How would it pause python and wait for
    >>> it to have text to send?

    >>
    >> Whatever you want. You don't have to "pause python", Python itself
    >> won't resume until your function doesn't return. [example using
    >> Tkinter.askstring]
    >>

    > I think I see the OP's problem. He has written a GUI program in C++,
    > and is using (embedding) Python functions into it. So presumably those
    > functions are being called from events in the C++ event loop.
    >
    > If one of those functions tries to call back into C++ code, the event
    > loop will never get control, to process the events from the standard UI
    > controls.
    >
    > So if the input is to be handled as an integral part of the C++ UI,
    > there's a distinct problem.
    >
    > On the other hand, Gabriel's dialog box should work fine, as long as you
    > don' t mind a modal dialog box as a solution. I don't know tkinter's
    > askstring, but I suspect it'd work. However, the rest of the C++ GUI
    > would be frozen, which could be a problem.


    Perhaps looking a other examples may help. Both IDLE and PythonWin replace
    raw_input with a message box; IDLE is a Tkinter application, and PythonWin
    wraps MFC. Both have a main message loop and use a modal message box.

    --
    Gabriel Genellina
    Gabriel Genellina, Nov 2, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    24,951
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    785
    Dan Pop
    Jun 21, 2004
  3. Ben
    Replies:
    2
    Views:
    1,342
    jacob navia
    Aug 29, 2009
  4. Terry Cooper
    Replies:
    7
    Views:
    424
    Janos Sebok
    Jun 9, 2009
  5. Stefano Sabatini
    Replies:
    6
    Views:
    291
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page