Problem with C-API

Diez B. Roggisch deets at nospam.web.de
Fri Mar 17 12:33:34 EST 2006


John Dean wrote:

> Hi
> 
> I spent the whole of yesterday trying the get the following C code to
> execute
> 
> PyRun_String("def title();", Py_file_input, dict, dict);
                    ^^^^^^^^^^^

Try using a colon here....

> PyRun_String("\treturn 'Foo Bar'", Py_file_input, dict, dict);
> PyRun_String("x = title()", Py_file_input, dict, dict);
> PyObject * result = PyRun_String("print x", Py_file_input, dict, dict);
> printf( "The result is %s\n", PyObject_AsString( result );


Overall it seems to me that ther must be a better way to do such a thing -
even in C. AFAIK it supports multiline-stringliterals. Why don't you do
something like this:

script = "\
def foo():\n\
    return 'FooBar'\n\
x = title()\n\
print x\n"

And then read that script line by line, feeding it to the parser. Or maybe
the parser even groks that string at once - don't know that.


Diez



More information about the Python-list mailing list