Retrieve a variable from an embedded interpreter

Mike Johnson afp_randjohnson at yahoo.com
Sun Feb 24 07:19:02 EST 2002


Talking to myself here. :-)

So I've continued searching the web. (Searching for ".py" is the only
way to go. :-)

Anyhow, I've been playing with the Py_RunString
function. Unfortunately, I now get segfaults. But I think I'm getting
closer...

I was following what this guy did:

http://mail.python.org/pipermail/python-list/1999-November/015502.html

stdout + stderr:

a=hi Assertion failed Aborted

---

code:

---

void cb_script_execute ( void ) {
        PyObject *a;
        PyObject *pdict;
        char *sa;

        Py_Initialize ();

        pdict = PyDict_New();
        PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());

        PyRun_SimpleString ( "a = \"hi\"" );
        PyRun_SimpleString ( "print \"a=\" + a" );
        a = PyRun_String ( "a", 0, pdict, pdict );
        sa = PyString_AsString ( a );
        printf ( "sa = %s\n", sa );

        Py_DECREF ( pdict );
        Py_DECREF ( a );
        Py_Finalize ();

}

Thanks,

- Mike Johnson




More information about the Python-list mailing list