embedded python question
Chris Liechti
cliechti at gmx.net
Wed Jul 24 16:30:22 EDT 2002
"Keith S." <keith_s at ntlworld.nospam.com> wrote in
news:3D3EFC18.30705 at ntlworld.nospam.com:
> Just wrote:
>
>> Have a look at code.py in the std library.
>
> Hmm, that looks interesting...
>
> So in my C code I've something like:
>
> char *code = "class Sout:\n"
> " def write(self, s):\n"
> " output(s)\n"
> "\n"
> "class Sin:\n"
> " def __init__(self, prompt):\n"
> " input(prompt)\n"
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.
> "\n"
> "import sys\n"
> "from code import *\n"
> "from gld import *\n"
you're aware of side effects when using import "*"?
> "sys.stdout = Sout()\n"
> "sys.stdin = None\n"
> "interact('---Starting Python---, Sin)\n"
if you pass a callable class it must be an instance, not a class object.
but as said above, just use your "input" here.
> PyRun_SimpleString(code);
>
> The input() C function returns Py_BuildValue("s", cmd ), where cmd
> is the cmd typed in the cmd line.
>
> However this does not work... the output is something like:
>
> ---Starting Python---
> >>>
> Traceback (most recent call last):
> File "<string>", line 15, in ?
> File "C:\Python22\lib\code.py", line 307, in interact
>
> console.interact(banner)
> File "C:\Python22\lib\code.py", line 244, in interact
>
> more = self.push(line)
> File "C:\Python22\lib\code.py", line 265, in push
>
> source = "\n".join(self.buffer)
> TypeError
>:
> sequence item 0: expected string, instance found
>
>
> Any clues on what's wrong here?
>
> - Keith
>
>
chris
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list