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

#include <Python.h>          /* standard API defs  */

main() {
    /* error checking omitted! */
    char *cstr;
    PyObject *pstr, *pmod, *pdict;
    Py_Initialize();

    /* result = string.upper('spam') + '!' */
    pmod  = PyImport_ImportModule("string"); /* namespace */
    pdict = PyModule_GetDict(pmod);
    pstr  = PyRun_String("upper('spam') + '!'", 
                            Py_eval_input, pdict, pdict);

    /* convert result to C */
    PyArg_Parse(pstr, "s", &cstr);
    printf("%s\n", cstr);
    Py_DECREF(pmod);
    Py_DECREF(pstr);    /* free exported objects */
}



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