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
    Py_Initialize();

    // 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
instead?

Thanks for any ideas or help.

-dB


=====
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.
http://geocities.yahoo.com/ps/info1




More information about the Python-list mailing list