[C++-sig] Multiple python interpreter contexts

Clay Culver clay at idleengineer.net
Tue Feb 22 21:06:49 CET 2005


I'm trying to make two separate contexts for my embedded python 
application.  That is, if I set some the X variable in one, it does not 
set X in the other.  I tried to do this by copying the dictionary like this:

    object mainModule(handle<>(borrowed(PyImport_AddModule("__main__"))));
    
    dict pyNamespace = dict(mainModule.attr("__dict__"));
    dict pyNamespace2 = dict(pyNamespace);

    handle<> result(PyRun_String(
        "test = 1\n",
        Py_file_input, pyNamespace.ptr(), pyNamespace.ptr())
        );

    handle<> result2(PyRun_String(
        "test = 2\n",
        Py_file_input, pyNamespace2.ptr(), pyNamespace2.ptr())
        );

    handle<> result3(PyRun_String(
        "print test\n",
        Py_file_input, pyNamespace.ptr(), pyNamespace.ptr())
        );

    handle<> result4(PyRun_String(
        "print test\n",
        Py_file_input, pyNamespace2.ptr(), pyNamespace2.ptr())
        );

But this always prints out 2, 2 instead of 1, 2 like I want it to.  What 
am I doing wrong here?

Thanks,
Clay



More information about the Cplusplus-sig mailing list