Segmentation Fault

P

pycraze

I would like to ask a question. How do one handle the exception due to
Segmentation fault due to Python ? Our bit operations and arithmetic
manipulations are written in C and to some of our testcases we
experiance Segmentation fault from the python libraries.

If i know how to handle the exception for Segmentation fault , it will
help me complete the run on any testcase , even if i experiance Seg
Fault due to any one or many functions in my testcase.
 
S

Simon Forman

pycraze said:
I would like to ask a question. How do one handle the exception due to
Segmentation fault due to Python ? Our bit operations and arithmetic
manipulations are written in C and to some of our testcases we
experiance Segmentation fault from the python libraries.

If i know how to handle the exception for Segmentation fault , it will
help me complete the run on any testcase , even if i experiance Seg
Fault due to any one or many functions in my testcase.

AFAIK, seg fault kills your program dead. There's no exception to
handle. If you're getting seg faults from the python standard library,
that's a pretty serious thing, way more serious than just not-passed
testcases.

An uncaught exception in python should generate a traceback. If it
does, please post that.

Whether it does or not, could you post a minimal example of the code
that causes the seg fault? Python standard lib is fairly robust but of
course not bullet-proof. If there's a way to make it seg fault (and
it's in the standard lib and not your C code) people will want to know.

HTH,
~Simon
 
J

John Machin

pycraze said:
I would like to ask a question. How do one handle the exception due to
Segmentation fault due to Python ?

This is confusing. A seg fault kills the process immediately. No
exception (in the Python sense of that word) is raised.
Our bit operations and arithmetic
manipulations are written in C

Do you have a Python extension that is written in C, or are you
embedding Python in a C program -- in other words, is a Python script
calling a C function, or is a C program calling a Python function?
and to some of our testcases we
experiance Segmentation fault from the python libraries.

What is it that you are calling "the python libraries"? Tell us the
filenames of these libraries. *Show us what is output on your stderr
when you experience "Segmentation fault from the python libraries".*

Was this Python/C combination recently created by current personnel, or
do we have a case of vanished author(s)?
If i know how to handle the exception for Segmentation fault , it will
help me complete the run on any testcase , even if i experiance Seg
Fault due to any one or many functions in my testcase.

It would also help if you told us what platform (hardware and software)
that you are running on, what version of Python, and what versions of
any 3rd-party Python modules/packages that may be involved.

Cheers,
John
 
S

Steve Holden

pycraze said:
I would like to ask a question. How do one handle the exception due to
Segmentation fault due to Python ? Our bit operations and arithmetic
manipulations are written in C and to some of our testcases we
experiance Segmentation fault from the python libraries.

If i know how to handle the exception for Segmentation fault , it will
help me complete the run on any testcase , even if i experiance Seg
Fault due to any one or many functions in my testcase.
You will observe, if you look at the list of Python exceptions, that a
segfault isn't among them. A segfault is a trap to the hardware, and
generally indicates that a program is so badly hosed that it wouldn't
make much sense to rely on any further computation in it.

regards
Steve
 
D

Duncan Booth

pycraze said:
I would like to ask a question. How do one handle the exception due to
Segmentation fault due to Python ? Our bit operations and arithmetic
manipulations are written in C and to some of our testcases we
experiance Segmentation fault from the python libraries.
From what you said, I guess you mean that you have written some extensions
to Python in C, but that the segmentation fault is generated when you call
standard Python library functions.

Most likely your C code has bugs: quite possibly you have messed up
reference counting so some objects you use are being freed and then the
Python libraries allocate other objects into the same memory. It is very
easy to do that especially if you are in the habit of trying to 'borrow'
references instead of religiously incrementing/decrementing reference
counts everywhere.

Try to eliminate the problem down to the smallest reproducable code sample
and post that.

Another suggestion I would make is to see if you can use ctypes or Pyrex to
form the glue between Python and C so that none of the C code you write
knows anything about Python. That should eliminate some possible sources
for error.
 
T

thomas.samson

Simon Forman said:
AFAIK, seg fault kills your program dead. There's no exception to
handle. If you're getting seg faults from the python standard library,
that's a pretty serious thing, way more serious than just not-passed
testcases.

Segfault handling is platform-dependant... So, at least on unix-like
platform, you can use the signal module to detect segfault:

import signal

def handler(signum, frame):
print 'Segfault detected'
# you may use the stack frame here to help debugging

signal.signal(signal.SIGSEGV, handler)
 
S

Simon Forman

Segfault handling is platform-dependant... So, at least on unix-like
platform, you can use the signal module to detect segfault:

import signal

def handler(signum, frame):
print 'Segfault detected'
# you may use the stack frame here to help debugging

signal.signal(signal.SIGSEGV, handler)

--
Thomas SAMSON
"You're very sure of your facts, " he said at last, "I
couldn't trust the thinking of a man who takes the Universe
- if there is one - for granted. "

It's good to know that this is possible. However, it's almost
certainly a bad idea to "catch" seg faults and then just proceed with
further testcases. Printing out debugging information would be pretty
good though.

Peace,
~Simon
 
J

John Savage

pycraze said:
I would like to ask a question. How do one handle the exception due to
Segmentation fault due to Python ? Our bit operations and arithmetic
manipulations are written in C and to some of our testcases we
experiance Segmentation fault from the python libraries.

You don't happen to be using the MSDOS executable do you? I find I get
a segmentation violation with the MSDOS version when I try to print
coloured text. I think I have narrowed it down further: to the situation
where coloured text extends to or beyond the edge of the screen--maybe
it's the text wrapping that causes the seg fault.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top