From a367d2790578a925d99c3f765e7bc44fb840cd7b Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Mon, 8 Mar 2021 10:40:38 -0600 Subject: [PATCH 1/2] Include error message in exception when openmc.run fails --- openmc/executor.py | 16 ++++++++++------ tests/unit_tests/test_source_file.py | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/openmc/executor.py b/openmc/executor.py index 8d208795c7..3a36772267 100644 --- a/openmc/executor.py +++ b/openmc/executor.py @@ -25,8 +25,12 @@ def _run(args, output, cwd): # Raise an exception if return status is non-zero if p.returncode != 0: - raise subprocess.CalledProcessError(p.returncode, ' '.join(args), - ''.join(lines)) + # Get error message from output and simplify whitespace + output = ''.join(lines) + _, _, error_msg = output.partition('ERROR: ') + error_msg = ' '.join(error_msg.split()) + + raise RuntimeError(error_msg) def plot_geometry(output=True, openmc_exec='openmc', cwd='.'): @@ -43,7 +47,7 @@ def plot_geometry(output=True, openmc_exec='openmc', cwd='.'): Raises ------ - subprocess.CalledProcessError + RuntimeError If the `openmc` executable returns a non-zero status """ @@ -70,7 +74,7 @@ def plot_inline(plots, openmc_exec='openmc', cwd='.', convert_exec='convert'): Raises ------ - subprocess.CalledProcessError + RuntimeError If the `openmc` executable returns a non-zero status """ @@ -133,7 +137,7 @@ def calculate_volumes(threads=None, output=True, cwd='.', Raises ------ - subprocess.CalledProcessError + RuntimeError If the `openmc` executable returns a non-zero status See Also @@ -188,7 +192,7 @@ def run(particles=None, threads=None, geometry_debug=False, Raises ------ - subprocess.CalledProcessError + RuntimeError If the `openmc` executable returns a non-zero status """ diff --git a/tests/unit_tests/test_source_file.py b/tests/unit_tests/test_source_file.py index 70aa1be4dd..8166f57126 100644 --- a/tests/unit_tests/test_source_file.py +++ b/tests/unit_tests/test_source_file.py @@ -73,6 +73,6 @@ def test_wrong_source_attributes(run_in_tmpdir): # When we run the model, it should error out with a message that includes # the names of the wrong attributes - with pytest.raises(subprocess.CalledProcessError) as excinfo: + with pytest.raises(RuntimeError) as excinfo: openmc.run() - assert 'platypus, axolotl, narwhal' in excinfo.value.output + assert 'platypus, axolotl, narwhal' in str(excinfo.value) From d19512b454765518af3c6ae0c89828da029f274d Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Tue, 9 Mar 2021 13:31:53 -0600 Subject: [PATCH 2/2] Account for exceptions/segfaults in _run function --- openmc/executor.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/openmc/executor.py b/openmc/executor.py index 3a36772267..008290c181 100644 --- a/openmc/executor.py +++ b/openmc/executor.py @@ -27,7 +27,12 @@ def _run(args, output, cwd): if p.returncode != 0: # Get error message from output and simplify whitespace output = ''.join(lines) - _, _, error_msg = output.partition('ERROR: ') + if 'ERROR: ' in output: + _, _, error_msg = output.partition('ERROR: ') + elif 'what()' in output: + _, _, error_msg = output.partition('what(): ') + else: + error_msg = 'OpenMC aborted unexpectedly.' error_msg = ' '.join(error_msg.split()) raise RuntimeError(error_msg)