How to suppress the output of an external module ?
MRAB
google at mrabarnett.plus.com
Wed Dec 27 15:26:19 EST 2006
Scott David Daniels wrote:
> fdu.xiaojf at gmail.com wrote:
> > Hi,
> >
> > I'm writing a program which uses an external module written in C
> > and calls a function provided by the module to do my job. The
> > function produces a lot of output to the stdout.
> >
> > Is there a way to suppress the output produced by the function and
> > hence make my program run faster?
>
> > It's too complicated for me to modify the source code and recompile
> > the external module.
> This would be the best method, you could define printf and fprintf
> macros that would eliminate the output code altogether.
>
> > Any hints will be greatly appreciated.
> Well, it will depend on your OS, but the trick is to essentially
> replace the C stdout channel with a file which has been opened to
> write to "/dev/null" or "NUL.txt" (unix and Windows respectively).
> You'll need to first copy the channel to another so you can use
> it again after the function is done (a system call). Next do the
> raw open (which should get the available channel), and the C stdout
> stuff is successfully redirected. Once done w/ your function,
> close your new stdout and copy the channel back.
>
In Windows the null device is, strictly speaking, "nul" or "nul:", not
"nul.txt", but the latter appears to work too.
More information about the Python-list
mailing list