After looking at the python source, it looks like the proper way to do this is:
Code:
if( <print traceback> )
{
Py_XINCREF(t);
Py_XINCREF(val);
Py_XINCREF(tb);
PyErr_Restore(t, val, tb);
PyErr_PrintEx(0);
}
The difference is in the last call which tells it not to 'save' the current exception values when printing them, thus allowing the ref counts to clear normally. Been using this for better part of a day now with no trouble.