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 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.
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.
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
24 #include <structmember.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"
38 " ``Signature(value: str or unicode[, variant_level: int]) -> Signature``\n"
40 "``value`` must be a valid D-Bus signature (zero or more single complete\n"
43 "``variant_level`` must be non-negative; the default is 0.\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"
56 DBusSignatureIter iter;
60 SignatureIter_tp_dealloc (SignatureIter *self)
62 Py_XDECREF(self->string);
68 SignatureIter_tp_iternext (SignatureIter *self)
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);
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);
92 SignatureIter_tp_iter(PyObject *self)
98 static PyTypeObject SignatureIterType = {
99 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
101 "_dbus_bindings._SignatureIter",
102 sizeof(SignatureIter),
104 (destructor)SignatureIter_tp_dealloc, /* tp_dealloc */
110 0, /* tp_as_number */
111 0, /* tp_as_sequence */
112 0, /* tp_as_mapping */
118 0, /* tp_as_buffer */
119 Py_TPFLAGS_DEFAULT, /* tp_flags */
123 0, /* tp_richcompare */
124 0, /* tp_weaklistoffset */
125 SignatureIter_tp_iter, /* tp_iter */
126 (iternextfunc)SignatureIter_tp_iternext, /* tp_iternext */
132 0, /* tp_descr_get */
133 0, /* tp_descr_set */
134 0, /* tp_dictoffset */
137 /* deliberately not callable! Use iter(Signature) instead */
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]) {
151 dbus_signature_iter_init(&(iter->iter), PyString_AS_STRING(self));
154 /* this is a null string, make a null iterator */
157 return (PyObject *)iter;
161 Signature_tp_new (PyTypeObject *cls, PyObject *args, PyObject *kwargs)
163 const char *str = NULL;
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");
173 return (DBusPyStrBase_Type.tp_new)(cls, args, kwargs);
176 PyTypeObject DBusPySignature_Type = {
177 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
188 0, /* tp_as_number */
189 0, /* tp_as_sequence */
190 0, /* tp_as_mapping */
196 0, /* tp_as_buffer */
197 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
198 Signature_tp_doc, /* tp_doc */
201 0, /* tp_richcompare */
202 0, /* tp_weaklistoffset */
203 Signature_tp_iter, /* tp_iter */
208 DEFERRED_ADDRESS(&DBusPythonStringType), /* tp_base */
210 0, /* tp_descr_get */
211 0, /* tp_descr_set */
212 0, /* tp_dictoffset */
215 Signature_tp_new, /* tp_new */
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;
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;
244 /* vim:set ft=c cino< sw=4 sts=4 et: */