views:

168

answers:

1

I have a C++ app that embeds the Python interpreter. There are points in the code where the interpreter may get interrupted and I need to make sure the interpreter is in a 'safe' state to execute new code. I would just call Py_Finalize and re-initialize everything except I have a bunch of PyObject * references that I need to stay valid. Is there a function to do this or is it even necessary? When I mentioned the interpreter being interrupted above, I meant by a seg. fault or access violation which my app tries to recover from.

+1  A: 

Er, trying to "recover" from a segfault or access violation is quite dangerous. There is a reason you get these in the first place, and it's that your program has tried to do something which it shouldn't have tried to do; therefore it has hit a bug or an unforeseen condition.

There is no provision in the Python interpreter to roll back to a "safe point" in cases such as those mentioned. Even finalizing and reinitializing the interpreter might still leave some static data in a inconsistent state.

If you told us why you are trying to do this we might be able to suggest an alternative.

Antoine P.
My application embeds the Python interpreter to allow third parties to extend it. Some of my own scripts (especially ones with tkinter) crash for some reason and debugging tells me the offending code is in python31.dll
George Edison
Then the only sane answer is to try to find out what crashes exactly, and fix it and/or circumvent it and/or report a bug to http://bugs.python.org :-)
Antoine P.
Well, I gave it a try and it works! I call SetUnhandledExceptionFilter first and then when an access violation occurs, my function gets called. Then I simply increase register EIP until the troublesome code is past, then resume. Of course, the user is informed that he should save his work and restart, but it does work.
George Edison