Redirecting stdio for extension function (again)

Michael Schmitt nomail at nomail.com
Mon Oct 27 10:14:38 EST 2003


Hello.

I wanted to capture the stdout output of a called extension function.

Michael Hudson kindly suggested a way to redirect filedescriptors.
I still can't get this working:

fd_stdout= os.dup(1)

def outputOff():
    nsofd = os.dup(1)
    nso = os.fdopen(nsofd, 'w')
    sys.stdout = nso
    dnfd = os.open('/dev/null', os.O_WRONLY)
    os.close(1)
    os.dup2(dnfd, 1)
    os.close(dnfd)

def outputOn():
    os.close(1)
    os.dup2(fd_stdout, 1)

this is used as:
outputOff()
call_to_extensionfunction()
outputOn()

Calling outputOn() seems to write the previously redirected output to 
the screen again. So it seems to be buffered.
I played a bit with ftruncate, but have no idea yet, how to discard the 
buffered output. I think I still didn't really get the details of file 
descriptors.

Thanks for any hints.

Michael




More information about the Python-list mailing list