Manage stdout / stderr from Jython

I am calling a function in a java library from jython that prints to stdout. I would like to suppress this output from a jython script. I am trying to use the python idiom, replacing sys.stdout with a file like an object (StringIO), but this does not display the output of the java library. I assume sys.stdout does not affect the java program. Is there a standard convention for redirecting or suppressing this output programmatically in jython? If not, in what ways can I do this?

+7
source share
2 answers

You can use System.setOut , for example:

 >>> from java.lang import System >>> from java.io import PrintStream, OutputStream >>> oldOut = System.out >>> class NoOutputStream(OutputStream): ... def write(self, b, off, len): pass ... >>> System.setOut(PrintStream(NoOutputStream())) >>> System.out.println('foo') >>> System.setOut(oldOut) >>> System.out.println('foo') foo 

Note that this will not affect Python output, as Jython captures System.out when it starts, so you can reassign sys.stdout as you would expect.

+9
source

I created a context manager to mimic (Python3) contextlib redirect_stdout (here) :

 '''Wouldn't it be nice if sys.stdout knew how to redirect the JVM stdout? Shooting star. Author: Sean Summers < seansummers@gmail.com > 2015-09-28 v0.1 Permalink: https://gist.githubusercontent.com/seansummers/bbfe021e83935b3db01d/raw/redirect_java_stdout.py ''' from java import io, lang from contextlib import contextmanager @contextmanager def redirect_stdout(new_target): ''' Context manager for temporarily redirecting sys.stdout to another file or file-like object see contextlib.redirect_stdout documentation for usage ''' # file objects aren't java.io.File objects... if isinstance(new_target, file): new_target.close() new_target = io.PrintStream(new_target.name) old_target, target = lang.System.out, new_target try: lang.System.setOut(target) yield None finally: lang.System.setOut(old_target) 
+1
source

All Articles