Skip to content

Commit b038160

Browse files
author
debsankha manik
committed
in-python traceback generation and propagation working
1 parent 43e50e2 commit b038160

File tree

2 files changed

+10
-31
lines changed

2 files changed

+10
-31
lines changed

devs/adevs_python.hpp

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -199,45 +199,24 @@ class Atomic: public AtomicBase {
199199
PyObject *ptype, *pvalue, *ptraceback;
200200
PyObject *pystr, *pystr_unic;
201201

202+
std::string error_desc;
202203
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
203-
pystr = PyObject_Str(pvalue);
204-
pystr_unic = PyUnicode_AsEncodedString(pystr, "utf-8", "~E~");
205-
std::string error_desc {PyBytes_AsString(pystr_unic)};
206-
Py_XDECREF(pystr);
207-
Py_XDECREF(pystr_unic);
208-
209-
// try to get traceback as per https://stackoverflow.com/a/15907460
210-
211-
/* See if we can get a full traceback */
212-
std::string full_backtrace = "";
213-
PyObject *pyth_module, *pyth_func;
214-
pyth_module = PyImport_ImportModule("traceback");
215-
if (pyth_module == NULL) {
216-
full_backtrace = "";
204+
if (pvalue == NULL){
205+
error_desc = {"No information on the occured exception available"};
217206
}
218207
else {
208+
pystr = PyObject_Str(pvalue);
209+
pystr_unic = PyUnicode_AsEncodedString(pystr, "utf-8", "ignore");
210+
error_desc = {PyBytes_AsString(pystr_unic)};
219211

220-
pyth_func = PyObject_GetAttrString(pyth_module, "format_exception");
221-
if (pyth_func && PyCallable_Check(pyth_func)) {
222-
PyObject *pyth_val;
223-
224-
pyth_val = PyObject_CallFunctionObjArgs(pyth_func, ptype, pvalue, ptraceback, NULL);
225-
226-
pystr = PyObject_Str(pyth_val);
227-
pystr_unic = PyUnicode_AsEncodedString(pystr, "utf-8", "~E~");
228-
full_backtrace = {PyBytes_AsString(pystr_unic)};
229-
230-
Py_XDECREF(pyth_val);
231-
Py_XDECREF(pyth_func);
232-
Py_XDECREF(pystr);
212+
Py_XDECREF(pystr);
233213
Py_XDECREF(pystr_unic);
214+
}
234215
Py_XDECREF(ptype);
235216
Py_XDECREF(pvalue);
236217
Py_XDECREF(ptraceback);
237218

238-
}
239-
}
240-
return error_desc + full_backtrace;
219+
return "Python traceback follows:\n" + error_desc;
241220
}
242221

243222
private:

devs/devs.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ cdef void cy_delta_ext(
283283
atomic_base.delta_ext(e, list(input_bag))
284284
except Exception as ex:
285285
emsg = traceback.format_exception(type(ex), ex, ex.__traceback__)
286-
print(''.join(emsg))
286+
emsg = ''.join(emsg)
287287
raise ValueError(emsg)
288288

289289

0 commit comments

Comments
 (0)