Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Segmentation Fault (http://www.velocityreviews.com/forums/t367048-segmentation-fault.html)

pycraze 08-28-2006 04:02 AM

Segmentation Fault
 
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.


Simon Forman 08-28-2006 04:59 AM

Re: Segmentation Fault
 
pycraze wrote:
> 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


John Machin 08-28-2006 07:14 AM

Re: Segmentation Fault
 
pycraze wrote:
> 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


Steve Holden 08-28-2006 08:41 AM

Re: Segmentation Fault
 
pycraze wrote:
> 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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden


Duncan Booth 08-28-2006 09:16 AM

Re: Segmentation Fault
 
pycraze wrote:

> 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.

thomas.samson@gmail.com 08-28-2006 12:43 PM

Re: Segmentation Fault
 
"Simon Forman" <rogue_pedro@yahoo.com> writes:

> pycraze wrote:
>> 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.


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. "

Simon Forman 08-28-2006 05:47 PM

Re: Segmentation Fault
 
thomas.samson@gmail.com wrote:
> "Simon Forman" <rogue_pedro@yahoo.com> writes:
>
> > pycraze wrote:
> >> 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.

>
> 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


John Savage 08-31-2006 01:42 PM

Re: Segmentation Fault
 
"pycraze" <dennis.varghese@wipro.com> writes:
>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.
--
John Savage (my news address is not valid for email)


All times are GMT. The time now is 05:18 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.