embedded python question

Chris Liechti cliechti at gmx.net
Fri Jul 26 06:50:48 EDT 2002


"Keith S." <keith_s at ntlworld.nospam.com> wrote in
news:3D412292.1030608 at ntlworld.nospam.com: 

> Chris Liechti wrote:
> 
>> this class isn't what you want... you need to supply a callable
>> either name that method __call__ and add a "return" of the result, or
>> much simpler, leave it away and use your "input" directly below.
> 
> OK, so now I do:
> 
> PyRun_SimpleString("interact('---Starting Python---', input)\n");
> 
> where my input function looks like:
> 
> static PyObject * input(PyObject *self, PyObject *args)
> {
>      char *string;
>      if (!PyArg_ParseTuple(args, "s", &string))
>           return 0;
>      gld::gMsgView()->printf(MSG_NONE, "%s", string);
>      return PyString_FromString("");
> }
> 
> (I'm returning an empty string just to test the above). This goes
> into an infinite loop - I'd have expected it to output the prompt
> '>>>' once, then wait for a non-null string.

wrong expectation ;-) it reads from the input until it gets something 
usable. it should print the prompt once indeed, but then its calling your 
input over and over as you return only "". code.interact expects that you 
block until input s available.
(you can try that out at the interactive prompt too, playing with 
code.interact)

> Any ideas what's wroong? I've looked at the implementation of
> builtin_raw_input() and can't find any clues to help.

just return some real data, "print 'hello'\n" or so.
and i think you can exit the interactive loop by raising an EOFError 
exception.

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list