@@ -1644,6 +1644,68 @@ static int write_raw_doc(buffer_t buffer, PyObject* raw, PyObject* _raw_str) {
1644
1644
return bytes_written ;
1645
1645
}
1646
1646
1647
+
1648
+ /* Update Invalid Document error message to include doc.
1649
+ */
1650
+ void handle_invalid_doc_error () {
1651
+ PyObject * etype = NULL , * evalue = NULL , * etrace = NULL ;
1652
+ PyErr_Fetch (& etype , & evalue , & etrace );
1653
+ PyObject * InvalidDocument = _error ("InvalidDocument" );
1654
+ if (InvalidDocument == NULL ) {
1655
+ PyErr_Restore (etype , evalue , etrace );
1656
+ return 0 ;
1657
+ }
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 );
1666
+ 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 0 ;
1674
+ }
1675
+ PyObject * dict_str_utf8 = PyUnicode_AsUTF8 (dict );
1676
+ Py_DECREF (dict_str );
1677
+ if (dict_str_utf8 == NULL ) {
1678
+ Py_DECREF (msg );
1679
+ return 0
1680
+ }
1681
+ PyObject * msg_utf8 = PyUnicode_AsUTF8 (msg );
1682
+ if (msg_utf8 == NULL ) {
1683
+ Py_DECREF (msg );
1684
+ Py_DECREF (dict_str_utf8 )
1685
+ return 0
1686
+ }
1687
+ PyObject * new_msg = PyUnicode_FromFormat ("Invalid document %s | %s" , dict_str_utf8 , msg_utf8 );
1688
+ Py_DECREF (msg_utf8 );
1689
+
1690
+ if (new_msg ) {
1691
+ Py_DECREF (msg );
1692
+ evalue = new_msg ;
1693
+ }
1694
+ else {
1695
+ Py_DECREF (new_msg );
1696
+ evalue = msg ;
1697
+ }
1698
+ }
1699
+ }
1700
+ PyErr_NormalizeException (& etype , & evalue , & etrace );
1701
+ }
1702
+ else {
1703
+ Py_DECREF (InvalidDocument );
1704
+ }
1705
+ PyErr_Restore (etype , evalue , etrace );
1706
+ }
1707
+
1708
+
1647
1709
/* returns the number of bytes written or 0 on failure */
1648
1710
int write_dict (PyObject * self , buffer_t buffer ,
1649
1711
PyObject * dict , unsigned char check_keys ,
@@ -1743,46 +1805,10 @@ int write_dict(PyObject* self, buffer_t buffer,
1743
1805
while (PyDict_Next (dict , & pos , & key , & value )) {
1744
1806
if (!decode_and_write_pair (self , buffer , key , value ,
1745
1807
check_keys , options , top_level )) {
1746
- if (PyErr_Occurred ()) {
1747
- PyObject * etype = NULL , * evalue = NULL , * etrace = NULL ;
1748
- PyErr_Fetch (& etype , & evalue , & etrace );
1749
- PyObject * InvalidDocument = _error ("InvalidDocument" );
1750
- if (InvalidDocument == NULL ) {
1751
- return 0 ;
1752
- }
1753
-
1754
- if (top_level && InvalidDocument && PyErr_GivenExceptionMatches (etype , InvalidDocument )) {
1755
-
1756
- Py_DECREF (etype );
1757
- etype = InvalidDocument ;
1758
-
1759
- if (evalue ) {
1760
- PyObject * msg = PyObject_Str (evalue );
1761
- Py_DECREF (evalue );
1762
-
1763
- if (msg ) {
1764
- // Prepend doc to the existing message
1765
- PyObject * dict_str = PyObject_Str (dict );
1766
- if (dict_str == NULL ) {
1767
- return 0 ;
1768
- }
1769
- PyObject * new_msg = PyUnicode_FromFormat ("Invalid document %s | %s" , PyUnicode_AsUTF8 (dict_str ), PyUnicode_AsUTF8 (msg ));
1770
- Py_DECREF (dict_str );
1771
-
1772
- if (new_msg ) {
1773
- evalue = new_msg ;
1774
- }
1775
- else {
1776
- evalue = msg ;
1777
- }
1778
- }
1779
- }
1780
- PyErr_NormalizeException (& etype , & evalue , & etrace );
1781
- }
1782
- else {
1783
- Py_DECREF (InvalidDocument );
1784
- }
1785
- PyErr_Restore (etype , evalue , etrace );
1808
+ Py_DECREF (key );
1809
+ Py_DECREF (value );
1810
+ if (PyErr_Occurred () && top_level ) {
1811
+ handle_invalid_doc_error ();
1786
1812
}
1787
1813
return 0 ;
1788
1814
}
@@ -1805,6 +1831,9 @@ int write_dict(PyObject* self, buffer_t buffer,
1805
1831
Py_DECREF (key );
1806
1832
Py_DECREF (value );
1807
1833
Py_DECREF (iter );
1834
+ if (PyErr_Occurred () && top_level ) {
1835
+ handle_invalid_doc_error ();
1836
+ }
1808
1837
return 0 ;
1809
1838
}
1810
1839
Py_DECREF (key );
0 commit comments