Skip to content

Commit 25979f8

Browse files
committed
Fix traceback.format_exception call if error value or traceback is null.
1 parent 5bf43a2 commit 25979f8

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

module.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,25 +268,32 @@ static PyObject *quickjs_to_python(ContextData *context_obj, JSValue value) {
268268
} else {
269269
PyObject *tbm = PyImport_ImportModule("traceback");
270270
PyObject *tbfmt = PyObject_GetAttrString(tbm, "format_exception");
271-
PyObject *tbstrs = PyObject_CallFunctionObjArgs(
272-
tbfmt,
273-
context_obj->last_python_error_type,
274-
context_obj->last_python_error_value,
275-
context_obj->last_python_error_traceback,
276-
NULL);
271+
PyObject *errv = context_obj->last_python_error_value;
272+
if (errv == NULL) {
273+
errv = Py_None;
274+
}
275+
PyObject *errtb = context_obj->last_python_error_traceback;
276+
if (errtb == NULL) {
277+
errtb = Py_None;
278+
}
279+
Py_INCREF(errv);
280+
Py_INCREF(errtb);
281+
PyObject *tbstrs = PyObject_CallFunctionObjArgs(tbfmt, context_obj->last_python_error_type, errv, errtb, NULL);
277282
PyObject *tbinfo = PyUnicode_FromString(
278283
"\nThe above exception was caused by the following exception:\n\n");
279284
PyList_Insert(tbstrs, 0, tbinfo);
280285
PyObject *sep = PyUnicode_FromString("");
281286
py_stack = PyUnicode_Join(sep, tbstrs);
287+
Py_DECREF(errtb);
288+
Py_DECREF(errv);
282289
Py_DECREF(sep);
283290
Py_DECREF(tbinfo);
284291
Py_DECREF(tbstrs);
285292
Py_DECREF(tbfmt);
286293
Py_DECREF(tbm);
287294
Py_DECREF(context_obj->last_python_error_type);
288-
Py_DECREF(context_obj->last_python_error_value);
289-
Py_DECREF(context_obj->last_python_error_traceback);
295+
Py_XDECREF(context_obj->last_python_error_value);
296+
Py_XDECREF(context_obj->last_python_error_traceback);
290297
context_obj->last_python_error_type = NULL;
291298
context_obj->last_python_error_value = NULL;
292299
context_obj->last_python_error_traceback = NULL;

0 commit comments

Comments
 (0)