Update NEWS, README for 0.80.0
[dbus-python-phuang.git] / _dbus_bindings / signature.c
blobcb1d55dcaf3ec24beba3a417e9a9b0af01c1472d
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 "\n"
40 " ``Signature(value: str or unicode[, variant_level: int]) -> Signature``\n"
41 "\n"
42 "``value`` must be a valid D-Bus signature (zero or more single complete\n"
43 "types).\n"
44 "\n"
45 "``variant_level`` must be non-negative; the default is 0.\n"
46 "\n"
47 ":IVariables:\n"
48 " `variant_level` : int\n"
49 " Indicates how many nested Variant containers this object\n"
50 " is contained in: if a message's wire format has a variant containing a\n"
51 " variant containing a signature, this is represented in Python by a\n"
52 " Signature with variant_level==2.\n"
55 typedef struct {
56 PyObject_HEAD
57 PyObject *string;
58 DBusSignatureIter iter;
59 } SignatureIter;
61 static void
62 SignatureIter_tp_dealloc (SignatureIter *self)
64 Py_XDECREF(self->string);
65 self->string = NULL;
66 PyObject_Del(self);
69 static PyObject *
70 SignatureIter_tp_iternext (SignatureIter *self)
72 char *sig;
73 PyObject *obj;
75 /* Stop immediately if finished or not correctly initialized */
76 if (!self->string) return NULL;
78 sig = dbus_signature_iter_get_signature(&(self->iter));
79 if (!sig) return PyErr_NoMemory();
80 obj = PyObject_CallFunction((PyObject *)&DBusPySignature_Type, "s", sig);
81 dbus_free(sig);
82 if (!obj) return NULL;
84 if (!dbus_signature_iter_next(&(self->iter))) {
85 /* mark object as having been finished with */
86 Py_DECREF(self->string);
87 self->string = NULL;
90 return obj;
93 static PyObject *
94 SignatureIter_tp_iter(PyObject *self)
96 Py_INCREF(self);
97 return self;
100 static PyTypeObject SignatureIterType = {
101 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
103 "_dbus_bindings._SignatureIter",
104 sizeof(SignatureIter),
106 (destructor)SignatureIter_tp_dealloc, /* tp_dealloc */
107 0, /* tp_print */
108 0, /* tp_getattr */
109 0, /* tp_setattr */
110 0, /* tp_compare */
111 0, /* tp_repr */
112 0, /* tp_as_number */
113 0, /* tp_as_sequence */
114 0, /* tp_as_mapping */
115 0, /* tp_hash */
116 0, /* tp_call */
117 0, /* tp_str */
118 0, /* tp_getattro */
119 0, /* tp_setattro */
120 0, /* tp_as_buffer */
121 Py_TPFLAGS_DEFAULT, /* tp_flags */
122 0, /* tp_doc */
123 0, /* tp_traverse */
124 0, /* tp_clear */
125 0, /* tp_richcompare */
126 0, /* tp_weaklistoffset */
127 SignatureIter_tp_iter, /* tp_iter */
128 (iternextfunc)SignatureIter_tp_iternext, /* tp_iternext */
129 0, /* tp_methods */
130 0, /* tp_members */
131 0, /* tp_getset */
132 0, /* tp_base */
133 0, /* tp_dict */
134 0, /* tp_descr_get */
135 0, /* tp_descr_set */
136 0, /* tp_dictoffset */
137 0, /* tp_init */
138 0, /* tp_alloc */
139 /* deliberately not callable! Use iter(Signature) instead */
140 0, /* tp_new */
141 0, /* tp_free */
144 static PyObject *
145 Signature_tp_iter (PyObject *self)
147 SignatureIter *iter = PyObject_New(SignatureIter, &SignatureIterType);
148 if (!iter) return NULL;
150 if (PyString_AS_STRING (self)[0]) {
151 Py_INCREF(self);
152 iter->string = self;
153 dbus_signature_iter_init(&(iter->iter), PyString_AS_STRING(self));
155 else {
156 /* this is a null string, make a null iterator */
157 iter->string = NULL;
159 return (PyObject *)iter;
162 static PyObject *
163 Signature_tp_new (PyTypeObject *cls, PyObject *args, PyObject *kwargs)
165 const char *str = NULL;
166 PyObject *ignored;
167 static char *argnames[] = {"object_path", "variant_level", NULL};
169 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|O:__new__", argnames,
170 &str, &ignored)) return NULL;
171 if (!dbus_signature_validate(str, NULL)) {
172 PyErr_SetString(PyExc_ValueError, "Corrupt type signature");
173 return NULL;
175 return (DBusPyStrBase_Type.tp_new)(cls, args, kwargs);
178 PyTypeObject DBusPySignature_Type = {
179 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
181 "dbus.Signature",
184 0, /* tp_dealloc */
185 0, /* tp_print */
186 0, /* tp_getattr */
187 0, /* tp_setattr */
188 0, /* tp_compare */
189 0, /* tp_repr */
190 0, /* tp_as_number */
191 0, /* tp_as_sequence */
192 0, /* tp_as_mapping */
193 0, /* tp_hash */
194 0, /* tp_call */
195 0, /* tp_str */
196 0, /* tp_getattro */
197 0, /* tp_setattro */
198 0, /* tp_as_buffer */
199 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
200 Signature_tp_doc, /* tp_doc */
201 0, /* tp_traverse */
202 0, /* tp_clear */
203 0, /* tp_richcompare */
204 0, /* tp_weaklistoffset */
205 Signature_tp_iter, /* tp_iter */
206 0, /* tp_iternext */
207 0, /* tp_methods */
208 0, /* tp_members */
209 0, /* tp_getset */
210 DEFERRED_ADDRESS(&DBusPythonStringType), /* tp_base */
211 0, /* tp_dict */
212 0, /* tp_descr_get */
213 0, /* tp_descr_set */
214 0, /* tp_dictoffset */
215 0, /* tp_init */
216 0, /* tp_alloc */
217 Signature_tp_new, /* tp_new */
218 0, /* tp_free */
221 dbus_bool_t
222 dbus_py_init_signature(void)
224 if (PyType_Ready(&SignatureIterType) < 0) return 0;
226 DBusPySignature_Type.tp_base = &DBusPyStrBase_Type;
227 if (PyType_Ready(&DBusPySignature_Type) < 0) return 0;
228 DBusPySignature_Type.tp_print = NULL;
230 return 1;
233 dbus_bool_t
234 dbus_py_insert_signature(PyObject *this_module)
236 Py_INCREF(&DBusPySignature_Type);
237 if (PyModule_AddObject(this_module, "Signature",
238 (PyObject *)&DBusPySignature_Type) < 0) return 0;
239 Py_INCREF(&SignatureIterType);
240 if (PyModule_AddObject(this_module, "_SignatureIter",
241 (PyObject *)&SignatureIterType) < 0) return 0;
243 return 1;
246 /* vim:set ft=c cino< sw=4 sts=4 et: */