Beispiel #1
0
def runAsJava(test_dir, main_code, extra_code=None):
    """Run a block of Python code as a Java program."""
    # Output source code into test directory
    transpiler = Transpiler()

    # 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", adjust(main_code))

        if extra_code:
            for name, code in extra_code.items():
                transpiler.transpile_string("%s.py" % name, adjust(code))

    transpiler.write(test_dir, verbosity=0)

    proc = subprocess.Popen(
        [
            "java", "-classpath", "../../dist/python-java.jar:.",
            "-XX:-UseSplitVerifier", "python.test"
        ],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        cwd=test_dir,
    )
    out = proc.communicate()

    return out[0].decode('utf8')
Beispiel #2
0
    def runAsJava(self,
                  main_code,
                  extra_code=None,
                  run_in_function=False,
                  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", adjust(main_code, run_in_function=run_in_function))

            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
Beispiel #3
0
def runAsJava(test_dir, main_code, extra_code=None, run_in_function=False, 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", adjust(main_code, run_in_function=run_in_function))

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

    transpiler.write(test_dir)

    if args is None:
        args = []

    proc = subprocess.Popen(
        ["java", "-classpath", "../../dist/python-java.jar:../java:.", "python.test.__init__"] + args,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        cwd=test_dir,
    )
    out = proc.communicate()

    return out[0].decode('utf8')
Beispiel #4
0
def runAsJava(test_dir, main_code, extra_code=None):
    """Run a block of Python code as a Java program."""
    # Output source code into test directory
    transpiler = Transpiler()

    # 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", adjust(main_code))

        if extra_code:
            for name, code in extra_code.items():
                transpiler.transpile_string("%s.py" % name, adjust(code))

    transpiler.write(test_dir, verbosity=0)

    proc = subprocess.Popen(
        ["java", "-classpath", "../../dist/python-java.jar:.", "-XX:-UseSplitVerifier", "python.test"],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        cwd=test_dir,
    )
    out = proc.communicate()

    return out[0].decode('utf8')
Beispiel #5
0
def runAsJava(test_dir, main_code, extra_code=None, run_in_function=False, 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", adjust(main_code, run_in_function=run_in_function))

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

    transpiler.write(test_dir)

    if args is None:
        args = []

    if len(args) == 0:
        global _jvm

        # encode to turn str into bytes-like object
        _jvm.stdin.write(("python.test.__init__\n").encode("utf-8"))
        _jvm.stdin.flush()
        out = ""
        while True:
            try:
                line = _jvm.stdout.readline().decode("utf-8")
                if line == ".{0}".format(os.linesep):
                    break
                else:
                    out += line
            except IOError:
                continue
    else:
        classpath = os.pathsep.join([
            os.path.join('..', '..', 'dist', 'python-java.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=test_dir
        )
        out = proc.communicate()[0].decode('utf8')

    return out
Beispiel #6
0
def runAsJava(test_dir,
              main_code,
              extra_code=None,
              run_in_function=False,
              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", adjust(main_code, run_in_function=run_in_function))

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

    transpiler.write(test_dir)

    if args is None:
        args = []

    proc = subprocess.Popen(
        [
            "java", "-classpath", "../../dist/python-java.jar:../java:.",
            "python.test.__init__"
        ] + args,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        cwd=test_dir,
    )
    out = proc.communicate()

    return out[0].decode('utf8')
Beispiel #7
0
def runAsJava(main_code, **extra):
    """Run a block of Python code as a Java program."""

    transpiler = Transpiler("org.pybee")
    with capture_output():
        transpiler.transpile_string("test.py", main_code)

        for name, code in extra.items():
            transpiler.transpile_string("%s.py" % name, code)

    transpiler.write(os.path.dirname(__file__), verbosity=0)

    proc = subprocess.Popen(
        ["java", "-classpath", "../python.jar:.", "-XX:-UseSplitVerifier", "org.pybee.test"],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        cwd=os.path.dirname(__file__),
    )
    out = proc.communicate()
    return out[0].decode("utf8").replace("\t", "    ")
Beispiel #8
0
    def runAsJava(self, main_code, extra_code=None, args=None, timed=False):
        """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 = []

        t1_start = time.perf_counter()
        t2_start = time.process_time()

        if len(args) == 0:

            # encode to turn str into bytes-like object
            self.jvm.stdin.write(("python.test\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

            t1_stop = time.perf_counter()
            t2_stop = time.process_time()

        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"] + args,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
                cwd=self.temp_dir
            )

            t1_stop = time.perf_counter()
            t2_stop = time.process_time()

            out = proc.communicate()[0].decode('utf8')

            if proc.returncode != 0:
                raise Exception(
                    "Java subprocess didn't exit cleanly (exit status %s)\n\n: %s" % (
                        proc.returncode, out
                    )
                )

        if timed:
            print("  Elapsed time: ", (t1_stop-t1_start), " sec")
            print("  CPU process time: ", (t2_stop-t2_start), " sec")
            
        return out
Beispiel #9
0
 def test_block_large_code(self):
     "Test exception for large code of an anonymous block."
     large_code = 'print(1 + 2)\n' * 3000
     transpiler = Transpiler(verbosity=0)
     self.assertRaises(BlockCodeTooLarge, transpiler.transpile_string,
                       "test.py", large_code)
Beispiel #10
0
 def test_method_large_code(self):
     "Test exception for large code of a method."
     large_code = 'def test():\n' + '    print(1 + 2)\n' * 3000
     transpiler = Transpiler(verbosity=0)
     self.assertRaises(MethodCodeTooLarge, transpiler.transpile_string,
                       "test.py", large_code)