Skip to content

Commit a83dd1e

Browse files
committed
propagate java assertions in tests when started via binary executable
1 parent e55ee90 commit a83dd1e

File tree

4 files changed

+51
-27
lines changed

4 files changed

+51
-27
lines changed

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,15 @@ protected List<String> preprocessArguments(List<String> givenArgs, Map<String, S
331331
}
332332
}
333333

334+
if (!ImageInfo.inImageCode()) {
335+
for (String arg : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
336+
if (arg.equals("-ea")) {
337+
addRelaunchArg("--vm.ea");
338+
break;
339+
}
340+
}
341+
}
342+
334343
// According to CPython if no arguments are given, they contain an empty string.
335344
if (programArgs.isEmpty()) {
336345
programArgs.add("");

graalpython/com.oracle.graal.python.test/src/tests/test_subprocess.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2021, Oracle and/or its affiliates.
1+
# Copyright (c) 2018, 2022, Oracle and/or its affiliates.
22
# Copyright (C) 1996-2017 Python Software Foundation
33
#
44
# Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -126,3 +126,13 @@ def test_kill(self):
126126
finally:
127127
p.kill()
128128
p.wait()
129+
130+
def test_java_asserts(self):
131+
import sys
132+
if sys.implementation.name == "graalpy":
133+
import subprocess, __graalpython__
134+
if __graalpython__.java_assert():
135+
result = subprocess.run([sys.executable, "-c", "import __graalpython__; __graalpython__.java_assert()"])
136+
else:
137+
result = subprocess.run([sys.executable, "-c", "import __graalpython__; not __graalpython__.java_assert()"])
138+
assert result.returncode == 0

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,4 +712,15 @@ TruffleString doit(VirtualFrame frame,
712712
return fromJavaStringNode.execute(tempFile.getPath(), TS_ENCODING);
713713
}
714714
}
715+
716+
@Builtin(name = "java_assert", minNumOfPositionalArgs = 0)
717+
@GenerateNodeFactory
718+
abstract static class JavaAssertNode extends PythonBuiltinNode {
719+
@Specialization
720+
Object doit() {
721+
boolean assertOn = false;
722+
assert assertOn = true;
723+
return assertOn;
724+
}
725+
}
715726
}

mx.graalpython/mx_graalpython.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def run_cpython_test(args):
311311
env = os.environ.copy()
312312
delete_bad_env_keys(env)
313313
with _dev_pythonhome_context():
314-
mx.run([python_gvm_with_assertions()] + interp_args + [
314+
mx.run([python_gvm()] + ['--vm.ea'] + interp_args + [
315315
os.path.join(SUITE.dir, "graalpython/com.oracle.graal.python.test/src/tests/run_cpython_test.py"),
316316
] + test_args + testfiles, env=env)
317317

@@ -342,7 +342,9 @@ def retag_unittests(args):
342342
'graalpython/com.oracle.graal.python.test/src/tests/test_tagged_unittests.py',
343343
'--retag'
344344
]
345-
vm = python_svm() if not parsed_args.jvm else python_gvm_with_assertions()
345+
vm = python_svm() if not parsed_args.jvm else python_gvm()
346+
if parsed_args.jvm:
347+
args += ['-ea']
346348
mx.run([vm] + args + remaining_args, env=env)
347349
if parsed_args.upload_results_to:
348350
with tempfile.TemporaryDirectory(prefix='graalpython-retagger-') as d:
@@ -590,18 +592,6 @@ def python_enterprise_gvm(_=None):
590592
mx.log(launcher)
591593
return launcher
592594

593-
def python_gvm_with_assertions():
594-
launcher = python_gvm()
595-
patch_batch_launcher(launcher, '-ea')
596-
return launcher
597-
598-
599-
def python_managed_gvm_with_assertions():
600-
launcher = python_managed_gvm()
601-
patch_batch_launcher(launcher, '-ea')
602-
return launcher
603-
604-
605595
def python_svm(_=None):
606596
home = _graalvm_home(envfile="graalpython-launcher")
607597
launcher = _join_bin(home, "graalpy")
@@ -695,7 +685,7 @@ def is_included(path):
695685

696686

697687
def run_python_unittests(python_binary, args=None, paths=None, aot_compatible=False, exclude=None, env=None,
698-
use_pytest=False, cwd=None, lock=None, out=None, err=None, nonZeroIsFatal=True):
688+
use_pytest=False, cwd=None, lock=None, out=None, err=None, nonZeroIsFatal=True, javaAsserts=False):
699689
if lock:
700690
lock.acquire()
701691
# ensure that the test distribution is up-to-date
@@ -759,6 +749,9 @@ def graalvm_vm_arg(java_arg):
759749
mx.run([python_binary, "--jvm", agent_args] + args + [testfile],
760750
nonZeroIsFatal=nonZeroIsFatal, env=env, cwd=cwd, out=out, err=err)
761751
else:
752+
if javaAsserts:
753+
args += ['-ea']
754+
762755
args += testfiles
763756
mx.logv(" ".join([python_binary] + args))
764757
if lock:
@@ -838,7 +831,7 @@ def run(self):
838831
mx.abort("At least one HPy testing thread failed.")
839832

840833

841-
def run_tagged_unittests(python_binary, env=None, cwd=None):
834+
def run_tagged_unittests(python_binary, env=None, cwd=None, javaAsserts=False):
842835
if env is None:
843836
env = os.environ
844837
sub_env = env.copy()
@@ -852,7 +845,8 @@ def run_tagged_unittests(python_binary, env=None, cwd=None):
852845
args=["-v"],
853846
paths=["test_tagged_unittests.py"],
854847
env=sub_env,
855-
cwd=cwd
848+
cwd=cwd,
849+
javaAsserts=javaAsserts
856850
)
857851

858852

@@ -877,19 +871,19 @@ def graalpython_gate_runner(args, tasks):
877871
test_args = [exe, _graalpytest_driver(), "-v", _graalpytest_root()]
878872
mx.run(test_args, nonZeroIsFatal=True, env=env)
879873
mx.run(["env"])
880-
run_python_unittests(python_gvm_with_assertions())
874+
run_python_unittests(python_gvm(), javaAsserts=True)
881875

882876
with Task('GraalPython sandboxed tests', tasks, tags=[GraalPythonTags.unittest_sandboxed]) as task:
883877
if task:
884-
run_python_unittests(python_managed_gvm_with_assertions())
878+
run_python_unittests(python_managed_gvm(), javaAsserts=True)
885879

886880
with Task('GraalPython multi-context unittests', tasks, tags=[GraalPythonTags.unittest_multi]) as task:
887881
if task:
888-
run_python_unittests(python_gvm_with_assertions(), args=["-multi-context"])
882+
run_python_unittests(python_gvm(), args=["-multi-context"], javaAsserts=True)
889883

890884
with Task('GraalPython Jython emulation tests', tasks, tags=[GraalPythonTags.unittest_jython]) as task:
891885
if task:
892-
run_python_unittests(python_gvm_with_assertions(), args=["--python.EmulateJython"], paths=["test_interop.py"])
886+
run_python_unittests(python_gvm(), args=["--python.EmulateJython"], paths=["test_interop.py"], javaAsserts=True)
893887

894888
with Task('GraalPython HPy tests', tasks, tags=[GraalPythonTags.unittest_hpy]) as task:
895889
if task:
@@ -901,8 +895,8 @@ def graalpython_gate_runner(args, tasks):
901895

902896
with Task('GraalPython posix module tests', tasks, tags=[GraalPythonTags.unittest_posix]) as task:
903897
if task:
904-
run_python_unittests(python_gvm_with_assertions(), args=["--PosixModuleBackend=native"], paths=["test_posix.py", "test_mmap.py"])
905-
run_python_unittests(python_gvm_with_assertions(), args=["--PosixModuleBackend=java"], paths=["test_posix.py", "test_mmap.py"])
898+
run_python_unittests(python_gvm(), args=["--PosixModuleBackend=native"], paths=["test_posix.py", "test_mmap.py"], javaAsserts=True)
899+
run_python_unittests(python_gvm(), args=["--PosixModuleBackend=java"], paths=["test_posix.py", "test_mmap.py"], javaAsserts=True)
906900

907901
with Task('GraalPython Python tests', tasks, tags=[GraalPythonTags.tagged]) as task:
908902
if task:
@@ -1813,7 +1807,7 @@ def python_coverage(args):
18131807
'coverage-upload',
18141808
])
18151809
if args.truffle:
1816-
executable = python_gvm_with_assertions()
1810+
executable = python_gvm()
18171811
variants = [
18181812
{"args": []},
18191813
{"args": ["--python.EmulateJython"], "paths": ["test_interop.py"]},
@@ -1843,9 +1837,9 @@ def python_coverage(args):
18431837
env['GRAAL_PYTHON_ARGS'] = " ".join(extra_args)
18441838
env['ENABLE_THREADED_GRAALPYTEST'] = "false"
18451839
if kwds.pop("tagged", False):
1846-
run_tagged_unittests(executable, env=env)
1840+
run_tagged_unittests(executable, env=env, javaAsserts=True)
18471841
else:
1848-
run_python_unittests(executable, env=env, **kwds)
1842+
run_python_unittests(executable, env=env, javaAsserts=True, **kwds)
18491843

18501844
# generate a synthetic lcov file that includes all sources with 0
18511845
# coverage. this is to ensure all sources actuall show up - otherwise,

0 commit comments

Comments
 (0)