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
        """
if isinstance(new_target, file):
    new_target.close()
new_target = io.PrintStream(new_target.name)
Controlling stdout/stderr from Jython