1# 2# Copyright (c) 2016-2018 JEP AUTHORS. 3# 4# This file is licensed under the the zlib/libpng License. 5# 6# This software is provided 'as-is', without any express or implied 7# warranty. In no event will the authors be held liable for any 8# damages arising from the use of this software. 9# 10# Permission is granted to anyone to use this software for any 11# purpose, including commercial applications, and to alter it and 12# redistribute it freely, subject to the following restrictions: 13# 14# 1. The origin of this software must not be misrepresented; you 15# must not claim that you wrote the original software. If you use 16# this software in a product, an acknowledgment in the product 17# documentation would be appreciated but is not required. 18# 19# 2. Altered source versions must be plainly marked as such, and 20# must not be misrepresented as being the original software. 21# 22# 3. This notice may not be removed or altered from any source 23# distribution. 24# 25 26import sys 27 28class StdOutToJava(object): 29 "Redirects Python's sys.stdout to Java's System.out" 30 31 def __init__(self): 32 from java.lang import System 33 self.oldout = sys.stdout 34 self.printmethod = getattr(System.out, 'print') 35 self.flushmethod = getattr(System.out, 'flush') 36 37 def write(self, msg): 38 self.printmethod(msg) 39 40 def flush(self): 41 self.flushmethod() 42 43 44class StdErrToJava(object): 45 "Redirects Python's sys.stderr to Java's System.err" 46 47 def __init__(self): 48 from java.lang import System 49 self.olderr = sys.stderr 50 self.printmethod = getattr(System.err, 'print') 51 self.flushmethod = getattr(System.err, 'flush') 52 53 def write(self, msg): 54 self.printmethod(msg) 55 56 def flush(self): 57 self.flushmethod() 58 59 60def setup(): 61 sys.stdout = StdOutToJava() 62 sys.stderr = StdErrToJava() 63