4

I am wrapping some C library, and I have one function which in some cases may result with segmentation fault. In that case I need to call second function, which will in that situation complete successfully.

Does anyone knows how can I handle segmentation fault in cython?

4
  • 1
    Are you a Linux-ian or a Windows-ian ? My answer below is Linux/gcc oriented but can be quite easily adapted, probably by using something like "SetUnhandledExceptionFilter()", with "EXCEPTION_ACCESS_VIOLATION"... Commented Jul 3, 2013 at 10:54
  • Cross platform solution actually Commented Jul 3, 2013 at 11:01
  • 1
    This can be useful then: spin.atomicobject.com/2013/01/13/exceptions-stack-traces-c, with "__WIN32" macro usage... Commented Jul 3, 2013 at 11:05
  • Reviewing: the example above was fully compatible with Windows/gcc (MinGW)... Commented Jul 3, 2013 at 19:34

1 Answer 1

7

A short example that might help (using signal):

example.h (assuming the Cython extension is named myext.pyx)

// Header autogenerated by Cython when declaring "public api" functions
#include "../myext_api.h"  

void handleFuncFailure(char *func1_name, char *func2_name);
void generateSegFault();

example.c

#include <example.h>
#include <signal.h>

static char *func2name;

static void handler2(int sig)
{
    // Catch exceptions
    switch(sig)
    {
        case SIGSEGV:
            fputs("Caught SIGSEGV: segfault !!\n", stderr);
            break;
    }
    int error;
    // "call_a_cy_func()" is provided by "myext.pyx"
    call_a_cy_func(func2name, &error);
    exit(sig);
}

void handleFuncFailure(char *func1_name, char *func2_name) {

    // Provided by autogenerated "myext_api.h" header
    import_myext();

    func2name = func2_name;
    signal(SIGSEGV, handler2);
    int error;
    // "call_a_cy_func()" is provided by "myext.pyx"
    call_a_cy_func(func1_name, &error);
}

void generateSegFault() {
    *(int*) 0 = 0;
}

myext.pyx

# Helper function to call a function by its name
# "public api" enables to call this function from C side (See above)
cdef public api void call_a_cy_func(char* method, bint *error):
    if (method in globals()):
        error[0] = 0
        globals()[method]();
    else:
        error[0] = 1

# Expose C functions
cdef extern from "src/example.h":
    void handleFuncFailure(char *func1_name, char *func2_name)
    void generateSegFault()

# The unreliable function
def func1():
    print "hello1 ! Generating segfault..."
    generateSegFault()

# The reliable function
def func2():
    print "hello2 ! Running safe code..."

# To be called from the Cython extension inner code    
cdef myHandleFuncFailure(f1, f2):
    handleFuncFailure(f1, f2)

# To be called from Python source by importing "myext" module
def myHandleFuncFailure2():
    myHandleFuncFailure("func1", "func2")

Ouput

hello1 ! Generating segfault...

Caught SIGSEGV: segfault !!

hello2 ! Running safe code...

I hope this gives some ideas, at least...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.