redirecting stdio for extension function

Michael Hudson mwh at python.net
Thu Oct 16 12:07:29 EDT 2003


Michael Schmitt <nomail at nomail.com> writes:

> Hello.
> 
> I am using an extension function (written in C), which produces a lot of 
> output via fprintf statements. 
> Is there a way to redirect this output, without changing the extension 
> function?

You can muck with the file descriptors 0, 1 and 2 at a pretty low
level if you're on Unix:

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)

or similar might do what you want (untested...).

Cheers,
mwh


-- 
  > The conversion rate from Imperial Shitloads to Metric Shitloads is
  > to multiply by 1.07.  Which you multiply is left as an exercise.
  Both.
                                   -- Bob McCown & Jasper Janssen, asr




More information about the Python-list mailing list