tests/utils.py
def runAsJava(self, main_code, extra_code=None, args=None):
        """Run a block of Python code as a Java program."""
        # Output source code into test directory
        transpiler = Transpiler(verbosity=0)

        # Don't redirect stderr; we want to see any errors from the transpiler
        # as top level test failures.
        with capture_output(redirect_stderr=False):
            transpiler.transpile_string("test.py", main_code)

            if extra_code:
                for name, code in extra_code.items():
                    transpiler.transpile_string("%s.py" % name.replace('.', os.path.sep), adjust(code))

        transpiler.write(self.temp_dir)

        if args is None:
            args = []

        if len(args) == 0:
            # encode to turn str into bytes-like object
            self.jvm.stdin.write(("python.test.__init__\n").encode("utf-8"))
            self.jvm.stdin.flush()

            out = ""
            while True:
                try:
                    line = self.jvm.stdout.readline().decode("utf-8")
                    if line == ".{0}".format(os.linesep):
                        break
                    else:
                        out += line
                except IOError as e:
                    continue
        else:
            classpath = os.pathsep.join([
                os.path.join('..', 'dist', 'python-java-support.jar'),
                os.path.join('..', 'java'),
                os.curdir,
            ])
            proc = subprocess.Popen(
                ["java", "-classpath", classpath, "python.test.__init__"] + args,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
                cwd=self.temp_dir
            )
            out = proc.communicate()[0].decode('utf8')

        return out
Run a block of Python code as a Java program.
Run a block of Python code as a Java program.
Run a block of Python code as a Java program .
def runAsJava(self, main_code, extra_code=None, args=None):
        transpiler = Transpiler(verbosity=0)
        with capture_output(redirect_stderr=False):
            transpiler.transpile_string("test.py", main_code)
            if extra_code:
                for name, code in extra_code.items():
                    transpiler.transpile_string("%s.py" % name.replace('.', os.path.sep), adjust(code))
        transpiler.write(self.temp_dir)
        if args is None:
            args = []
        if len(args) == 0:
            self.jvm.stdin.write(("python.test.__init__\n").encode("utf-8"))
            self.jvm.stdin.flush()
            out = ""
            while True:
                try:
                    line = self.jvm.stdout.readline().decode("utf-8")
                    if line == ".{0}".format(os.linesep):
                        break
                    else:
                        out += line
                except IOError as e:
                    continue
        else:
            classpath = os.pathsep.join([
                os.path.join('..', 'dist', 'python-java-support.jar'),
                os.path.join('..', 'java'),
                os.curdir,
            ])
            proc = subprocess.Popen(
                ["java", "-classpath", classpath, "python.test.__init__"] + args,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
                cwd=self.temp_dir
            )
            out = proc.communicate()[0].decode('utf8')
        return out
def runAsJava ( self , main_code , extra_code = None , args = None ) : transpiler = Transpiler ( verbosity = 0 ) with capture_output ( redirect_stderr = False ) : transpiler . transpile_string ( "test.py" , main_code ) if extra_code : for name , code in extra_code . items ( ) : transpiler . transpile_string ( "%s.py" % name . replace ( '.' , os . path . sep ) , adjust ( code ) ) transpiler . write ( self . temp_dir ) if args is None : args = [ ] if len ( args ) == 0 : self . jvm . stdin . write ( ( "python.test.__init__\n" ) . encode ( "utf-8" ) ) self . jvm . stdin . flush ( ) out = "" while True : try : line = self . jvm . stdout . readline ( ) . decode ( "utf-8" ) if line == ".{0}" . format ( os . linesep ) : break else : out += line except IOError as e : continue else : classpath = os . pathsep . join ( [ os . path . join ( '..' , 'dist' , 'python-java-support.jar' ) , os . path . join ( '..' , 'java' ) , os . curdir , ] ) proc = subprocess . Popen ( [ "java" , "-classpath" , classpath , "python.test.__init__" ] + args , stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = subprocess . STDOUT , cwd = self . temp_dir ) out = proc . communicate ( ) [ 0 ] . decode ( 'utf8' ) return out
Run a block of Python code as a Java program.
Run a block of Python code as a Java program .
"""Run a block of Python code as a Java program.""" # Output source code into test directory # Don't redirect stderr; we want to see any errors from the transpiler # as top level test failures. # encode to turn str into bytes-like object
self
main_code
extra_code
args