Skip to content

Commit 50d9130

Browse files
Navjot SinghNavjot Singh
authored andcommitted
fixed nesting issue and added top level check
1 parent 73d38c8 commit 50d9130

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

bson/_cbsonmodule.c

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,35 +1744,34 @@ int write_dict(PyObject* self, buffer_t buffer,
17441744
if (!decode_and_write_pair(self, buffer, key, value,
17451745
check_keys, options, top_level)) {
17461746
if (PyErr_Occurred()) {
1747-
PyObject *etype, *evalue, *etrace;
1747+
PyObject *etype = NULL, *evalue = NULL, *etrace = NULL;
17481748
PyErr_Fetch(&etype, &evalue, &etrace);
17491749
PyObject *InvalidDocument = _error("InvalidDocument");
17501750

1751-
if (PyErr_GivenExceptionMatches(etype, InvalidDocument)) {
1752-
if (InvalidDocument) {
1753-
Py_DECREF(etype);
1754-
etype = InvalidDocument;
1755-
1756-
if (evalue) {
1757-
PyObject *msg = PyObject_Str(evalue);
1758-
Py_DECREF(evalue);
1759-
1760-
if (msg) {
1761-
// Prepend doc to the existing message
1762-
PyObject *dict_str = PyObject_Str(dict);
1763-
PyObject *new_msg = PyUnicode_FromFormat("Invalid document %s | %s", PyUnicode_AsUTF8(dict_str), PyUnicode_AsUTF8(msg));
1764-
Py_DECREF(dict_str);
1765-
1766-
if (new_msg) {
1767-
evalue = new_msg;
1768-
}
1769-
else {
1770-
evalue = msg;
1771-
}
1751+
if (top_level && InvalidDocument && PyErr_GivenExceptionMatches(etype, InvalidDocument)) {
1752+
1753+
Py_DECREF(etype);
1754+
etype = InvalidDocument;
1755+
1756+
if (evalue) {
1757+
PyObject *msg = PyObject_Str(evalue);
1758+
Py_DECREF(evalue);
1759+
1760+
if (msg) {
1761+
// Prepend doc to the existing message
1762+
PyObject *dict_str = PyObject_Str(dict);
1763+
PyObject *new_msg = PyUnicode_FromFormat("Invalid document %s | %s", PyUnicode_AsUTF8(dict_str), PyUnicode_AsUTF8(msg));
1764+
Py_DECREF(dict_str);
1765+
1766+
if (new_msg) {
1767+
evalue = new_msg;
1768+
}
1769+
else {
1770+
evalue = msg;
17721771
}
17731772
}
1774-
PyErr_NormalizeException(&etype, &evalue, &etrace);
17751773
}
1774+
PyErr_NormalizeException(&etype, &evalue, &etrace);
17761775
}
17771776
else {
17781777
Py_DECREF(InvalidDocument);

0 commit comments

Comments
 (0)