How to prevent inline python to exit () my process

I am having problems running embedded python. Turns out I can't catch this SystemExit exception thrown by sys.exit ();

This is what I still have:

$ cat call.c #include <Python.h> int main(int argc, char *argv[]) { Py_InitializeEx(0); PySys_SetArgv(argc-1, argv+1); if (PyRun_AnyFileEx(fopen(argv[1], "r"), argv[1], 1) != 0) { PyObject *exc = PyErr_Occurred(); printf("terminated by %s\n", PyErr_GivenExceptionMatches(exc, PyExc_SystemExit) ? "exit()" : "exception"); } Py_Finalize(); return 0; } 

Also, my script:

 $ cat unittest-files/python-return-code.py from sys import exit exit(99) 

Launch:

 $ ./call unittest-files/python-return-code.py $ echo $? 99 

I should execute the file, not the command.

+8
python exception embedded exit
source share
1 answer
Function

PyRun_SimpleFileExFlags (and all its functions, including PyRun_AnyFileEx ) handle the exceptions themselves, going beyond SystemExit or printing a trace. Use the PyRun_File* family of functions to handle exceptions in the surrounding code.

+5
source share

All Articles