@@ -1744,35 +1744,34 @@ int write_dict(PyObject* self, buffer_t buffer,
1744
1744
if (!decode_and_write_pair (self , buffer , key , value ,
1745
1745
check_keys , options , top_level )) {
1746
1746
if (PyErr_Occurred ()) {
1747
- PyObject * etype , * evalue , * etrace ;
1747
+ PyObject * etype = NULL , * evalue = NULL , * etrace = NULL ;
1748
1748
PyErr_Fetch (& etype , & evalue , & etrace );
1749
1749
PyObject * InvalidDocument = _error ("InvalidDocument" );
1750
1750
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 ;
1772
1771
}
1773
1772
}
1774
- PyErr_NormalizeException (& etype , & evalue , & etrace );
1775
1773
}
1774
+ PyErr_NormalizeException (& etype , & evalue , & etrace );
1776
1775
}
1777
1776
else {
1778
1777
Py_DECREF (InvalidDocument );
0 commit comments