gstreamer/gst/gststructure.override
Michael Smith d64761af50 gst/: Don't leak PyObjects wrapping GValues when indexing into a
Original commit message from CVS:
* gst/gststructure.override:
* gst/gsttaglist.override:
Don't leak PyObjects wrapping GValues when indexing into a
GStStructure. Also fix a copy/paste identical bug in taglists.
2006-03-06 16:24:53 +00:00

347 lines
9.2 KiB
C

/* -*- Mode: C; c-basic-offset: 4 -*- */
/* gst-python
* Copyright (C) 2005 Johan Dahlin
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Johan Dahlin <johan@gnome.org>
*/
%%
ignore
gst_structure_get_boolean
gst_structure_get_int
gst_structure_get_fourcc
gst_structure_get_double
gst_structure_get_date
gst_structure_get_clock_time
gst_structure_get_string
gst_structure_get_value
gst_structure_get_enum
gst_structure_get_fraction
gst_structure_set
gst_structure_get_name_id
gst_structure_id_get_value
gst_structure_id_set_value
gst_structure_set_parent_refcount
gst_structure_remove_fields
gst_structure_map_in_place
gst_structure_fixate_field_nearest_fraction
%%
override gst_structure_new kwargs
static int
_wrap_gst_structure_new(PyGBoxed *self, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = { "name", NULL };
char *name;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:GstStructure.__init__", kwlist, &name))
return -1;
self->gtype = GST_TYPE_STRUCTURE;
self->free_on_dealloc = FALSE;
self->boxed = gst_structure_new(name, NULL);
if (!self->boxed) {
PyErr_SetString(PyExc_RuntimeError, "could not create GstStructure object");
return -1;
}
return 0;
}
%%
override gst_structure_set_value kwargs
static PyObject *
_wrap_gst_structure_set_value(PyObject *self, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = { "field", "value", "type_name", NULL };
char *field;
PyObject *py_value = NULL;
char *type_name = NULL;
GType type;
GValue value = { 0 };
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"sO|s:GstStructure.set_value",
kwlist, &field, &py_value,
&type_name)) {
return NULL;
}
if (type_name) {
if (strcmp (type_name, "char") == 0) {
type = G_TYPE_CHAR;
} else if (strcmp (type_name, "uchar") == 0) {
type = G_TYPE_UCHAR;
} else if (strcmp (type_name, "boolean") == 0) {
type = G_TYPE_BOOLEAN;
} else if (strcmp (type_name, "int") == 0) {
type = G_TYPE_INT;
} else if (strcmp (type_name, "uint") == 0) {
type = G_TYPE_UINT;
} else if (strcmp (type_name, "long") == 0) {
type = G_TYPE_LONG;
} else if (strcmp (type_name, "ulong") == 0) {
type = G_TYPE_ULONG;
} else if (strcmp (type_name, "int64") == 0) {
type = G_TYPE_INT64;
} else if (strcmp (type_name, "uint64") == 0) {
type = G_TYPE_UINT64;
} else if (strcmp (type_name, "float") == 0) {
type = G_TYPE_FLOAT;
} else if (strcmp (type_name, "double") == 0) {
type = G_TYPE_DOUBLE;
} else if (strcmp (type_name, "string") == 0) {
type = G_TYPE_STRING;
} else {
PyErr_SetString(PyExc_TypeError,
"invalid type name");
return NULL;
}
} else {
/* Let PyGTK guess a GType for the object. */
type = pyg_type_from_object((PyObject *) py_value->ob_type);
}
if (type != G_TYPE_INVALID) {
g_value_init(&value, type);
} else if (!pygst_value_init_for_pyobject(&value, py_value)) {
return NULL;
}
if (pygst_value_from_pyobject(&value, py_value) != 0) {
return NULL;
}
gst_structure_set_value(pyg_boxed_get(self, GstStructure), field,
&value);
Py_INCREF(Py_None);
return Py_None;
}
%%
define GstStructure.has_key args
static PyObject*
_wrap_gst_structure_has_key(PyGObject *self, PyObject *args)
{
gchar *key;
gboolean has_field;
if (!PyArg_ParseTuple(args, "s:GstStructure.has_key", &key))
return NULL;
has_field = gst_structure_has_field((GstStructure*)self->obj, key);
return PyBool_FromLong(has_field);
}
%%
override gst_structure_keys noargs
static PyObject *
_wrap_gst_structure_keys (PyObject *self)
{
GstStructure *s;
int i, n;
PyObject *ret;
ret = PyList_New(0);
s = pyg_boxed_get(self, GstStructure);
n = gst_structure_n_fields(s);
for (i = 0; i < n; ++i) {
const gchar *name = gst_structure_nth_field_name (s, i);
PyList_Append(ret, PyString_FromString(name));
}
return ret;
}
%%
override-slot GstStructure.tp_as_mapping
static int
_wrap_gst_structure_length(PyGObject *self)
{
return gst_structure_n_fields((GstStructure*)self->obj);
}
static PyObject *
_wrap_gst_structure_subscript(PyGObject *self, PyObject *py_key)
{
PyObject *v = NULL;
const char *field = PyString_AsString(py_key);
if (gst_structure_has_field((GstStructure*)self->obj, field)) {
const GValue *gvalue;
gvalue = gst_structure_get_value((GstStructure*)self->obj, field);
g_assert(gvalue != NULL);
v = pygst_value_as_pyobject(gvalue, TRUE);
} else {
PyErr_SetString(PyExc_KeyError, field);
}
return v;
}
static int
_wrap_gst_structure_ass_subscript(PyGObject *self,
PyObject *py_key,
PyObject *py_value)
{
const char *key;
GstStructure* structure;
structure = (GstStructure*)self->obj;
key = PyString_AsString(py_key);
if (py_value != NULL) {
GValue v = { 0, };
if (!pygst_value_init_for_pyobject (&v, py_value))
return -1;
if (pygst_value_from_pyobject(&v, py_value))
return -1;
gst_structure_set_value(structure, key, &v);
g_value_unset(&v);
} else {
gst_structure_remove_field(structure, key);
}
return 0;
}
static PyMappingMethods _wrap_gst_structure_tp_as_mapping = {
(inquiry)_wrap_gst_structure_length, /* mp_length */
(binaryfunc)_wrap_gst_structure_subscript, /* mp_subscript */
(objobjargproc)_wrap_gst_structure_ass_subscript /* mp_ass_subscript */
};
%%
override gst_structure_foreach kwargs
static gboolean
pygst_structure_foreach_marshal(GQuark field_id,
const GValue *value,
gpointer user_data)
{
PyGstCustomNotify *cunote = user_data;
PyObject *py_field, *py_value, *retobj;
gboolean retval = TRUE;
PyGILState_STATE state;
g_assert(cunote->func);
state = pyg_gil_state_ensure();
py_field = Py_BuildValue("s", g_quark_to_string(field_id));
py_value = pygst_value_as_pyobject(value, FALSE);
if (cunote->data)
retobj = PyEval_CallFunction(cunote->func, "(NNO)",
py_field, py_value,
cunote->data);
else
retobj = PyEval_CallFunction(cunote->func, "(NN)",
py_field, py_value);
if (PyErr_Occurred () || (retobj == NULL) || (retobj == Py_None)) {
PyErr_Print ();
retval = FALSE;
} else if (retobj != Py_None) {
retval = PyInt_AsLong(retobj);
}
Py_XDECREF(retobj);
pyg_gil_state_release(state);
return retval;
}
static PyObject *
_wrap_gst_structure_foreach (PyGObject *self,
PyObject *args,
PyObject *kwargs)
{
static char *kwlist[] = { "foreach_function", "args", NULL };
PyObject *pyfunc, *pyarg = NULL;
PyGstCustomNotify cunote;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"O|O:GstStructure.foreach",
kwlist,
&pyfunc, &pyarg)) {
return NULL;
}
if (!PyCallable_Check(pyfunc)) {
PyErr_SetString(PyExc_TypeError, "foreach_function not callable");
return NULL;
}
cunote.func = pyfunc;
cunote.data = pyarg;
gst_structure_foreach(pyg_boxed_get(self, GstStructure),
pygst_structure_foreach_marshal,
&cunote);
Py_INCREF(Py_None);
return Py_None;
}
%%
override-slot GstStructure.tp_repr
static PyObject *
_wrap_gst_structure_tp_repr (PyGObject *self)
{
char *buf;
PyObject *retval;
buf = g_strdup_printf("<GstStructure (%s) at %lx>",
gst_structure_get_name((GstStructure*)self->obj),
(long)self->obj);
retval = PyString_FromString(buf);
g_free(buf);
return retval;
}
%%
override gst_structure_from_string kwargs
static PyObject *
_wrap_gst_structure_from_string(PyObject *self, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = { "string", NULL };
char *string;
GstStructure *ret;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:structure_from_string", kwlist, &string))
return NULL;
ret = gst_structure_from_string(string, NULL);
/* pyg_boxed_new handles NULL checking */
return pyg_boxed_new(GST_TYPE_STRUCTURE, ret, FALSE, TRUE);
}
%%
override-slot GstStructure.tp_dealloc
static void
_wrap_gst_structure_tp_dealloc (PyObject *self)
{
PyGBoxed *boxed = (PyGBoxed *) self;
if (boxed->free_on_dealloc && boxed->boxed) {
gst_structure_free (boxed->boxed);
} else if (boxed->boxed) {
pygst_caps_map_remove_structure (self);
}
self->ob_type->tp_free((PyObject *)self);
}