PageRenderTime 46ms CodeModel.GetById 34ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/GStreamer/Source/gst-python/gst/pygstexception.c

http://ossbuild.googlecode.com/
C | 269 lines | 172 code | 63 blank | 34 comment | 46 complexity | dabfa9e00ac88d6bee0296546a96c275 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-2.0, AGPL-1.0, LGPL-2.1, LGPL-2.0, GPL-3.0, LGPL-3.0, CC-BY-SA-3.0
  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
 24#include <Python.h>
 25#include "structmember.h"
 26
 27PyObject *PyGstExc_LinkError = NULL;
 28PyObject *PyGstExc_AddError = NULL;
 29PyObject *PyGstExc_QueryError = NULL;
 30PyObject *PyGstExc_RemoveError = NULL;
 31PyObject *PyGstExc_PluginNotFoundError = NULL;
 32PyObject *PyGstExc_ElementNotFoundError = NULL;
 33
 34
 35static PyObject *
 36call_exception_init (PyObject * args)
 37{
 38  PyObject *parent_init = NULL;
 39  PyObject *res = NULL;
 40
 41  /* get Exception.__init__ */
 42  parent_init = PyObject_GetAttrString (PyExc_Exception, "__init__");
 43  if (parent_init == NULL)
 44    goto exception;
 45
 46  /* call Exception.__init__. This will set self.args */
 47  res = PyObject_CallObject (parent_init, args);
 48  if (res == NULL)
 49    goto exception;
 50
 51  Py_DECREF (parent_init);
 52
 53  return res;
 54
 55exception:
 56  Py_XDECREF (parent_init);
 57  Py_XDECREF (res);
 58
 59  return NULL;
 60}
 61
 62static int
 63add_method (PyObject * klass, PyObject * dict, PyMethodDef * method)
 64{
 65  PyObject *module = NULL;
 66  PyObject *func = NULL;
 67  PyObject *meth = NULL;
 68
 69  module = PyString_FromString ("gst");
 70  if (module == NULL)
 71    goto exception;
 72
 73  func = PyCFunction_NewEx (method, NULL, module);
 74  if (func == NULL)
 75    goto exception;
 76  Py_DECREF (module);
 77
 78  meth = PyMethod_New (func, NULL, klass);
 79  if (meth == NULL)
 80    goto exception;
 81  Py_DECREF (func);
 82
 83  if (PyDict_SetItemString (dict, method->ml_name, meth) < 0)
 84    goto exception;
 85  Py_DECREF (meth);
 86
 87  return 0;
 88
 89exception:
 90  Py_XDECREF (module);
 91  Py_XDECREF (func);
 92  Py_XDECREF (meth);
 93
 94  return -1;
 95}
 96
 97static PyObject *
 98link_error_init (PyObject * self, PyObject * args)
 99{
100  PyObject *err_type = NULL;
101  int status;
102
103  if (!PyArg_ParseTuple (args, "O|O:__init__", &self, &err_type))
104    return NULL;
105
106  if (err_type == NULL)
107    err_type = Py_None;
108  Py_INCREF (err_type);
109
110  /* set self.error */
111  status = PyObject_SetAttrString (self, "error", err_type);
112  Py_DECREF (err_type);
113  if (status < 0)
114    return NULL;
115
116  return call_exception_init (args);
117}
118
119static PyObject *
120element_not_found_error_init (PyObject * self, PyObject * args)
121{
122  PyObject *element_name = NULL;
123  int status;
124
125  if (!PyArg_ParseTuple (args, "O|O:__init__", &self, &element_name))
126    return NULL;
127
128  if (element_name == NULL)
129    element_name = Py_None;
130  Py_INCREF (element_name);
131
132  /* set self.name */
133  status = PyObject_SetAttrString (self, "name", element_name);
134  Py_DECREF (element_name);
135  if (status < 0)
136    return NULL;
137
138  return call_exception_init (args);
139}
140
141static PyMethodDef link_error_init_method = { "__init__",
142  link_error_init, METH_VARARGS
143};
144
145static PyMethodDef element_not_found_error_init_method = { "__init__",
146  element_not_found_error_init, METH_VARARGS
147};
148
149void
150pygst_exceptions_register_classes (PyObject * d)
151{
152  PyObject *dict = NULL;
153
154  /* register gst.LinkError */
155  dict = PyDict_New ();
156  if (dict == NULL)
157    goto exception;
158
159  PyGstExc_LinkError = PyErr_NewException ("gst.LinkError",
160      PyExc_Exception, dict);
161  if (PyGstExc_LinkError == NULL)
162    goto exception;
163
164  if (add_method (PyGstExc_LinkError, dict, &link_error_init_method) < 0)
165    goto exception;
166
167  Py_DECREF (dict);
168
169  if (PyDict_SetItemString (d, "LinkError", PyGstExc_LinkError) < 0)
170    goto exception;
171
172  Py_DECREF (PyGstExc_LinkError);
173
174  /* register gst.AddError */
175  PyGstExc_AddError = PyErr_NewException ("gst.AddError",
176      PyExc_Exception, NULL);
177  if (PyGstExc_AddError == NULL)
178    goto exception;
179
180  if (PyDict_SetItemString (d, "AddError", PyGstExc_AddError) < 0)
181    goto exception;
182
183  Py_DECREF (PyGstExc_AddError);
184
185  /* register gst.RemoveError */
186  PyGstExc_RemoveError = PyErr_NewException ("gst.RemoveError",
187      PyExc_Exception, NULL);
188  if (PyGstExc_RemoveError == NULL)
189    goto exception;
190
191  if (PyDict_SetItemString (d, "RemoveError", PyGstExc_RemoveError) < 0)
192    goto exception;
193
194  Py_DECREF (PyGstExc_RemoveError);
195
196  /* register gst.QueryError */
197  PyGstExc_QueryError = PyErr_NewException ("gst.QueryError",
198      PyExc_Exception, NULL);
199  if (PyGstExc_QueryError == NULL)
200    goto exception;
201
202  if (PyDict_SetItemString (d, "QueryError", PyGstExc_QueryError) < 0)
203    goto exception;
204
205  Py_DECREF (PyGstExc_QueryError);
206
207/* FIXME: remove this method in 0.11; element_factory_make deals with element
208   factories, not plug-ins */
209
210  /* register gst.PluginNotFoundError */
211  dict = PyDict_New ();
212  if (dict == NULL)
213    goto exception;
214
215  PyGstExc_PluginNotFoundError =
216      PyErr_NewException ("gst.PluginNotFoundError", PyExc_Exception, dict);
217  if (PyGstExc_PluginNotFoundError == NULL)
218    goto exception;
219
220  if (add_method (PyGstExc_PluginNotFoundError,
221          dict, &element_not_found_error_init_method) < 0)
222    goto exception;
223
224  Py_DECREF (dict);
225
226  if (PyDict_SetItemString (d, "PluginNotFoundError",
227          PyGstExc_PluginNotFoundError) < 0)
228    goto exception;
229
230  Py_DECREF (PyGstExc_PluginNotFoundError);
231
232  /* register gst.ElementNotFoundError */
233  dict = PyDict_New ();
234  if (dict == NULL)
235    goto exception;
236
237  PyGstExc_ElementNotFoundError =
238      PyErr_NewException ("gst.ElementNotFoundError",
239      PyGstExc_PluginNotFoundError, dict);
240  if (PyGstExc_ElementNotFoundError == NULL)
241    goto exception;
242
243  if (add_method (PyGstExc_ElementNotFoundError,
244          dict, &element_not_found_error_init_method) < 0)
245    goto exception;
246
247  Py_DECREF (dict);
248
249  if (PyDict_SetItemString (d, "ElementNotFoundError",
250          PyGstExc_ElementNotFoundError) < 0)
251    goto exception;
252
253  Py_DECREF (PyGstExc_ElementNotFoundError);
254
255  return;
256
257  return;
258
259exception:
260  Py_XDECREF (dict);
261  Py_XDECREF (PyGstExc_LinkError);
262  Py_XDECREF (PyGstExc_AddError);
263  Py_XDECREF (PyGstExc_RemoveError);
264  Py_XDECREF (PyGstExc_QueryError);
265  Py_XDECREF (PyGstExc_PluginNotFoundError);
266  Py_XDECREF (PyGstExc_ElementNotFoundError);
267
268  return;
269}