redirecting stdio for extension function

M

Michael Schmitt

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?

Thanks and best regards,
Michael
 
M

Michael Hudson

Michael Schmitt said:
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
 
M

Michael Schmitt

I am using an extension function (written in C), which produces a lot of
You can muck with the file descriptors 0, 1 and 2 at a pretty low
level if you're on Unix:

Thanks for the hint.
I tried the following two functions:

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)

But 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.

Thanks.

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top