Actually commit the numerous copyright-statement changes.
[dbus-python-phuang.git] / _dbus_bindings / signature.c
blobfab864f93b1ad0f862cd13dd1b4838356e45bc6e
1 /* Implementation of Signature type for D-Bus bindings.
2  *
3  * Copyright (C) 2006 Collabora Ltd.
4  *
5  * Licensed under the Academic Free License version 2.1
6  *
7  * This library is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2.1 of the License, or
10  * (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
23 #include <Python.h>
24 #include <structmember.h>
26 #include <stdint.h>
28 #include "dbus_bindings-internal.h"
29 #include "types-internal.h"
31 PyDoc_STRVAR(Signature_tp_doc,
32 "A string subclass whose values are restricted to valid D-Bus\n"
33 "signatures. When iterated over, instead of individual characters it\n"
34 "produces Signature instances representing single complete types.\n"
35 "\n"
36 "Constructor::\n"
37 "\n"
38 "    ``Signature(value: str or unicode[, variant_level: int]) -> Signature``\n"
39 "\n"
40 "``value`` must be a valid D-Bus signature (zero or more single complete\n"
41 "types).\n"
42 "\n"
43 "``variant_level`` must be non-negative; the default is 0.\n"
44 "\n"
45 ":IVariables:\n"
46 "  `variant_level` : int\n"
47 "    Indicates how many nested Variant containers this object\n"
48 "    is contained in: if a message's wire format has a variant containing a\n"
49 "    variant containing a signature, this is represented in Python by a\n"
50 "    Signature with variant_level==2.\n"
53 typedef struct {
54     PyObject_HEAD
55     PyObject *string;
56     DBusSignatureIter iter;
57 } SignatureIter;
59 static void
60 SignatureIter_tp_dealloc (SignatureIter *self)
62     Py_XDECREF(self->string);
63     self->string = NULL;
64     PyObject_Del(self);
67 static PyObject *
68 SignatureIter_tp_iternext (SignatureIter *self)
70     char *sig;
71     PyObject *obj;
73     /* Stop immediately if finished or not correctly initialized */
74     if (!self->string) return NULL;
76     sig = dbus_signature_iter_get_signature(&(self->iter));
77     if (!sig) return PyErr_NoMemory();
78     obj = PyObject_CallFunction((PyObject *)&DBusPySignature_Type, "s", sig);
79     dbus_free(sig);
80     if (!obj) return NULL;
82     if (!dbus_signature_iter_next(&(self->iter))) {
83         /* mark object as having been finished with */
84         Py_DECREF(self->string);
85         self->string = NULL;
86     }
88     return obj;
91 static PyObject *
92 SignatureIter_tp_iter(PyObject *self)
94     Py_INCREF(self);
95     return self;
98 static PyTypeObject SignatureIterType = {
99     PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
100     0,
101     "_dbus_bindings._SignatureIter",
102     sizeof(SignatureIter),
103     0,
104     (destructor)SignatureIter_tp_dealloc,   /* tp_dealloc */
105     0,                                      /* tp_print */
106     0,                                      /* tp_getattr */
107     0,                                      /* tp_setattr */
108     0,                                      /* tp_compare */
109     0,                                      /* tp_repr */
110     0,                                      /* tp_as_number */
111     0,                                      /* tp_as_sequence */
112     0,                                      /* tp_as_mapping */
113     0,                                      /* tp_hash */
114     0,                                      /* tp_call */
115     0,                                      /* tp_str */
116     0,                                      /* tp_getattro */
117     0,                                      /* tp_setattro */
118     0,                                      /* tp_as_buffer */
119     Py_TPFLAGS_DEFAULT,                     /* tp_flags */
120     0,                                      /* tp_doc */
121     0,                                      /* tp_traverse */
122     0,                                      /* tp_clear */
123     0,                                      /* tp_richcompare */
124     0,                                      /* tp_weaklistoffset */
125     SignatureIter_tp_iter,                  /* tp_iter */
126     (iternextfunc)SignatureIter_tp_iternext,  /* tp_iternext */
127     0,                                      /* tp_methods */
128     0,                                      /* tp_members */
129     0,                                      /* tp_getset */
130     0,                                      /* tp_base */
131     0,                                      /* tp_dict */
132     0,                                      /* tp_descr_get */
133     0,                                      /* tp_descr_set */
134     0,                                      /* tp_dictoffset */
135     0,                                      /* tp_init */
136     0,                                      /* tp_alloc */
137     /* deliberately not callable! Use iter(Signature) instead */
138     0,                                      /* tp_new */
139     0,                                      /* tp_free */
142 static PyObject *
143 Signature_tp_iter (PyObject *self)
145     SignatureIter *iter = PyObject_New(SignatureIter, &SignatureIterType);
146     if (!iter) return NULL;
148     if (PyString_AS_STRING (self)[0]) {
149         Py_INCREF(self);
150         iter->string = self;
151         dbus_signature_iter_init(&(iter->iter), PyString_AS_STRING(self));
152     }
153     else {
154         /* this is a null string, make a null iterator */
155         iter->string = NULL;
156     }
157     return (PyObject *)iter;
160 static PyObject *
161 Signature_tp_new (PyTypeObject *cls, PyObject *args, PyObject *kwargs)
163     const char *str = NULL;
164     PyObject *ignored;
165     static char *argnames[] = {"object_path", "variant_level", NULL};
167     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|O:__new__", argnames,
168                                      &str, &ignored)) return NULL;
169     if (!dbus_signature_validate(str, NULL)) {
170         PyErr_SetString(PyExc_ValueError, "Corrupt type signature");
171         return NULL;
172     }
173     return (DBusPyStrBase_Type.tp_new)(cls, args, kwargs);
176 PyTypeObject DBusPySignature_Type = {
177     PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
178     0,
179     "dbus.Signature",
180     0,
181     0,
182     0,                                      /* tp_dealloc */
183     0,                                      /* tp_print */
184     0,                                      /* tp_getattr */
185     0,                                      /* tp_setattr */
186     0,                                      /* tp_compare */
187     0,                                      /* tp_repr */
188     0,                                      /* tp_as_number */
189     0,                                      /* tp_as_sequence */
190     0,                                      /* tp_as_mapping */
191     0,                                      /* tp_hash */
192     0,                                      /* tp_call */
193     0,                                      /* tp_str */
194     0,                                      /* tp_getattro */
195     0,                                      /* tp_setattro */
196     0,                                      /* tp_as_buffer */
197     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
198     Signature_tp_doc,                       /* tp_doc */
199     0,                                      /* tp_traverse */
200     0,                                      /* tp_clear */
201     0,                                      /* tp_richcompare */
202     0,                                      /* tp_weaklistoffset */
203     Signature_tp_iter,                      /* tp_iter */
204     0,                                      /* tp_iternext */
205     0,                                      /* tp_methods */
206     0,                                      /* tp_members */
207     0,                                      /* tp_getset */
208     DEFERRED_ADDRESS(&DBusPythonStringType), /* tp_base */
209     0,                                      /* tp_dict */
210     0,                                      /* tp_descr_get */
211     0,                                      /* tp_descr_set */
212     0,                                      /* tp_dictoffset */
213     0,                                      /* tp_init */
214     0,                                      /* tp_alloc */
215     Signature_tp_new,                       /* tp_new */
216     0,                                      /* tp_free */
219 dbus_bool_t
220 dbus_py_init_signature(void)
222     if (PyType_Ready(&SignatureIterType) < 0) return 0;
224     DBusPySignature_Type.tp_base = &DBusPyStrBase_Type;
225     if (PyType_Ready(&DBusPySignature_Type) < 0) return 0;
226     DBusPySignature_Type.tp_print = NULL;
228     return 1;
231 dbus_bool_t
232 dbus_py_insert_signature(PyObject *this_module)
234     Py_INCREF(&DBusPySignature_Type);
235     if (PyModule_AddObject(this_module, "Signature",
236                            (PyObject *)&DBusPySignature_Type) < 0) return 0;
237     Py_INCREF(&SignatureIterType);
238     if (PyModule_AddObject(this_module, "_SignatureIter",
239                            (PyObject *)&SignatureIterType) < 0) return 0;
241     return 1;
244 /* vim:set ft=c cino< sw=4 sts=4 et: */