Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I catch and handle a fatal error when Py_initialize fails?

Tags:

c++

python

c

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?

like image 545
DangerMouse Avatar asked Oct 07 '11 14:10

DangerMouse


People also ask

How to handle unexpected error in Python?

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.

How do you fix a fatal error in Python?

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.

How to catch exception error in Python?

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.

How to handle runtime error in Python?

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.


2 Answers

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.

like image 104
Jason Avatar answered Oct 13 '22 02:10

Jason


A fatal error is induced by calling Py_FatalError, which bids farewell with an explanatory message and then calls abort().

like image 38
Marcelo Cantos Avatar answered Oct 13 '22 04:10

Marcelo Cantos