Redirecting stdio for extension function (again)

M

Michael Schmitt

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top