private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack

sndive at sndive at
Wed Nov 14 02:15:10 CET 2007

On Nov 13, 3:31 pm, "Chris Mellon" <arka... at> wrote:
> On Nov 13, 2007 3:18 PM,  <snd... at> wrote:
> > On Nov 9, 5:36 pm, "Gabriel Genellina" <gagsl-... at> wrote:
> > > En Tue, 06 Nov 2007 23:25:17 -0300, <snd... at> escribió:
> > > One should make a lot of assumptions about your code because it's not
> > > complete. Please post a minimal complete example showing your problem.
> > It's a rather large program. My assumption was that just posting the
> > snippet around the call site and the callee pathetic attempt
> > to extract interp would be sufficient :(
> The creation of a minimal runnable sample is a fantastic way to find
> any bugs in your code, and has the benefit of (if the bug is not in
> your code) giving other people a simple way to recreate the bug. If I
> were to check this (and I'm not, but I would if you'd posted runnable
> code) I'll have to write the code myself from your textual
> description. Then, if the code works, I'll have to post the code that
> I wrote as well as my negative response, and go through several back
> and forths trying to isolate any differences between what I wrote and
> what you wrote but didn't show. That's way more work than I'm willing
> to do to solve someone else's problem.
> In my experience, creating a minimal sample that demonstrates the bug
> will lead you to the actual bug more than half the time. That's a lot
> of time (yours and other peoples) that can be saved if you do it.

save as
import node

print "import test worked"

def runtest():
    print "runtest()!!!"
    res = node.root()

the program reproducing the problem:
#include <Python.h>

#ifndef PyMODINIT_FUNC	/* declarations for DLL import/export */
#define PyMODINIT_FUNC void

PyObject *g_mainmod;
PyObject *g_maindict;
bool worked = false;
PyObject *
pyNode_root(PyObject *self, PyObject *args)
        PyObject *dict = PyEval_GetGlobals();
        PyObject *co = PyDict_GetItemString(dict, "interp");
        void *interp = PyCObject_AsVoidPtr(co);
        // ...
        printf("root() worked\n");
        return 0;

static PyMethodDef module_methods[] = {
    /* no need to create pyNode from python programs
    {"new",		pyNode_new,		METH_VARARGS,
		PyDoc_STR("new() -> new Node object")},
    {"root",		pyNode_root,		METH_VARARGS,
		PyDoc_STR("root('dm') -> wrapper for the rootnode")},

    {NULL}  /* Sentinel */

    PyRun_SimpleString("import sys\n"
		       "print ': ',sys.path\n"

    g_mainmod = PyImport_AddModule("__main__");
    g_maindict = PyModule_GetDict(g_mainmod);
    Py_INCREF(g_maindict); // it was a borrowed reference

    PyObject* m = Py_InitModule("node", module_methods);
    if (m == NULL)
	return 1;

    PyObject *eglobal = PyDict_New();
    void *handle = (void*)0xdeadc0ed;
    PyObject *ih = PyCObject_FromVoidPtr(handle, NULL);
    int st= PyDict_SetItemString(eglobal, "interp", ih);

    PyObject *import = PyImport_ImportModule("testimp");
    if(!import) {
        return 3;
    PyModule_AddObject(g_mainmod, "testimp", import);

    PyObject *mScriptHandle=
                                 "comp", Py_eval_input);
    if(!mScriptHandle) {
        if (PyErr_Occurred()) {
        return 2;

    PyObject *res = PyEval_EvalCode((PyCodeObject*)mScriptHandle,
                                    eglobal, g_maindict);
    if (PyErr_Occurred()) {


More information about the Python-list mailing list