execl() and inheritied streams

Erik Johnson ej
Tue Sep 28 23:21:43 CEST 2004


    I eventually figured out that that was basically what was happening, but
didn't know how to set them at a lower level and wasn't having much luck
searching documentation on keywords.

Thanks for your reply! :)

-ej

>   Replacing sys.stdout, sys.stderr, and sys.stdin only effects code which
goes through the sys module to find those streams.  The new process you
execl() is very unlikely to do this, since your instance of the sys module
will be destroyed by the execl() call :)  Instead, you need to twiddle file
descriptors:
>
>     f = file('out.txt', 'a')
>     os.dup2(f.fileno(), sys.stdout.fileno())
>     os.dup2(f.fileno(), sys.stderr.fileno())
>
>   Now 1 and 2 (stdout and stderr, respectively) refer to a different file
object down in the file descriptor table, far away from your process.  These
survive execl() and so can have an effect on the executed process.
>
>   Jp





More information about the Python-list mailing list