Skip to content

Commit

Permalink
add test to trigger C++->Python AssertionError
Browse files Browse the repository at this point in the history
  • Loading branch information
anilbey committed Oct 20, 2023
1 parent fd033a2 commit e0f988e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
5 changes: 3 additions & 2 deletions efel/cppcore/cfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
19 changes: 6 additions & 13 deletions efel/cppcore/cppcore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> values;
return_value = pFeature->getFeature<int>(string(feature_name), values);
Expand Down
12 changes: 12 additions & 0 deletions tests/test_cppcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__"])

0 comments on commit e0f988e

Please sign in to comment.