Segmentation Fault

Discussion in 'Python' started by pycraze, Aug 28, 2006.

  1. pycraze

    pycraze Guest

    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.
     
    pycraze, Aug 28, 2006
    #1
    1. Advertising

  2. pycraze

    Simon Forman Guest

    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
     
    Simon Forman, Aug 28, 2006
    #2
    1. Advertising

  3. pycraze

    John Machin Guest

    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
     
    John Machin, Aug 28, 2006
    #3
  4. pycraze

    Steve Holden Guest

    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
     
    Steve Holden, Aug 28, 2006
    #4
  5. pycraze

    Duncan Booth Guest

    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.
     
    Duncan Booth, Aug 28, 2006
    #5
  6. pycraze

    Guest

    "Simon Forman" <> 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. "
     
    , Aug 28, 2006
    #6
  7. pycraze

    Simon Forman Guest

    wrote:
    > "Simon Forman" <> 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
     
    Simon Forman, Aug 28, 2006
    #7
  8. pycraze

    John Savage Guest

    "pycraze" <> 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)
     
    John Savage, Aug 31, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    893
  2. Pud
    Replies:
    0
    Views:
    605
  3. Replies:
    0
    Views:
    568
  4. Ivan Vecerina
    Replies:
    0
    Views:
    506
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,743
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page