Getting stdout using ctypes.

Christian Heimes lists at cheimes.de
Thu Aug 14 13:42:45 EDT 2008


Grant Edwards wrote:
> If he's running Unix, he needs to replace the stdout file
> descriptor (fd 1), with something that's connected to the
> "write" end of a pipe.  Then he needs to read the other
> ("read") end of the pipe in his application.
> 
> You do that using the dup2() system call:

It's dangerous to mix streams and descriptors. Traditionally freopen() 
is used to redirect a standard stream to a file:

http://www.gnu.org/software/libc/manual/html_mono/libc.html#Standard-Streams

— Function: FILE * freopen (const char *filename, const char *opentype, 
FILE *stream)

     This function is like a combination of fclose and fopen. It first 
closes the stream referred to by stream, ignoring any errors that are 
detected in the process. (Because errors are ignored, you should not use 
freopen on an output stream if you have actually done any output using 
the stream.) Then the file named by filename is opened with mode 
opentype as for fopen, and associated with the same stream object stream.

     If the operation fails, a null pointer is returned; otherwise, 
freopen returns stream.

     freopen has traditionally been used to connect a standard stream 
such as stdin with a file of your own choice. This is useful in programs 
in which use of a standard stream for certain purposes is hard-coded. In 
the GNU C library, you can simply close the standard streams and open 
new ones with fopen. But other systems lack this ability, so using 
freopen is more portable.




More information about the Python-list mailing list