Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress stdout / stderr print from Python functions

Tags:

python

stdout

I have a Python script that is using some closed-box Python functions (i.e. I can't edit these functions) provided by my employer. When I call these functions, they are printing output to my linux terminal that I would like to suppress. I've tried redirecting stdout / stderr via;

orig_out = sys.stdout sys.stdout = StringIO() rogue_function() sys.stdout = orig_out 

but this fails to catch the output. I think the functions I'm calling via-Python (rogue_function() from above) are really wrappers for compiled C-code, which are actually doing the printing.

Does anyone know of a way I can do a "deep-capture" of any print handed to stdout / stderr by a function (and any sub-functions that function calls)?

UPDATE:

I ended up taking the method outlined in the selected answer below and writing a context manager to supress stdout and stderr:

# Define a context manager to suppress stdout and stderr. class suppress_stdout_stderr(object):     '''     A context manager for doing a "deep suppression" of stdout and stderr in      Python, i.e. will suppress all print, even if the print originates in a      compiled C/Fortran sub-function.        This will not suppress raised exceptions, since exceptions are printed     to stderr just before a script exits, and after the context manager has     exited (at least, I think that is why it lets exceptions through).            '''     def __init__(self):         # Open a pair of null files         self.null_fds =  [os.open(os.devnull,os.O_RDWR) for x in range(2)]         # Save the actual stdout (1) and stderr (2) file descriptors.         self.save_fds = [os.dup(1), os.dup(2)]      def __enter__(self):         # Assign the null pointers to stdout and stderr.         os.dup2(self.null_fds[0],1)         os.dup2(self.null_fds[1],2)      def __exit__(self, *_):         # Re-assign the real stdout/stderr back to (1) and (2)         os.dup2(self.save_fds[0],1)         os.dup2(self.save_fds[1],2)         # Close all file descriptors         for fd in self.null_fds + self.save_fds:             os.close(fd) 

To use this you just:

with suppress_stdout_stderr():     rogue_function() 

This works "pretty good". It does suppress the printout from the rogue functions that were cluttering up my script. I noticed in testing it that it lets through raised exceptions as well as some logger print, and I'm not entirely clear why. I think it has something to do with when these messages get sent to stdout / stderr (I think it happens after my context manager exits). If anyone can confirm this, I'd be interested in hearing the details ...

like image 999
jeremiahbuddha Avatar asked Jun 21 '12 00:06

jeremiahbuddha


People also ask

How do I disable stderr in Python?

suppress stdout and stderr with context managerUse suppress_stdout and suppress_stderr flags to indicate which stream to be suppressed. Save the state of the sys. stdout and sys. stderr in the __enter__ function, and redirect them to devnull based on the suppress_stdout and suppress_stderr flags.

How do you turn off print in Python?

If you don't want that one function to print, call blockPrint() before it, and enablePrint() when you want it to continue. If you want to disable all printing, start blocking at the top of the file.


2 Answers

As of python 3.5 we can do this with minimal work using built-ins in contextlib, namely redirect_stdout and redirect_stderr. We only need to combine these two built-in context managers in a custom context manager of ours, which can be easily done using the nice pattern in Martijn's answer here. Redirecting both outputs to os.devnull should be safe and portable enough.

from contextlib import contextmanager,redirect_stderr,redirect_stdout from os import devnull  @contextmanager def suppress_stdout_stderr():     """A context manager that redirects stdout and stderr to devnull"""     with open(devnull, 'w') as fnull:         with redirect_stderr(fnull) as err, redirect_stdout(fnull) as out:             yield (err, out) 

Note that suppressing stderr will still give you full tracebacks when something breaks, which is a good thing:

import sys  def rogue_function():     print('spam to stdout')     print('important warning', file=sys.stderr)     1 + 'a'     return 42  with suppress_stdout_stderr():     rogue_function() 

When run the above only prints

Traceback (most recent call last):   File "tmp.py", line 20, in <module>     rogue_function()   File "foo.py", line 16, in rogue_function     1 + 'a' TypeError: unsupported operand type(s) for +: 'int' and 'str' 

to the terminal. Unhandled exceptions should never go unnoticed.

like image 163

This approach (found through the related sidebar) might work. It reassigns the file descriptors rather than just the wrappers to them in sys.stdout, etc.

like image 42
Danica Avatar answered Sep 28 '22 05:09

Danica