Skip to content

Instantly share code, notes, and snippets.

@tanakh
Created July 12, 2010 05:33
Show Gist options
  • Save tanakh/472150 to your computer and use it in GitHub Desktop.
Save tanakh/472150 to your computer and use it in GitHub Desktop.
string GetPythonErrorString()
{
try{
PyObject *ptype, *pvalue, *ptraceback;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
object prev_stderr = import("sys").attr("stderr");
object output_cacher = import("cStringIO").attr("StringIO")();
import("sys").attr("stderr") = output_cacher;
PyErr_Restore(ptype, pvalue, ptraceback);
PyErr_Print();
import("sys").attr("stderr") = prev_stderr;
return extract<string>(output_cacher.attr("getvalue")())();
}catch(...){
handle_exception();
PyErr_Clear();
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment