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 18:35:02 EST 2007


On Nov 9, 7:36 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
> En Tue, 06 Nov 2007 23:25:17 -0300, <snd... at gmail.com> escribió:
>
> > i naively created execution context:
> >     PyObject *execcontext = PyDict_New();
> > stuffed a handle in it:
> >     PyObject *ih = PyCObject_FromVoidPtr(handle, NULL);
> >     int st= PyDict_SetItemString(res, "interp", ih);
>
> What's `res`?
> One should make a lot of assumptions about your code because it's not
> complete. Please post a minimal complete example showing your problem.
>
> --
> Gabriel Genellina

for some reason i can't get compilestring/run_string to work in my
example.
compile the code below
and from the command line type

import node
print node.root()

you'd get:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1218588544 (LWP 13539)]
0x08048e37 in pyNode_root (self=0x0, args=0xb759d02c) at main.cpp:17
17              assert(PyCObject_Check(co));
(gdb) p co
$1 = (PyObject *) 0x0

because interp is not on globals

#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();

    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 *exec = PyDict_New();
    void *handle = (void*)0xdeadc0ed;
    PyObject *ih = PyCObject_FromVoidPtr(handle, NULL);
    //Py_INCREF(ih);
    int st= PyDict_SetItemString(exec, "interp", ih);
    assert(!st);

    PyRun_InteractiveLoop(stdin, "<stdin>");
}




More information about the Python-list mailing list