[C++-sig] Redirecting stdout for B.P extensions?

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Wed Sep 14 19:23:58 CEST 2005


Hello,

Basically, sys.stdout is NOT the standard output of your program, it's
merely the Python wrapper used as stdout. So if you replace sys.stdout,
only Python-aware code explicitly using sys.stdout will see its output
redirected.

If you want to redirect the *real* stdout, you have to use the good old
C way ;) Basically, replace the file descriptor 1 (stdout) with any file
descriptor you have created (usually a pipe). But this becomes a system
question without relation to Python !

Pierre

Hubert Holin a écrit :
> Somewhere in the E.U., le 14/09/2005
> 
>      Bonjour
> 
>          In a (perhaps perverse) project involving Objective-C  
> (Cocoa), Python and C++, I have run into a difficulty for which I  
> know an unpleasant workaround, and for which I am looking for a  
> gentler cure.
> 
>          In essence, at some point a Python script is calling a  
> function which is actually coded in C++ using Boost.Python (and began  
> life as part of a terminal tool). Prior to the call, I redirect  
> Python's output stream (by assigning to sys.stdout). From that point  
> onward, Python does print where it is told to, but C++ code within  
> the extension which "<<"'s to ::std::cout is not redirected (apart  
> from that, the whole shebang works nicely).
> 
>          As I have full control over the C++ code, I can add another  
> parameter to the extension function's call to represent the  
> destination of outputs, and recode all over the place. That's the  
> unpleasant workaround. What I would like would be for a mechanism to  
> tell the extension to actually use Python's current sys.stdout. At  
> any rate, there will necessarily come a time when I will use some C++  
> code over which I will *not* have control, so a general cure would be  
> best...
> 
>      Merci
> 
>              Hubert Holin
> 
> P.S.: sorry, I found no way to search the archive... pointers to that  
> welcome as well!
> 
> _______________________________________________
> 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