First page Back Continue Last page Overview Graphics

Reading int, string and float

#include <python2.6/Python.h>

main( ) {

PyObject * module;

PyObject * dict;

PyObject * obj;

long lval;

float x;

char* str;

Py_Initialize( );

PyRun_SimpleString("abc = 2*3; xyz = 34/7.0; name=\"Ede\"");

module = PyImport_AddModule("__main__");

dict = PyModule_GetDict(module);

obj = PyMapping_GetItemString(dict, "abc");

lval = (obj != NULL) ? PyInt_AsLong(obj) : 0;

obj = PyMapping_GetItemString(dict, "name");

str = (obj != NULL) ? PyString_AsString(obj) : "undefined";

obj = PyMapping_GetItemString(dict, "xyz");

x = (obj != NULL) ? PyFloat_AsDouble(obj) : 0;

printf("1st: %d, 2nd: %s, 3rd: %f\n", (int) lval, str, x);

}