Evaluate a python expression from C?

David Brady daves_spam_dodging_account at yahoo.com
Tue Nov 27 21:33:01 CET 2001

Hi all,

I would like to embed Python in my application, and
one of the methods of access I'd like to have is the
ability to send an expression to Python as a string,
and get back a string containing the result.

For example, to be able to say "(2+5)*3" and get "21"
as the result.

I have tried using PyRun_String() but I get an access
violation.  My code looks like this:

void PyEval(char *data)
    // data is a shared char[900] buffer;
    // size checking is left out for clarity here

    // The following line access violates.
    PyObject *pObj = PyRun_String(data,0,NULL,NULL);

    char *buf;
    PyArg_ParseTuple( pObj, "s", &buf);
    strcpy( data, buf);

Could the problem be the NULL args I pass in?  For
simple evaluation, what should I populate them with

Thanks for any ideas or help.


David Brady
daves_spam_dodging_account at yahoo.com
I'm feeling very surreal today... or *AM* I?

Do You Yahoo!?
Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.

More information about the Python-list mailing list