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

sndive at gmail.com sndive at gmail.com
Tue Nov 13 20:15:10 EST 2007


On Nov 13, 3:31 pm, "Chris Mellon" <arka... at gmail.com> wrote:
> On Nov 13, 2007 3:18 PM,  <snd... at gmail.com> wrote:
>
> > On Nov 9, 5:36 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
> > > En Tue, 06 Nov 2007 23:25:17 -0300, <snd... at gmail.com> 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 testimp.py:
import node

print "import test worked"

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

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

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

PyObject *g_mainmod;
PyObject *g_maindict;
bool worked = false;
static
PyObject *
pyNode_root(PyObject *self, PyObject *args)
{
        PyObject *dict = PyEval_GetGlobals();
        PyObject *co = PyDict_GetItemString(dict, "interp");
        assert(PyCObject_Check(co));
        void *interp = PyCObject_AsVoidPtr(co);
        assert(interp);
        // ...
        printf("root() worked\n");
        worked=true;
        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 */
};

int
main()
{
    Py_Initialize();
    PyRun_SimpleString("import sys\n"
		       "sys.path.append('.')\n"
		       "print ': ',sys.path\n"
		       );

    g_mainmod = PyImport_AddModule("__main__");
    assert(g_mainmod);
    g_maindict = PyModule_GetDict(g_mainmod);
    assert(g_maindict);
    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);
    assert(!st);

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

    PyObject *mScriptHandle=
        Py_CompileString("testimp.runtest()",
                                 "comp", Py_eval_input);
    if(!mScriptHandle) {
        if (PyErr_Occurred()) {
            PyErr_Print();
        }
        return 2;
    }

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

    Py_DECREF(res);
    assert(worked);
}




More information about the Python-list mailing list