[py] fix reference counting of exception types (thanks @wackou)
[aubio.git] / python / ext / py-filter.c
index 8eb87aa..220eb13 100644 (file)
@@ -159,6 +159,14 @@ Py_filter_set_c_weighting (Py_filter * self, PyObject *args)
     if (PyErr_Occurred() == NULL) {
       PyErr_SetString (PyExc_ValueError,
           "error when setting filter to C-weighting");
+    } else {
+      // change the RuntimeError into ValueError
+      PyObject *type, *value, *traceback;
+      PyErr_Fetch(&type, &value, &traceback);
+      Py_XDECREF(type);
+      type = PyExc_ValueError;
+      Py_XINCREF(type);
+      PyErr_Restore(type, value, traceback);
     }
     return NULL;
   }
@@ -179,6 +187,14 @@ Py_filter_set_a_weighting (Py_filter * self, PyObject *args)
     if (PyErr_Occurred() == NULL) {
       PyErr_SetString (PyExc_ValueError,
           "error when setting filter to A-weighting");
+    } else {
+      // change the RuntimeError into ValueError
+      PyObject *type, *value, *traceback;
+      PyErr_Fetch(&type, &value, &traceback);
+      Py_XDECREF(type);
+      type = PyExc_ValueError;
+      Py_XINCREF(type);
+      PyErr_Restore(type, value, traceback);
     }
     return NULL;
   }
@@ -199,6 +215,14 @@ Py_filter_set_biquad(Py_filter * self, PyObject *args)
     if (PyErr_Occurred() == NULL) {
       PyErr_SetString (PyExc_ValueError,
           "error when setting filter with biquad coefficients");
+    } else {
+      // change the RuntimeError into ValueError
+      PyObject *type, *value, *traceback;
+      PyErr_Fetch(&type, &value, &traceback);
+      Py_XDECREF(type);
+      type = PyExc_ValueError;
+      Py_XINCREF(type);
+      PyErr_Restore(type, value, traceback);
     }
     return NULL;
   }