From e0f988e1416dc777db8f8c007edd599b504325bf Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Fri, 20 Oct 2023 15:54:31 +0200 Subject: [PATCH] add test to trigger C++->Python AssertionError --- efel/cppcore/cfeature.cpp | 5 +++-- efel/cppcore/cppcore.cpp | 19 ++++++------------- tests/test_cppcore.py | 12 ++++++++++++ 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/efel/cppcore/cfeature.cpp b/efel/cppcore/cfeature.cpp index 12cc528e..1ec75bfb 100644 --- a/efel/cppcore/cfeature.cpp +++ b/efel/cppcore/cfeature.cpp @@ -627,10 +627,11 @@ string cFeature::featuretype(string featurename) { if (npos != string::npos) { featurename = featurename.substr(0, npos); } + if (featurename == "__test_efel_assertion__") // for testing only + throw EfelAssertionError("Test efel assertion is successfully triggered."); string type = featuretypes[featurename]; - if (type != "int" && type != "double") { + if (type != "int" && type != "double") throw std::runtime_error("Unknown feature name: " + featurename); - } return type; } diff --git a/efel/cppcore/cppcore.cpp b/efel/cppcore/cppcore.cpp index 196057d4..938a0646 100644 --- a/efel/cppcore/cppcore.cpp +++ b/efel/cppcore/cppcore.cpp @@ -114,25 +114,18 @@ _getfeature(PyObject* self, PyObject* args, const string &input_type) { int return_value; if (!PyArg_ParseTuple(args, "sO!", &feature_name, &PyList_Type, &py_values)) { + PyErr_SetString(PyExc_TypeError, "Unexpected argument type provided."); return NULL; } - string feature_type; try { - feature_type = pFeature->featuretype(string(feature_name)); - } - catch(const std::runtime_error& e) - { - PyErr_SetString(PyExc_RuntimeError, e.what()); - return NULL; - } + string feature_type = pFeature->featuretype(string(feature_name)); - if (!input_type.empty() && feature_type != input_type){ // when types do not match - PyErr_SetString(PyExc_TypeError, "Feature type does not match"); - return NULL; - } + if (!input_type.empty() && feature_type != input_type){ // when types do not match + PyErr_SetString(PyExc_TypeError, "Feature type does not match"); + return NULL; + } - try { if (feature_type == "int") { vector values; return_value = pFeature->getFeature(string(feature_name), values); diff --git a/tests/test_cppcore.py b/tests/test_cppcore.py index fac793d2..19ad2a49 100644 --- a/tests/test_cppcore.py +++ b/tests/test_cppcore.py @@ -209,3 +209,15 @@ def test_caching(self, feature_name): assert contents.count(f"Calculated feature {feature_name}") == 1 # make sure Reusing computed value of text occurs twice assert contents.count(f"Reusing computed value of {feature_name}") == 2 + + +def test_efel_assertion_error(): + """Testing if c++ assertion error is propagated to python acorrectly.""" + import efel + efel.reset() + trace = { + "stim_start": [25], + "stim_end": [75], + } + with pytest.raises(AssertionError): + efel.getFeatureValues([trace], ["__test_efel_assertion__"])