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

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Thu Apr 8 09:59:05 CEST 2004


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).

Pierre


On Thu, 2004-04-08 at 05:13, iceryeah wrote:
> hello,
>     if python script has error when c++ prog is running, i can get the
> expection with error_already_set, now i want to get the expection
> infomation, but python interpreter output info to std_err, i can't get it!
>     tell me your method how to get expection info. thanks
> 
> 
> 
> 
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68 





More information about the Cplusplus-sig mailing list