Close #20105: set __traceback__ when chaining exceptions in C
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index bff7f08..2531ead 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -2689,8 +2689,11 @@
* types as well, but that's quite a bit trickier due to the extra
* state potentially stored on OSError instances.
*/
-
- Py_XDECREF(tb);
+ /* Ensure the traceback is set correctly on the existing exception */
+ if (tb != NULL) {
+ PyException_SetTraceback(val, tb);
+ Py_DECREF(tb);
+ }
#ifdef HAVE_STDARG_PROTOTYPES
va_start(vargs, format);