diff --git a/bson/_cbsonmodule.c b/bson/_cbsonmodule.c index be91e41734..d7c028dbbc 100644 --- a/bson/_cbsonmodule.c +++ b/bson/_cbsonmodule.c @@ -1657,10 +1657,10 @@ void handle_invalid_doc_error(PyObject* dict) { } if (evalue && PyErr_GivenExceptionMatches(etype, InvalidDocument)) { - PyObject *msg = PyObject_Str(evalue); + msg = PyObject_Str(evalue); if (msg) { // Prepend doc to the existing message - PyObject *dict_str = PyObject_Str(dict); + dict_str = PyObject_Str(dict); if (dict_str == NULL) { goto cleanup; } @@ -1672,15 +1672,17 @@ void handle_invalid_doc_error(PyObject* dict) { if (msg_utf8 == NULL) { goto cleanup; } - PyObject *new_msg = PyUnicode_FromFormat("Invalid document %s | %s", dict_str_utf8, msg_utf8); + new_msg = PyUnicode_FromFormat("Invalid document %s | %s", dict_str_utf8, msg_utf8); Py_DECREF(evalue); Py_DECREF(etype); etype = InvalidDocument; InvalidDocument = NULL; if (new_msg) { evalue = new_msg; + new_msg = NULL; } else { evalue = msg; + msg = NULL; } } PyErr_NormalizeException(&etype, &evalue, &etrace);