[py] fix reference counting of exception types (thanks @wackou)
authorPaul Brossier <piem@piem.org>
Sat, 29 Jun 2019 10:42:36 +0000 (12:42 +0200)
committerPaul Brossier <piem@piem.org>
Sat, 29 Jun 2019 10:42:36 +0000 (12:42 +0200)
Commit 8bfef30 exposed a reference counting error, causing the
interpreter to crash before exiting. The solution is to incref the
exception type before calling PyErr_Restore.

python/ext/py-filter.c

index 19dcc60..220eb13 100644 (file)
@@ -163,7 +163,10 @@ Py_filter_set_c_weighting (Py_filter * self, PyObject *args)
       // change the RuntimeError into ValueError
       PyObject *type, *value, *traceback;
       PyErr_Fetch(&type, &value, &traceback);
-      PyErr_Restore(PyExc_ValueError, value, traceback);
+      Py_XDECREF(type);
+      type = PyExc_ValueError;
+      Py_XINCREF(type);
+      PyErr_Restore(type, value, traceback);
     }
     return NULL;
   }
@@ -188,7 +191,10 @@ Py_filter_set_a_weighting (Py_filter * self, PyObject *args)
       // change the RuntimeError into ValueError
       PyObject *type, *value, *traceback;
       PyErr_Fetch(&type, &value, &traceback);
-      PyErr_Restore(PyExc_ValueError, value, traceback);
+      Py_XDECREF(type);
+      type = PyExc_ValueError;
+      Py_XINCREF(type);
+      PyErr_Restore(type, value, traceback);
     }
     return NULL;
   }
@@ -213,7 +219,10 @@ Py_filter_set_biquad(Py_filter * self, PyObject *args)
       // change the RuntimeError into ValueError
       PyObject *type, *value, *traceback;
       PyErr_Fetch(&type, &value, &traceback);
-      PyErr_Restore(PyExc_ValueError, value, traceback);
+      Py_XDECREF(type);
+      type = PyExc_ValueError;
+      Py_XINCREF(type);
+      PyErr_Restore(type, value, traceback);
     }
     return NULL;
   }