Raise KeyError in prob functions for undefined functions/categories

This commit is contained in:
John J. Camilleri
2021-09-27 14:33:14 +02:00
parent 19251e5e61
commit 7dafeee57b
2 changed files with 21 additions and 11 deletions

View File

@@ -295,7 +295,12 @@ PGF_categoryProbability(PGFObject *self, PyObject *args)
return NULL;
}
return PyFloat_FromDouble((double)prob);
double dprob = (double) prob;
if (dprob == INFINITY) {
PyErr_Format(PyExc_KeyError, "category '%s' is not defined", s);
return NULL;
}
return PyFloat_FromDouble(dprob);
}
static PyObject *
@@ -315,7 +320,12 @@ PGF_functionProbability(PGFObject *self, PyObject *args)
return NULL;
}
return PyFloat_FromDouble((double)prob);
double dprob = (double) prob;
if (dprob == INFINITY) {
PyErr_Format(PyExc_KeyError, "function '%s' is not defined", s);
return NULL;
}
return PyFloat_FromDouble(dprob);
}
static PyObject *
@@ -331,7 +341,8 @@ PGF_exprProbability(PGFObject *self, PyObject *args)
return NULL;
}
return PyFloat_FromDouble((double)prob);
double dprob = (double) prob;
return PyFloat_FromDouble(dprob);
}
static PyGetSetDef PGF_getseters[] = {

View File

@@ -66,19 +66,18 @@ def test_original_function_type(gr1):
gr1.functionType("foo")
def test_original_function_prob(gr1):
# with pytest.raises(KeyError):
# gr1.functionProbability("foo")
assert gr1.functionProbability("foo") == float('inf')
with pytest.raises(KeyError):
gr1.functionProbability("foo")
# assert gr1.functionProbability("foo") == float('inf')
@pytest.mark.skip(reason="failing")
def test_original_category_prob(gr1):
# with pytest.raises(KeyError):
# gr1.categoryProbability("Q")
assert gr1.categoryProbability("Q") == float('inf')
with pytest.raises(KeyError):
gr1.categoryProbability("Q")
# assert gr1.categoryProbability("Q") == float('inf')
def test_original_expr_prob(gr1):
# with pytest.raises(KeyError):
# gr1.functionProbability("foo")
# gr1.exprProbability(ExprFun("foo"))
assert gr1.exprProbability(ExprFun("foo")) == float('inf')
# gr2