I’m puzzling over an embedded Python 2.7.2 interpreter issue. I’ve embedded the interpreter in a Visual C++ 2010 application and it essentially just calls user-written scripts.
My end-users want to use matplotlib – I’ve already resolved a number of issues relating to its dependence on numpy – but when they call savefig(), the application crashes with:
**Fatal Python Error: PyEval_RestoreThread: NULL tstate
This isn’t an issue running the same script using the standard Python 2.7.2 interpreter, even using the same site-packages, so it seems to definitely be something wrong with my embedding. I call Py_Initialize() – do I need to do something with setting up Python threads?
I can’t quite get the solution from other questions here to work, but I’m more concerned that this is symptomatic of a wider problem in how I’m setting up the Python interpreter.
Finally resolved this – so going to explain what occurred for the sake of Googlers!
This only happened when using third-party libraries like numpy or matplotlib, but actually related to an error elsewhere in my code. As part of the software I wrote, I was extending the Python interpreter following the same basic pattern as shown in the Python C API documentation.
At the end of this code, I called the Py_DECREF function on some of the Python objects I had created along the way. My mistake was that I was calling this function on borrowed references, which should not be done.
This caused the software to crash with the error above when it reached the Py_Finalize command that I used to clean up. Removing the DECREF on the borrowed references fixed this error.