Skip to content

Commit 2db020f

Browse files
committed
[GR-10672] Immediately print exceptions during unit test setup.
PullRequest: graalpython/100
2 parents 045d78b + 416f3b9 commit 2db020f

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

graalpython/com.oracle.graal.python.test/src/graalpytest.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@
3939
import _io
4040
import sys
4141

42-
4342
os = sys.modules.get("posix", sys.modules.get("nt", None))
4443
if os is None:
4544
raise ImportError("posix or nt module is required in builtin modules")
4645

47-
4846
FAIL = '\033[91m'
4947
ENDC = '\033[0m'
5048
BOLD = '\033[1m'
@@ -57,12 +55,13 @@ class SkipTest(BaseException):
5755

5856

5957
class TestCase(object):
58+
6059
def __init__(self):
6160
self.exceptions = []
6261
self.passed = 0
6362
self.failed = 0
6463

65-
def run_safely(self, func):
64+
def run_safely(self, func, print_immediately=False):
6665
if verbose:
6766
print(u"\n\t\u21B3 ", func.__name__, " ", end="")
6867
try:
@@ -71,6 +70,8 @@ def run_safely(self, func):
7170
if isinstance(e, SkipTest):
7271
print("Skipped: %s" % e)
7372
else:
73+
if print_immediately:
74+
print("Exception during setup occurred: %s\n" % e)
7475
code = func.__code__
7576
self.exceptions.append(
7677
("%s:%d (%s)" % (code.co_filename, code.co_firstlineno, func), e)
@@ -95,7 +96,7 @@ def success(self):
9596

9697
def failure(self):
9798
self.failed += 1
98-
fail_msg = FAIL+BOLD+"F"+ENDC if verbose else "F"
99+
fail_msg = FAIL + BOLD + "F" + ENDC if verbose else "F"
99100
print(fail_msg, end="", flush=True)
100101

101102
def assertTrue(self, value, msg=""):
@@ -141,6 +142,7 @@ def assertSequenceEqual(self, expected, actual, msg=None):
141142
assert expected_value == next(actual_iter), msg
142143

143144
class assertRaises():
145+
144146
def __init__(self, exc_type, function=None, *args, **kwargs):
145147
if function is None:
146148
self.exc_type = exc_type
@@ -177,9 +179,9 @@ def run(cls, items=None):
177179
break
178180
items += typ.__dict__.items()
179181
if hasattr(instance, "setUp"):
180-
if not instance.run_safely(instance.setUp):
182+
if not instance.run_safely(instance.setUp, print_immediately=True):
181183
return instance
182-
for k,v in items:
184+
for k, v in items:
183185
if k.startswith("test"):
184186
if patterns:
185187
if not any(p in k for p in patterns):
@@ -198,6 +200,7 @@ class ThisTestCase(cls, TestCase): pass
198200

199201

200202
class TestRunner(object):
203+
201204
def __init__(self, paths):
202205
self.testfiles = TestRunner.find_testfiles(paths)
203206
self.exceptions = []
@@ -255,7 +258,7 @@ def run(self):
255258
print(u"\n\u25B9 ", module.__name__, end="")
256259
# some tests can modify the global scope leading to a RuntimeError: test_scope.test_nesting_plus_free_ref_to_global
257260
module_dict = dict(module.__dict__)
258-
for k,v in module_dict.items():
261+
for k, v in module_dict.items():
259262
if (k.startswith("Test") or k.endswith("Test") or k.endswith("Tests")) and isinstance(v, type):
260263
testcase = TestCase.runClass(v)
261264
else:

graalpython/com.oracle.graal.python.test/src/tests/cpyext/__init__.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ def setUp(self):
7070
def ccompile(self, name):
7171
from distutils.core import setup, Extension
7272
source_file = '%s/%s.c' % (__dir__, name)
73+
74+
try:
75+
stat_result = os.stat(source_file)
76+
if stat_result[6] == 0:
77+
raise SystemError("empty source file %s" % (source_file,))
78+
except FileNotFoundError:
79+
raise SystemError("source file %s not available" % (source_file,))
80+
7381
module = Extension(name, sources=[source_file])
7482
args = ['--quiet', 'build', 'install_lib', '-f', '--install-dir=%s' % __dir__]
7583
setup(
@@ -81,12 +89,25 @@ def ccompile(self, name):
8189
ext_modules=[module]
8290
)
8391
# ensure file was really written
92+
binary_file_llvm = '%s/%s.bc' % (__dir__, name)
93+
binary_file_gcc = '%s/%s.so' % (__dir__, name)
94+
95+
tries = 0
96+
while tries < 3 and not file_not_empty(binary_file_llvm) and not file_not_empty(binary_file_gcc):
97+
tries += 1
98+
99+
if tries >= 3:
100+
raise SystemError("binary file %s/%s.(bc|so) not available" % (__dir__, name))
101+
102+
103+
def file_not_empty(path):
84104
try:
85-
stat_result = os.stat(source_file)
105+
stat_result = os.stat(path)
86106
if stat_result[6] == 0:
87-
raise SystemError("empty source file %s" % (source_file,))
107+
return False
88108
except FileNotFoundError:
89-
raise SystemError("source file %s not available" % (source_file,))
109+
return False
110+
return True
90111

91112

92113
c_template = """
@@ -502,11 +523,21 @@ def CPyExtType(name, code, **kwargs):
502523
kwargs.setdefault("ready_code", "")
503524
c_source = UnseenFormatter().format(template, **kwargs)
504525

505-
with open("%s/%s.c" % (__dir__, name), "wb", buffering=0) as f:
526+
source_file = "%s/%s.c" % (__dir__, name)
527+
with open(source_file, "wb", buffering=0) as f:
506528
if GRAALPYTHON:
507529
f.write(c_source)
508530
else:
509531
f.write(bytes(c_source, 'utf-8'))
532+
533+
# ensure file was really written
534+
try:
535+
stat_result = os.stat(source_file)
536+
if stat_result[6] == 0:
537+
raise SystemError("empty source file %s" % (source_file,))
538+
except FileNotFoundError:
539+
raise SystemError("source file %s not available" % (source_file,))
540+
510541
ccompile(None, name)
511542
sys.path.insert(0, __dir__)
512543
try:

mx.graalpython/mx_graalpython.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ def graalpython_gate_runner(args, tasks):
342342
with Task('GraalPython Python tests', tasks, tags=[GraalPythonTags.unittest]) as task:
343343
if task:
344344
test_args = [_graalpytest_driver, "-v", _test_project + "src/tests/"]
345-
mx.command_function("python")(test_args)
345+
mx.command_function("python")(["--python.CatchAllExceptions=true"] + test_args)
346346
if platform.system() != 'Darwin':
347347
# TODO: re-enable when python3 is available on darwin
348348
mx.log("Running tests with CPython")

0 commit comments

Comments
 (0)