Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <signal.h>
- #include <setjmp.h>
- jmp_buf env;
- void (*prev_handler)(int);
- void segv_handler(int signal)
- {
- longjmp(env, 1);
- }
- /*
- Macros to temporarily catch a segmentation fault.
- Usage:
- SEGV_TRY()
- // Risky code...
- SEGV_CATCH()
- // Error handling...
- SEGV_END()
- // Other code...
- */
- #define SEGV_TRY() \
- prev_handler = signal(SIGSEGV, segv_handler); \
- if (setjmp(env) == 0) {
- // The previous signal handler should be restored as soon as possible.
- // That's why it gets restored either at the end of the try block
- // (successful execution) or at the beginning of the catch block
- // (erroneous execution).
- #define SEGV_CATCH() \
- signal(SIGSEGV, prev_handler); \
- } else { \
- signal(SIGSEGV, prev_handler);
- #define SEGV_END() \
- }
- /*
- Macro to replace SEGV_CATCH() and SEGV_END(). A RuntimeError will be raised
- in the error handling code.
- Usage:
- SEGV_TRY()
- // Risky code...
- BOOST_SEGV_CATCH()
- // Other code...
- */
- #define BOOST_SEGV_CATCH() \
- SEGV_CATCH() \
- BOOST_RAISE_EXCEPTION(PyExc_RuntimeError, "Segmentation fault.") \
- SEGV_END()
Advertisement
Add Comment
Please, Sign In to add comment