Attached patch implements an handler for the signal SIGSEGV. It uses its own stack to be able to allocate memory on the stack (eg. call a function), even on stack overflow.
The patch requires sigaction() and sigaltstack() functions, but I didn't patched configure.in script. These functions are available on Linux, but should be available on other UNIX OSes.
segfault() signal handler supposes that the thread state is consistent (interp->frame chained list). It calls indirectly PyUnicode_EncodeUTF8() and so call PyBytes_FromStringAndSize() which allocates memory on the heap. It clears PyUnicode "defenc" attribute (the result of PyUnicode_EncodeUTF8()) to free directly the memory.
To test it, try some scripts in Lib/test/crashers/.
One example:
--------------------
$ ./python Lib/test/crashers/recursive_call.py
Fatal Python error: segmentation fault
Traceback (most recent call first):
File "Lib/test/crashers/recursive_call.py", line 12, depth 15715
File "Lib/test/crashers/recursive_call.py", line 12, depth 15714
File "Lib/test/crashers/recursive_call.py", line 12, depth 15713
...
File "Lib/test/crashers/recursive_call.py", line 12, depth 3
File "Lib/test/crashers/recursive_call.py", line 12, depth 2
File "Lib/test/crashers/recursive_call.py", line 9, depth 1
Segmentation fault
-------------------- |