How can I catch segmentation fault in python?
clp2 at rebertia.com
Wed Nov 17 08:01:05 CET 2010
On Tue, Nov 16, 2010 at 9:26 PM, justin <justpark78 at gmail.com> wrote:
> Hi all,
> I am calling a program written in C inside Python using ctypes,
> and it seems that sometimes the program in C crashes while it's being
> used in Python.
> Even under the circumstances, I want to get the Python program going
> by handling the segmentation fault.
> I've already searched the Internet, but couldn't get the right answer
> to catch them.
> Could any of you please let me know how to deal with this and catch
> the segmentation fault in Python?
You can't "catch" it. It's not a mere Exception, it's a fatal error
that effectively crashes the interpreter itself.
At best, you could perhaps use the `multiprocessing` or `subprocess`
modules to segregate the part of your program that uses ctypes and
then try and recover when you detect that the subprocess has crashed.
Instead, if possible, fix the C program and/or ensure that there's not
an error in your ctypes interfacing code.
More information about the Python-list