[C++-sig] PyFinalize Safety
Paul Guse
paul at mustagh.com
Fri Feb 16 19:18:05 CET 2007
> Stefan wrote:
>> 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.
>
> When reading the original posting I wondered about the reason for wanting
> to
> reinitialize the python runtime. Your reply makes me even more curious.
>
> What do you mean by the 'main dictionary' ? Each call to
> boost::python::exec_file
> can take a new dictionary, making the individual invocations completely
> independent,
> can't it ?
>
By main dictionary I mean the "__dict__" attribute to the main module.
Checking the documentation, exec/exec_file seems like the way to go,
however, I can't actually find exec.hpp or import.hpp in my boost build. I'm
using 1_33_1 and used the installer(win) from boost.consulting. Any thoughts
on why I would not have those 2 headers? Newer version? My version of
python.hpp does not include those files as well. Is this a new feature?
The solution I proposed came about due to the main module namespace
retaining enties in it's _dict_ upon successive sessions (embeded).
So if I run the example code without clearing and updating the dictionary,
then variables defined in once session will persist to the next.
I'm eager to try your solution.
>> 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) ;
>
> (nit-picking: all the above could be written using only boost.python)
That it should be :)
>
> Regards,
> Stefan
>
> --
>
> ...ich hab' noch einen Koffer in Berlin...
> _______________________________________________
> 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