[C++-sig] Re: how to get python expection infomation in c++?

David Abrahams dave at boost-consulting.com
Thu Apr 8 13:28:09 CEST 2004


Pierre Barbier de Reuille <pierre.barbier at cirad.fr> writes:

> Hi !
>
> I had the same problem ... so I redirected sys.stderr to a pipe I opened
> before ...
>
> Here's the few lines that do that :
>
>       int nb_err[2];
>       FILE* err, *rd_err;
>       pipe( nb_err );
>       // Initialise the pipe
>       err = fdopen( nb_err[ 1 ], "w" );
>       setlinebuf( err );
>       rd_err = fdopen( nb_err[ 0 ], "r" );
>       // Install stderr
>       boost::python::handle<> herr( PyFile_FromFile(err, "embedded_err",
> "w", fclose ) );
>       object obj_err( herr );
>       main_namespace[ "__embedded_stderr__" ] = obj_err;
>       PyRun_String( "sys.stderr = __embedded_stderr__", Py_file_input,
> main_namespace.ptr(), main_namespace.ptr() );
>
> After that, you can read the errors in rd_err. The assumption is made
> that the sys module is already imported ! Also, main_namespace is a
> boost::python::dict wrapping the __main__ module dictionnary. As I don't
> need to interpret what's written but just to forward it to the user,
> this interface is ok for me (I just have a thread dedicated to listen
> the error output).

Easier approaches are detailed here:

    http://tinyurl.com/uiis

and in:

    http://tinyurl.com/uiiu

FWIW,

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list