Evaluate a python expression from C?

David Brady daves_spam_dodging_account at yahoo.com
Tue Nov 27 18:31:34 EST 2001


> -----Original Message-----
> From: Kragen Sitaker [mailto:kragen at pobox.com]
> Subject: Re: Evaluate a python expression from C?
> 
> There were several problems; that was one of them. 
> Here's an apparently-working program, in C:

Interesting.  It does not work on Win32.  However,
with your code as a starting help, I was able to "get
there from here."

For some reason, I don't know why, PyArg_ParseTuple()
does NOT work as expected; I tried adding and removing
parentheses around the arguments, and changing the "s"
to "(s)", all to no avail.  I know, I know... it would
help if I knew what I was doing.... :-)

Anyway.  I *did* get PyString_AsString() to work, but
it crashed whenever the expression did not evaluate to
a string.  I pored over the dox, and finally came up
with this strategy:

//  pObj = PyRun_String(data, Py_eval_input, globals,
locals)
//  PyArg_ParseTuple(pObj, "s", &buf); 
    pObj = PyObject_Repr(PyRun_String(data,
Py_eval_input, globals, locals));
    buf = PyString_AsString(pObj);

This appears to work for every input I've thrown at
the program so far (numeric and strings).

Thank you for your 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