How to I catch and handle a fatal error when Py_initialize fails?
I am embedding python in a C++ dll (so ultimately I can put it in an xll). When the setup is wrong Py_Initialize is documented as terminally failing - see http://doc开发者_JS百科s.python.org/c-api/init.html, "...it is a fatal error if the initialization fails.".
Is it possible to catch this error and how?
Thinking maybe a global windows hook?
I solved this by creating a separate executable that attempts to initialize python. My primary process will launch it and check the exit code and only call PyInitialize if the child process was successful. So, python is initialized twice, but it is better than an apparent crash to the user.
A fatal error is induced by calling Py_FatalError
, which bids farewell with an explanatory message and then calls abort()
.
At the moment, Jason's solution seems to be the only way, but this doesn't cover cases where Python fails due to a fatal error somewhere later after Py_Initialize()
, so there's no way for the embedding application to handle it in a more graceful manner (show GUI message and only then abort).
I've created a patch and issue in the Python bug tracker, cross-referencing this question: http://bugs.python.org/issue30560
精彩评论