title: SIGSEGV handler using Python with C extension modules
body: <p>I'm trying to setup a SIGSEGV signal handler for Python using a C extension module:</p>
  <pre class="lang-py prettyprint-override"><code>import my_extension_module
  import ctypes
  
  my_extension_module.setup_segfault_handler()
  ctypes.string_at(0) # i would like this to error and not segfault
  </code></pre>
  <p>I've seen several questions like <a href="https://stackoverflow.com/questions/2705792/c-sigsegv-handler-mprotect">this one</a> and functions like <code>longjmp</code> or <code>setjmp</code>, but none of these seem to work how I intend.</p>
  <p>I <strong>am aware</strong> that not terminating after a SIGSEGV is a bad idea for a number of reasons, but I need it nonetheless.</p>
  <p>Here is my current code:</p>
  <pre class="lang-c prettyprint-override"><code>static jmp_buf buf;
  
  void handle(int signum) {
    PyErr_SetString(PyExc_RuntimeError, &quot;segmentation fault occured&quot;);
    longjmp(buf, 1);
  }
  
  static PyObject* method_setup() {
    signal(SIGSEGV, handle);
    setjmp(buf);
  
    Py_RETURN_NONE;
  }
  </code></pre>