PageRenderTime 26ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

/gst/pygstexception.c

https://github.com/zaheerm/gst-python
C | 267 lines | 170 code | 63 blank | 34 comment | 46 complexity | 17dc27c45f0a477f7a84f4f6c13c2fd7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * pygstexception.c - gst-python exceptions
  3. * Copyright (C) 2005 Alessandro Decina
  4. *
  5. * Authors:
  6. * Alessandro Decina <alessandro@nnva.org>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Library General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public
  19. * License along with this library; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. */
  23. #include <Python.h>
  24. #include "structmember.h"
  25. PyObject *PyGstExc_LinkError = NULL;
  26. PyObject *PyGstExc_AddError = NULL;
  27. PyObject *PyGstExc_QueryError = NULL;
  28. PyObject *PyGstExc_RemoveError = NULL;
  29. PyObject *PyGstExc_PluginNotFoundError = NULL;
  30. PyObject *PyGstExc_ElementNotFoundError = NULL;
  31. static PyObject *
  32. call_exception_init(PyObject *args)
  33. {
  34. PyObject *parent_init = NULL;
  35. PyObject *res = NULL;
  36. /* get Exception.__init__ */
  37. parent_init = PyObject_GetAttrString(PyExc_Exception, "__init__");
  38. if (parent_init == NULL)
  39. goto exception;
  40. /* call Exception.__init__. This will set self.args */
  41. res = PyObject_CallObject(parent_init, args);
  42. if (res == NULL)
  43. goto exception;
  44. Py_DECREF(parent_init);
  45. return res;
  46. exception:
  47. Py_XDECREF(parent_init);
  48. Py_XDECREF(res);
  49. return NULL;
  50. }
  51. static int
  52. add_method(PyObject *klass, PyObject *dict, PyMethodDef *method) {
  53. PyObject *module = NULL;
  54. PyObject *func = NULL;
  55. PyObject *meth = NULL;
  56. module = PyString_FromString("gst");
  57. if (module == NULL)
  58. goto exception;
  59. func = PyCFunction_NewEx(method, NULL, module);
  60. if (func == NULL)
  61. goto exception;
  62. Py_DECREF(module);
  63. meth = PyMethod_New(func, NULL, klass);
  64. if (meth == NULL)
  65. goto exception;
  66. Py_DECREF(func);
  67. if (PyDict_SetItemString(dict, method->ml_name, meth) < 0)
  68. goto exception;
  69. Py_DECREF(meth);
  70. return 0;
  71. exception:
  72. Py_XDECREF(module);
  73. Py_XDECREF(func);
  74. Py_XDECREF(meth);
  75. return -1;
  76. }
  77. static PyObject *
  78. link_error_init(PyObject *self, PyObject *args)
  79. {
  80. PyObject *err_type = NULL;
  81. int status;
  82. if (!PyArg_ParseTuple(args, "O|O:__init__", &self, &err_type))
  83. return NULL;
  84. if (err_type == NULL)
  85. err_type = Py_None;
  86. Py_INCREF(err_type);
  87. /* set self.error */
  88. status = PyObject_SetAttrString(self, "error", err_type);
  89. Py_DECREF(err_type);
  90. if (status < 0)
  91. return NULL;
  92. return call_exception_init(args);
  93. }
  94. static PyObject *
  95. element_not_found_error_init(PyObject *self, PyObject *args)
  96. {
  97. PyObject *element_name = NULL;
  98. int status;
  99. if (!PyArg_ParseTuple(args, "O|O:__init__", &self, &element_name))
  100. return NULL;
  101. if (element_name == NULL)
  102. element_name = Py_None;
  103. Py_INCREF(element_name);
  104. /* set self.name */
  105. status = PyObject_SetAttrString(self, "name", element_name);
  106. Py_DECREF(element_name);
  107. if (status < 0)
  108. return NULL;
  109. return call_exception_init(args);
  110. }
  111. static PyMethodDef link_error_init_method = {"__init__",
  112. link_error_init, METH_VARARGS
  113. };
  114. static PyMethodDef element_not_found_error_init_method = {"__init__",
  115. element_not_found_error_init, METH_VARARGS
  116. };
  117. void
  118. pygst_exceptions_register_classes(PyObject *d)
  119. {
  120. PyObject *dict = NULL;
  121. /* register gst.LinkError */
  122. dict = PyDict_New();
  123. if (dict == NULL)
  124. goto exception;
  125. PyGstExc_LinkError = PyErr_NewException("gst.LinkError",
  126. PyExc_Exception, dict);
  127. if (PyGstExc_LinkError == NULL)
  128. goto exception;
  129. if (add_method(PyGstExc_LinkError, dict, &link_error_init_method) < 0)
  130. goto exception;
  131. Py_DECREF(dict);
  132. if (PyDict_SetItemString(d, "LinkError", PyGstExc_LinkError) < 0)
  133. goto exception;
  134. Py_DECREF(PyGstExc_LinkError);
  135. /* register gst.AddError */
  136. PyGstExc_AddError = PyErr_NewException("gst.AddError",
  137. PyExc_Exception, NULL);
  138. if (PyGstExc_AddError == NULL)
  139. goto exception;
  140. if (PyDict_SetItemString(d, "AddError", PyGstExc_AddError) < 0)
  141. goto exception;
  142. Py_DECREF(PyGstExc_AddError);
  143. /* register gst.RemoveError */
  144. PyGstExc_RemoveError = PyErr_NewException("gst.RemoveError",
  145. PyExc_Exception, NULL);
  146. if (PyGstExc_RemoveError == NULL)
  147. goto exception;
  148. if (PyDict_SetItemString(d, "RemoveError", PyGstExc_RemoveError) < 0)
  149. goto exception;
  150. Py_DECREF(PyGstExc_RemoveError);
  151. /* register gst.QueryError */
  152. PyGstExc_QueryError = PyErr_NewException("gst.QueryError",
  153. PyExc_Exception, NULL);
  154. if (PyGstExc_QueryError == NULL)
  155. goto exception;
  156. if (PyDict_SetItemString(d, "QueryError", PyGstExc_QueryError) < 0)
  157. goto exception;
  158. Py_DECREF(PyGstExc_QueryError);
  159. /* FIXME: remove this method in 0.11; element_factory_make deals with element
  160. factories, not plug-ins */
  161. /* register gst.PluginNotFoundError */
  162. dict = PyDict_New();
  163. if (dict == NULL)
  164. goto exception;
  165. PyGstExc_PluginNotFoundError = \
  166. PyErr_NewException("gst.PluginNotFoundError", PyExc_Exception, dict);
  167. if (PyGstExc_PluginNotFoundError == NULL)
  168. goto exception;
  169. if (add_method(PyGstExc_PluginNotFoundError,
  170. dict, &element_not_found_error_init_method) < 0)
  171. goto exception;
  172. Py_DECREF(dict);
  173. if (PyDict_SetItemString(d, "PluginNotFoundError",
  174. PyGstExc_PluginNotFoundError) < 0)
  175. goto exception;
  176. Py_DECREF(PyGstExc_PluginNotFoundError);
  177. /* register gst.ElementNotFoundError */
  178. dict = PyDict_New();
  179. if (dict == NULL)
  180. goto exception;
  181. PyGstExc_ElementNotFoundError = \
  182. PyErr_NewException("gst.ElementNotFoundError", PyGstExc_PluginNotFoundError, dict);
  183. if (PyGstExc_ElementNotFoundError == NULL)
  184. goto exception;
  185. if (add_method(PyGstExc_ElementNotFoundError,
  186. dict, &element_not_found_error_init_method) < 0)
  187. goto exception;
  188. Py_DECREF(dict);
  189. if (PyDict_SetItemString(d, "ElementNotFoundError",
  190. PyGstExc_ElementNotFoundError) < 0)
  191. goto exception;
  192. Py_DECREF(PyGstExc_ElementNotFoundError);
  193. return;
  194. return;
  195. exception:
  196. Py_XDECREF(dict);
  197. Py_XDECREF(PyGstExc_LinkError);
  198. Py_XDECREF(PyGstExc_AddError);
  199. Py_XDECREF(PyGstExc_RemoveError);
  200. Py_XDECREF(PyGstExc_QueryError);
  201. Py_XDECREF(PyGstExc_PluginNotFoundError);
  202. Py_XDECREF(PyGstExc_ElementNotFoundError);
  203. return;
  204. }