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://docs.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?
An unhandled exception displays an error message and the program suddenly crashes. To avoid such a scenario, there are two methods to handle Python exceptions: Try – This method catches the exceptions raised by the program. Raise – Triggers an exception manually using custom exceptions.
You can easily avoid this error by checking that the Python dev files come with your operating system. To avoid the fatal error, you should not hard code the library and include paths. The pkg-config helps you insert the correct compiler options on the command line so an application can use gcc -o test test.
The try and except block in Python is used to catch and handle exceptions. Python executes code following the try statement as a “normal” part of the program. The code that follows the except statement is the program's response to any exceptions in the preceding try clause.
Errors cannot be handled, while Python exceptions can be handled at the run time. An error can be a syntax (parsing) error, while there can be many types of exceptions that could occur during the execution and are not unconditionally inoperable.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With