[C++-sig] PyFinalize Safety

Paul Guse paul at mustagh.com
Fri Feb 16 17:58:44 CET 2007


Not sure if this solution/hack will work for you, but a quasi way to 
reinitialize the interpreter is to clear the main dictionary to the original 
state:

    - make a copy of the main dictionary upon initialization
    - run your python script
    - clear the main dictionary
    - set the dictionary to the original copy.

Some code:

    object main_module (handle<> (borrowed (PyImport_AddModule 
("__main__")))) ;
    object dictionary = main_module.attr ("__dict__") ;

    // copy the dictionary
    PyObject* clean_dict = PyDict_Copy (dictionary.ptr()) ;

    // run your python script.

    // reset main dictonary
    PyDict_Clear (dictionary.ptr()) ;
    PuDict_Update (dictionary.ptr(), clean_dict) ;
    Py_XDECREF (clean_dict) ;


Something like anyways. This could be a total hack, and I'm not sure it's 
foolproof, but it at least resets the main dictionary so that it appears to 
be a new session.

Paul



----- Original Message ----- 
From: "Podpruzhnikov Yuri" <ypodpruzhnikov at mcode.ru>
To: <c++-sig at python.org>
Sent: Thursday, February 15, 2007 7:56 AM
Subject: [C++-sig] PyFinalize Safety


> We embed Python to our project and use Boost.Python. Very much code had
> been already created.
> But great problem came to us: python interpreter must be reinitialized,
> but Boost.Python doesn't support "PyFinalize Safety"
> (http://www.boost.org/libs/python/todo.html#pyfinalize-safety)
>
> I see 2 decisions of the problem:
> 1) We delete Boost.Python from our project. But this will make extension
> of Python very difficult. :)
> 2) We write (or find) "PyFinalize Safety" of Boost.Python.
>
> I choose 2-nd decision till now. But if it is very difficult, I'll
> choose the 1-st way of solving the problem.
> I have a few questions to ask:
> - What is the difficulty of this problem to be solved?
> - Have anyone already written this?
> - If yes, could you give me code or advice at least?
>
> Sorry for my English.
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
> 




More information about the Cplusplus-sig mailing list