How can I catch segmentation fault in python?

John Nagle nagle at animats.com
Wed Nov 17 02:06:01 EST 2010


On 11/16/2010 10:15 PM, swapnil wrote:
> On Nov 17, 10:26 am, justin<justpar... 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?
>>
>> Thanks,
>> Justin.
>
> Segmentation fault isn't exactly an exception that you can catch. It
> usually means something has gone horribly wrong, like dereferencing
> invalid pointer, trying to access memory out of process's range. Since
> if you run out of memory Python simply raises MemoryError exception,
> which you can catch. So that is not the case for segmentation fault.

    Either fix the program so it doesn't crash,or run the offending
module and the C code in a subprocess.

				John Nagle



More information about the Python-list mailing list