Skip to content

Commit

Permalink
fixed a bug where raising an exception causes a crash (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
bab2min committed Jan 22, 2023
1 parent dca6d04 commit c13c651
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/python/PyUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,7 @@ namespace py
{
PyObject* exc, * val, * tb, * val2;
PyErr_Fetch(&exc, &val, &tb);
PyErr_NormalizeException(&exc, &val, &tb);
if (tb)
{
PyException_SetTraceback(val, tb);
Expand All @@ -1321,7 +1322,9 @@ namespace py
Py_DECREF(exc);
PyObject* et = e.pytype();
val2 = PyObject_CallFunctionObjArgs(et, py::UniqueObj{ buildPyValue(e.what()) }.get(), nullptr);
Py_INCREF(val);
PyException_SetCause(val2, val);
PyException_SetContext(val2, val);
PyErr_SetObject(et, val2);
Py_DECREF(val2);
}
Expand Down Expand Up @@ -1355,6 +1358,7 @@ namespace py
{
PyObject* exc, * val, * tb, * val2;
PyErr_Fetch(&exc, &val, &tb);
PyErr_NormalizeException(&exc, &val, &tb);
if (tb)
{
PyException_SetTraceback(val, tb);
Expand All @@ -1363,7 +1367,9 @@ namespace py
Py_DECREF(exc);
PyObject* et = e.pytype();
val2 = PyObject_CallFunctionObjArgs(et, py::UniqueObj{ buildPyValue(e.what()) }.get(), nullptr);
Py_INCREF(val);
PyException_SetCause(val2, val);
PyException_SetContext(val2, val);
PyErr_SetObject(et, val2);
Py_DECREF(val2);
}
Expand Down
9 changes: 9 additions & 0 deletions test/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ def infer_together(cls, inputFile, mdFields, f, kargs, ps):

mdl.infer(unseen_docs, parallel=ps, together=True)

def test_issue_187_crash_exc():
mdl = tp.LDAModel(k=10)
exc = None
try:
mdl.add_doc([[0, 1]])
except Exception as e:
exc = e
assert isinstance(exc, ValueError)

def train_raw_corpus(cls, inputFile, mdFields, f, kargs, ps):
print('Test train with raw corpus')
from nltk.stem.porter import PorterStemmer
Expand Down

0 comments on commit c13c651

Please sign in to comment.