@@ -666,17 +666,15 @@ _PyErr_ChainExceptions(PyObject *typ, PyObject *val, PyObject *tb)
666
666
}
667
667
668
668
if (_PyErr_Occurred (tstate )) {
669
- PyObject * typ2 , * val2 , * tb2 ;
670
- _PyErr_Fetch (tstate , & typ2 , & val2 , & tb2 );
671
669
_PyErr_NormalizeException (tstate , & typ , & val , & tb );
672
670
if (tb != NULL ) {
673
671
PyException_SetTraceback (val , tb );
674
672
Py_DECREF (tb );
675
673
}
676
674
Py_DECREF (typ );
677
- _PyErr_NormalizeException ( tstate , & typ2 , & val2 , & tb2 );
678
- PyException_SetContext (val2 , val );
679
- _PyErr_Restore (tstate , typ2 , val2 , tb2 );
675
+ PyObject * exc2 = _PyErr_GetRaisedException ( tstate );
676
+ PyException_SetContext (exc2 , val );
677
+ _PyErr_SetRaisedException (tstate , exc2 );
680
678
}
681
679
else {
682
680
_PyErr_Restore (tstate , typ , val , tb );
@@ -757,27 +755,15 @@ static PyObject *
757
755
_PyErr_FormatVFromCause (PyThreadState * tstate , PyObject * exception ,
758
756
const char * format , va_list vargs )
759
757
{
760
- PyObject * exc , * val , * val2 , * tb ;
761
-
762
758
assert (_PyErr_Occurred (tstate ));
763
- _PyErr_Fetch (tstate , & exc , & val , & tb );
764
- _PyErr_NormalizeException (tstate , & exc , & val , & tb );
765
- if (tb != NULL ) {
766
- PyException_SetTraceback (val , tb );
767
- Py_DECREF (tb );
768
- }
769
- Py_DECREF (exc );
759
+ PyObject * exc = _PyErr_GetRaisedException (tstate );
770
760
assert (!_PyErr_Occurred (tstate ));
771
-
772
761
_PyErr_FormatV (tstate , exception , format , vargs );
773
-
774
- _PyErr_Fetch (tstate , & exc , & val2 , & tb );
775
- _PyErr_NormalizeException (tstate , & exc , & val2 , & tb );
776
- PyException_SetCause (val2 , Py_NewRef (val ));
777
- PyException_SetContext (val2 , Py_NewRef (val ));
778
- Py_DECREF (val );
779
- _PyErr_Restore (tstate , exc , val2 , tb );
780
-
762
+ PyObject * exc2 = _PyErr_GetRaisedException (tstate );
763
+ PyException_SetCause (exc2 , Py_NewRef (exc ));
764
+ PyException_SetContext (exc2 , Py_NewRef (exc ));
765
+ Py_DECREF (exc );
766
+ _PyErr_SetRaisedException (tstate , exc2 );
781
767
return NULL ;
782
768
}
783
769
@@ -1698,19 +1684,18 @@ static void
1698
1684
PyErr_SyntaxLocationObjectEx (PyObject * filename , int lineno , int col_offset ,
1699
1685
int end_lineno , int end_col_offset )
1700
1686
{
1701
- PyObject * exc , * v , * tb , * tmp ;
1702
1687
PyThreadState * tstate = _PyThreadState_GET ();
1703
1688
1704
1689
/* add attributes for the line number and filename for the error */
1705
- _PyErr_Fetch (tstate , & exc , & v , & tb );
1706
- _PyErr_NormalizeException (tstate , & exc , & v , & tb );
1690
+ PyObject * exc = _PyErr_GetRaisedException (tstate );
1707
1691
/* XXX check that it is, indeed, a syntax error. It might not
1708
1692
* be, though. */
1709
- tmp = PyLong_FromLong (lineno );
1710
- if (tmp == NULL )
1693
+ PyObject * tmp = PyLong_FromLong (lineno );
1694
+ if (tmp == NULL ) {
1711
1695
_PyErr_Clear (tstate );
1696
+ }
1712
1697
else {
1713
- if (PyObject_SetAttr (v , & _Py_ID (lineno ), tmp )) {
1698
+ if (PyObject_SetAttr (exc , & _Py_ID (lineno ), tmp )) {
1714
1699
_PyErr_Clear (tstate );
1715
1700
}
1716
1701
Py_DECREF (tmp );
@@ -1722,7 +1707,7 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
1722
1707
_PyErr_Clear (tstate );
1723
1708
}
1724
1709
}
1725
- if (PyObject_SetAttr (v , & _Py_ID (offset ), tmp ? tmp : Py_None )) {
1710
+ if (PyObject_SetAttr (exc , & _Py_ID (offset ), tmp ? tmp : Py_None )) {
1726
1711
_PyErr_Clear (tstate );
1727
1712
}
1728
1713
Py_XDECREF (tmp );
@@ -1734,7 +1719,7 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
1734
1719
_PyErr_Clear (tstate );
1735
1720
}
1736
1721
}
1737
- if (PyObject_SetAttr (v , & _Py_ID (end_lineno ), tmp ? tmp : Py_None )) {
1722
+ if (PyObject_SetAttr (exc , & _Py_ID (end_lineno ), tmp ? tmp : Py_None )) {
1738
1723
_PyErr_Clear (tstate );
1739
1724
}
1740
1725
Py_XDECREF (tmp );
@@ -1746,20 +1731,20 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
1746
1731
_PyErr_Clear (tstate );
1747
1732
}
1748
1733
}
1749
- if (PyObject_SetAttr (v , & _Py_ID (end_offset ), tmp ? tmp : Py_None )) {
1734
+ if (PyObject_SetAttr (exc , & _Py_ID (end_offset ), tmp ? tmp : Py_None )) {
1750
1735
_PyErr_Clear (tstate );
1751
1736
}
1752
1737
Py_XDECREF (tmp );
1753
1738
1754
1739
tmp = NULL ;
1755
1740
if (filename != NULL ) {
1756
- if (PyObject_SetAttr (v , & _Py_ID (filename ), filename )) {
1741
+ if (PyObject_SetAttr (exc , & _Py_ID (filename ), filename )) {
1757
1742
_PyErr_Clear (tstate );
1758
1743
}
1759
1744
1760
1745
tmp = PyErr_ProgramTextObject (filename , lineno );
1761
1746
if (tmp ) {
1762
- if (PyObject_SetAttr (v , & _Py_ID (text ), tmp )) {
1747
+ if (PyObject_SetAttr (exc , & _Py_ID (text ), tmp )) {
1763
1748
_PyErr_Clear (tstate );
1764
1749
}
1765
1750
Py_DECREF (tmp );
@@ -1768,17 +1753,17 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
1768
1753
_PyErr_Clear (tstate );
1769
1754
}
1770
1755
}
1771
- if (exc != PyExc_SyntaxError ) {
1772
- if (_PyObject_LookupAttr (v , & _Py_ID (msg ), & tmp ) < 0 ) {
1756
+ if (( PyObject * ) Py_TYPE ( exc ) != PyExc_SyntaxError ) {
1757
+ if (_PyObject_LookupAttr (exc , & _Py_ID (msg ), & tmp ) < 0 ) {
1773
1758
_PyErr_Clear (tstate );
1774
1759
}
1775
1760
else if (tmp ) {
1776
1761
Py_DECREF (tmp );
1777
1762
}
1778
1763
else {
1779
- tmp = PyObject_Str (v );
1764
+ tmp = PyObject_Str (exc );
1780
1765
if (tmp ) {
1781
- if (PyObject_SetAttr (v , & _Py_ID (msg ), tmp )) {
1766
+ if (PyObject_SetAttr (exc , & _Py_ID (msg ), tmp )) {
1782
1767
_PyErr_Clear (tstate );
1783
1768
}
1784
1769
Py_DECREF (tmp );
@@ -1788,19 +1773,19 @@ PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
1788
1773
}
1789
1774
}
1790
1775
1791
- if (_PyObject_LookupAttr (v , & _Py_ID (print_file_and_line ), & tmp ) < 0 ) {
1776
+ if (_PyObject_LookupAttr (exc , & _Py_ID (print_file_and_line ), & tmp ) < 0 ) {
1792
1777
_PyErr_Clear (tstate );
1793
1778
}
1794
1779
else if (tmp ) {
1795
1780
Py_DECREF (tmp );
1796
1781
}
1797
1782
else {
1798
- if (PyObject_SetAttr (v , & _Py_ID (print_file_and_line ), Py_None )) {
1783
+ if (PyObject_SetAttr (exc , & _Py_ID (print_file_and_line ), Py_None )) {
1799
1784
_PyErr_Clear (tstate );
1800
1785
}
1801
1786
}
1802
1787
}
1803
- _PyErr_Restore (tstate , exc , v , tb );
1788
+ _PyErr_SetRaisedException (tstate , exc );
1804
1789
}
1805
1790
1806
1791
void
0 commit comments