ctypes: catch system exit from C library

Duncan Booth duncan.booth at invalid.invalid
Thu Nov 3 07:32:35 EDT 2011


Martin Landa <landa.martin at gmail.com> wrote:

> Hi all,
> 
> in my python application I am calling functions from a C library via
> `ctypes` interface. Some fns from that C library calls `exit()` on
> error. It causes that the python application crashes even without any
> notification. Is it possible to catch library system exit calls from
> such python application?
> 
> Thanks in advance, Martin

There is no safe way to do this. An unsafe way would be to install an 
atexit() handler and longjmp out of it, but that would mess up any other 
atexit processing and quite likely crash the program. Or you could just 
move the essential cleanup into atexit() but not attempt to stop the 
program termination.

The clean (but not necessarily easy) solution would be to move all of the 
code that uses the library into a separate subprocess and then you can 
catch and handle the subprocess exiting in your main code.


-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list