Redirecting stdio for extension function (again)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Schmitt

    Redirecting stdio for extension function (again)

    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_stdo ut, 1)

    this is used as:
    outputOff()
    call_to_extensi onfunction()
    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
Working...