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

sndive at gmail.com sndive at gmail.com
Wed Nov 14 19:08:42 CET 2007


On Nov 14, 12:57 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Tue, 13 Nov 2007 19:59:56 -0300, <snd... at gmail.com> escribió:
>
> > working on a smaller example. i could not get pyNode_root invoked yet
> > and
> > PyRun_String("import node\nprint node.root()\n",
> >                  Py_file_input,
> >                  exec, g_maindict);
>
> The globals argument should contain (at least) a key "__builtins__"
> pointing to the __builtin__ module; else, no builtin functions (like
> __import__) will be found. PyEval_Globals (you used it somewhere) returns
> a suitable globals argument.
>
thank you.
result is the same however:
pyt: main.cpp:17: PyObject* pyNode_root(PyObject*, PyObject*):
Assertion `co' failed.

Program received signal SIGABRT, Aborted.
[Switching to Thread -1218617216 (LWP 4807)]
0x00a54eff in raise () from /lib/tls/libc.so.6

with testimp.py:
import node

print "import test worked"

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

and the code
#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(co);
        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 *builtins = PyEval_GetBuiltins();
    st= PyDict_SetItemString(eglobal, "__builtins__", builtins);
    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