From 485c16a33e36efc1ebad476a3b7429ed8100e3ae Mon Sep 17 00:00:00 2001 From: Hartmut Kaiser Date: Fri, 15 May 2020 11:55:28 -0500 Subject: [PATCH] Adding option to print full error information for HPX exceptions - add PHYLANX_WITH_PYTHON_TESTS_EMIT_FULL_DIAGNOSTICS cmake variable - enable this option for CircleCI - adding ability to add to the Phylanx session configuration through PHYLANX_PYTHON_CONFIG environment variable - adding --hpx:ini=phylanx.full_error_diagnostics!=1 to enable full error diagnostics from Python code --- .circleci/config.yml | 3 ++- CMakeLists.txt | 5 +++++ cmake/Phylanx_AddPythonTest.cmake | 9 +++++++-- python/phylanx/core/config.py | 14 ++++++++++++++ python/src/bindings/execution_tree.cpp | 25 +++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2b6a32f7a..7582d0434 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -153,7 +153,8 @@ jobs: -DPHYLANX_WITH_ITERATIVE_SOLVERS=ON \ -DPHYLANX_WITH_DOCUMENTATION=ON \ -DPHYLANX_WITH_HIGHFIVE=OFF \ - -DPHYLANX_WITH_CXX17=ON + -DPHYLANX_WITH_CXX17=ON \ + -DPHYLANX_WITH_PYTHON_TESTS_EMIT_FULL_DIAGNOSTICS=On - persist_to_workspace: root: / paths: diff --git a/CMakeLists.txt b/CMakeLists.txt index ad712b2c6..f7bbb36ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,11 @@ phylanx_option( "Enable or disable the compilation of benchmark tests" ON ADVANCED CATEGORY "Build") +phylanx_option( + PHYLANX_WITH_PYTHON_TESTS_EMIT_FULL_DIAGNOSTICS BOOL + "Python tests emit the full HPX diagnostics if exceptions are thrown" + OFF ADVANCED CATEGORY "Build") + phylanx_option( PHYLANX_WITH_TESTS_REGRESSIONS BOOL "Enable or disable the compilation of regression tests" diff --git a/cmake/Phylanx_AddPythonTest.cmake b/cmake/Phylanx_AddPythonTest.cmake index a23128622..e611054a7 100644 --- a/cmake/Phylanx_AddPythonTest.cmake +++ b/cmake/Phylanx_AddPythonTest.cmake @@ -30,11 +30,16 @@ macro(add_phylanx_python_test category name) phylanx_debug("add_phylanx_python_test (${name})" "WORKING_DIRECTORY: ${_working_directory}") set(_environment) + if(PHYLANX_WITH_PYTHON_TESTS_EMIT_FULL_DIAGNOSTICS) + set(_environment + PHYLANX_PYTHON_CONFIG="--hpx:ini=phylanx.full_error_diagnostics!=1" + ) + endif() if(${name}_ENVIRONMENT) if(MSVC) - set(_environment ${CMAKE_COMMAND} -E env "${${name}_ENVIRONMENT}") + set(_environment ${CMAKE_COMMAND} -E env ${_environment} ${${name}_ENVIRONMENT}) else() - set(_environment ENVIRONMENT "${${name}_ENVIRONMENT}") + set(_environment ENVIRONMENT ${_environment} ${${name}_ENVIRONMENT}) endif() endif() phylanx_debug("add_phylanx_python_test (${name})" "ENVIRONMENT: ${_environment}") diff --git a/python/phylanx/core/config.py b/python/phylanx/core/config.py index 740c35624..81fc64c7b 100644 --- a/python/phylanx/core/config.py +++ b/python/phylanx/core/config.py @@ -6,6 +6,9 @@ from phylanx.core import init_hpx_runtime from phylanx.exceptions import RuntimeAlreadyInitializedError +import os +import platform + class PhylanxSession: cfg = [ @@ -30,6 +33,17 @@ def init(num_threads=1): if not PhylanxSession.is_initialized: hpx_thread = "hpx.os_threads!=%s" % num_threads PhylanxSession.cfg[3] = hpx_thread + + # append additional config settings taken from the environment + if 'PHYLANX_PYTHON_CONFIG' in os.environ: + if platform.system() == "Windows": + sep = ';' + else: + sep = ':' + + for e in os.environ['PHYLANX_PYTHON_CONFIG'].split(sep): + PhylanxSession.cfg.append(e) + init_hpx_runtime(PhylanxSession.cfg) PhylanxSession.is_initialized = True else: diff --git a/python/src/bindings/execution_tree.cpp b/python/src/bindings/execution_tree.cpp index 1a442d5e5..e258c9418 100644 --- a/python/src/bindings/execution_tree.cpp +++ b/python/src/bindings/execution_tree.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -307,4 +308,28 @@ void phylanx::bindings::bind_execution_tree(pybind11::module m) }); }, "wait for future to become ready"); + + // translate HPX exceptions and provide more error information, if desired + static pybind11::exception exc( + m, "HPXError", PyExc_RuntimeError); + + pybind11::register_exception_translator([](std::exception_ptr p) { + try + { + if (p) + std::rethrow_exception(p); + } + catch (hpx::exception& e) + { + if (hpx::get_config_entry("phylanx.full_error_diagnostics", "0") == + "1") + { + exc(hpx::diagnostic_information(e).c_str()); + } + else + { + exc(e.what()); + } + } + }); }