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