How to hide stdout/stderr on python extension
Question: I want to hide stdout/stderr in a python script **BUT** the print is not done
into the python script itself but inside the underlying .c extension when a
python module needs one.

it is the case for _netsnmp_ python module, it requires compilation, the
problem is that there are some 'printf' directly into the C (client_intf.c)
code that I would like to capture and hide :

    
    
    from netsnmp import *
    import sys,os
    devnull = open(os.devnull, 'w')
    sys.stdout, sys.stderr = devnull, devnull
    session = Session(DestHost='myhostthatdoesnotexisttogenerateerror',Version=2,Community='demopublic')
    

When I run it I still have some traces :

    
    
    python test.py
    getaddrinfo: myhostthatdoesnotexisttogenerateerror nodename nor servname provided, or not known
    error:snmp_new_session: Couldn't open SNMP session
    

How can I get rid of these messages on stdout/stderr ? this is important
because I am developping Nagios plugins, and I can afford such output
(nevertheless, I want to capture them to raise a custom exception)


Answer: Depending on your OS (seems to work on both Windows and Linux at least) the
following would shut up fx `stdout` completely, by replacing the (low level)
file descriptor:

    
    
    nulf = os.open(os.devnull, os.O_WRONLY)
    os.dup2(nulf, stdout.fileno())
    os.close(nulf)
    

the same to `stderr` doesn't seem to work that well in interactive mode (I
guess that the python interpreter tries to write it's stuff to stderr).

This solution will have the same effect that redirect to `/dev/null` or
equivalent when starting the interpreter (except it takes effect later),
including any printouts that are made in native code (and of course any
printout your script is trying to do). If you want to do any logging you would
have to open a file for that (you could also open a file other than
`os.devnull` in the first place if you'd like to redirect `stdout` to a file
instead).

Note that normally a program doesn't check the result of printouts and in that
case one could simply close the descriptor (ie `os.close(stdout.fileno())`),
but that would fail if the extension actually checks the results and acts
differently based on that.

