File: class/Workbook/Examples/Lecture16/basic4.c

#include <Python.h>    

main() {
    int cval;
    PyObject *pdict, *pval;
    Py_Initialize();
   
    /* make a new namespace */
    pdict = PyDict_New(); 
    PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());

    PyDict_SetItemString(pdict, "Y", PyInt_FromLong(2));   /* dict['Y'] = 2   */
    PyRun_String("X = 99",  Py_file_input, pdict, pdict);  /* run statements  */
    PyRun_String("X = X+Y", Py_file_input, pdict, pdict);  /* same X and Y    */
    pval = PyDict_GetItemString(pdict, "X");               /* fetch dict['X'] */

    PyArg_Parse(pval, "i", &cval);                         /* convert to C */
    printf("%d\n", cval);                                  /* result=101 */
    Py_DECREF(pdict);
}



[Home page] Books Code Blog Python Author Train Find ©M.Lutz