@@ -1649,59 +1649,48 @@ static int write_raw_doc(buffer_t buffer, PyObject* raw, PyObject* _raw_str) {
1649
1649
*/
1650
1650
void handle_invalid_doc_error (PyObject * dict ) {
1651
1651
PyObject * etype = NULL , * evalue = NULL , * etrace = NULL ;
1652
+ PyObject * msg = NULL , * dict_str = NULL , * new_msg = NULL ;
1652
1653
PyErr_Fetch (& etype , & evalue , & etrace );
1653
1654
PyObject * InvalidDocument = _error ("InvalidDocument" );
1654
1655
if (InvalidDocument == NULL ) {
1655
- PyErr_Restore (etype , evalue , etrace );
1656
- return ;
1656
+ goto cleanup ;
1657
1657
}
1658
1658
1659
- if (PyErr_GivenExceptionMatches (etype , InvalidDocument )) {
1660
-
1661
- Py_DECREF (etype );
1662
- etype = InvalidDocument ;
1663
-
1664
- if (evalue ) {
1665
- PyObject * msg = PyObject_Str (evalue );
1659
+ if (evalue && PyErr_GivenExceptionMatches (etype , InvalidDocument )) {
1660
+ PyObject * msg = PyObject_Str (evalue );
1661
+ if (msg ) {
1662
+ // Prepend doc to the existing message
1663
+ PyObject * dict_str = PyObject_Str (dict );
1664
+ if (dict_str == NULL ) {
1665
+ goto cleanup ;
1666
+ }
1667
+ const char * dict_str_utf8 = PyUnicode_AsUTF8 (dict_str );
1668
+ if (dict_str_utf8 == NULL ) {
1669
+ goto cleanup ;
1670
+ }
1671
+ const char * msg_utf8 = PyUnicode_AsUTF8 (msg );
1672
+ if (msg_utf8 == NULL ) {
1673
+ goto cleanup ;
1674
+ }
1675
+ PyObject * new_msg = PyUnicode_FromFormat ("Invalid document %s | %s" , dict_str_utf8 , msg_utf8 );
1666
1676
Py_DECREF (evalue );
1667
-
1668
- if (msg ) {
1669
- // Prepend doc to the existing message
1670
- PyObject * dict_str = PyObject_Str (dict );
1671
- if (dict_str == NULL ) {
1672
- Py_DECREF (msg );
1673
- return ;
1674
- }
1675
- const char * dict_str_utf8 = PyUnicode_AsUTF8 (dict );
1676
- Py_DECREF (dict_str );
1677
- if (dict_str_utf8 == NULL ) {
1678
- Py_DECREF (msg );
1679
- return ;
1680
- }
1681
- const char * msg_utf8 = PyUnicode_AsUTF8 (msg );
1682
- if (msg_utf8 == NULL ) {
1683
- Py_DECREF (msg );
1684
- return ;
1685
- }
1686
- PyObject * new_msg = PyUnicode_FromFormat ("Invalid document %s | %s" , dict_str_utf8 , msg_utf8 );
1687
- Py_DECREF (msg_utf8 );
1688
-
1689
- if (new_msg ) {
1690
- Py_DECREF (msg );
1691
- evalue = new_msg ;
1692
- }
1693
- else {
1694
- Py_DECREF (new_msg );
1695
- evalue = msg ;
1696
- }
1677
+ Py_DECREF (etype );
1678
+ etype = InvalidDocument ;
1679
+ InvalidDocument = NULL ;
1680
+ if (new_msg ) {
1681
+ evalue = new_msg ;
1682
+ } else {
1683
+ evalue = msg ;
1697
1684
}
1698
1685
}
1699
1686
PyErr_NormalizeException (& etype , & evalue , & etrace );
1700
1687
}
1701
- else {
1702
- Py_DECREF (InvalidDocument );
1703
- }
1688
+ cleanup :
1704
1689
PyErr_Restore (etype , evalue , etrace );
1690
+ Py_XDECREF (msg );
1691
+ Py_XDECREF (InvalidDocument );
1692
+ Py_XDECREF (dict_str );
1693
+ Py_XDECREF (new_msg );
1705
1694
}
1706
1695
1707
1696
@@ -1804,8 +1793,6 @@ int write_dict(PyObject* self, buffer_t buffer,
1804
1793
while (PyDict_Next (dict , & pos , & key , & value )) {
1805
1794
if (!decode_and_write_pair (self , buffer , key , value ,
1806
1795
check_keys , options , top_level )) {
1807
- Py_DECREF (key );
1808
- Py_DECREF (value );
1809
1796
if (PyErr_Occurred () && top_level ) {
1810
1797
handle_invalid_doc_error (dict );
1811
1798
}
@@ -1827,12 +1814,12 @@ int write_dict(PyObject* self, buffer_t buffer,
1827
1814
}
1828
1815
if (!decode_and_write_pair (self , buffer , key , value ,
1829
1816
check_keys , options , top_level )) {
1830
- Py_DECREF (key );
1831
- Py_DECREF (value );
1832
- Py_DECREF (iter );
1833
1817
if (PyErr_Occurred () && top_level ) {
1834
1818
handle_invalid_doc_error (dict );
1835
1819
}
1820
+ Py_DECREF (key );
1821
+ Py_DECREF (value );
1822
+ Py_DECREF (iter );
1836
1823
return 0 ;
1837
1824
}
1838
1825
Py_DECREF (key );
0 commit comments