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 17:59:56 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.

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);
triggers  Py_CompileString
failure with
Traceback (most recent call last):
  File "<string>", line 1, in ?
ImportError: __import__ not found

without RyRun_String call program runs but the pyNode_root is not
invoked

#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);
    int st= PyDict_SetItemString(exec, "interp", ih);
    assert(!st);


    PyRun_String("import node\nprint node.root()\n",
                 Py_file_input,
                 exec, g_maindict);

    PyObject *mScriptHandle=
        Py_CompileString("import node\nprint node.root()\n",
                                 "comp", Py_file_input);
    if(!mScriptHandle) {
        PyErr_Print();
        return 2;
    }

    PyObject *res = PyEval_EvalCode((PyCodeObject*)mScriptHandle,
                                    exec, g_maindict);

    assert(res=Py_None); // compiled as file_input
    assert(worked);
}




More information about the Python-list mailing list