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 PyDoc_STRVAR(Signature_tp_doc
,
26 "Signature(str) -> Signature\n"
28 "Signature is a string subclass whose values are restricted to valid D-Bus\n"
29 "signatures. When iterated over, instead of individual characters it\n"
30 "produces Signature instances representing single complete types.\n"
33 static PyTypeObject SignatureType
;
35 static inline int Signature_Check(PyObject
*o
)
37 return (o
->ob_type
== &SignatureType
)
38 || PyObject_IsInstance(o
, (PyObject
*)&SignatureType
);
44 DBusSignatureIter iter
;
48 SignatureIter_tp_dealloc (SignatureIter
*self
)
50 Py_XDECREF(self
->string
);
56 SignatureIter_tp_iternext (SignatureIter
*self
)
61 /* Stop immediately if finished or not correctly initialized */
62 if (!self
->string
) return NULL
;
64 sig
= dbus_signature_iter_get_signature(&(self
->iter
));
65 if (!sig
) return PyErr_NoMemory();
66 obj
= PyObject_CallFunction((PyObject
*)&SignatureType
, "s", sig
);
68 if (!obj
) return NULL
;
70 if (!dbus_signature_iter_next(&(self
->iter
))) {
71 /* mark object as having been finished with */
72 Py_DECREF(self
->string
);
80 SignatureIter_tp_iter(PyObject
*self
)
86 static PyTypeObject SignatureIterType
= {
87 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
89 "_dbus_bindings._SignatureIter",
90 sizeof(SignatureIter
),
92 (destructor
)SignatureIter_tp_dealloc
, /* tp_dealloc */
99 0, /* tp_as_sequence */
100 0, /* tp_as_mapping */
106 0, /* tp_as_buffer */
107 Py_TPFLAGS_DEFAULT
, /* tp_flags */
111 0, /* tp_richcompare */
112 0, /* tp_weaklistoffset */
113 SignatureIter_tp_iter
, /* tp_iter */
114 (iternextfunc
)SignatureIter_tp_iternext
, /* tp_iternext */
120 0, /* tp_descr_get */
121 0, /* tp_descr_set */
122 0, /* tp_dictoffset */
125 /* deliberately not callable! Use iter(Signature) instead */
131 Signature_tp_iter (PyObject
*self
)
133 SignatureIter
*iter
= PyObject_New(SignatureIter
, &SignatureIterType
);
134 if (!iter
) return NULL
;
136 if (PyString_AS_STRING (self
)[0]) {
139 dbus_signature_iter_init(&(iter
->iter
), PyString_AS_STRING(self
));
142 /* this is a null string, make a null iterator */
145 return (PyObject
*)iter
;
149 Signature_tp_new (PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
151 PyObject
*tuple
, *self
;
152 const char *str
= NULL
;
153 static char *argnames
[] = {"object_path", NULL
};
155 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s:__new__", argnames
,
157 if (!dbus_signature_validate(str
, NULL
)) {
158 PyErr_SetString(PyExc_ValueError
, "Corrupt type signature");
161 tuple
= Py_BuildValue("(s)", str
);
162 if (!tuple
) return NULL
;
163 self
= PyString_Type
.tp_new(cls
, tuple
, NULL
);
168 static PyTypeObject SignatureType
= {
169 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
179 str_subclass_tp_repr
, /* tp_repr */
180 0, /* tp_as_number */
181 0, /* tp_as_sequence */
182 0, /* tp_as_mapping */
188 0, /* tp_as_buffer */
189 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
190 Signature_tp_doc
, /* tp_doc */
193 0, /* tp_richcompare */
194 0, /* tp_weaklistoffset */
195 Signature_tp_iter
, /* tp_iter */
200 DEFERRED_ADDRESS(&PyString_Type
), /* tp_base */
202 0, /* tp_descr_get */
203 0, /* tp_descr_set */
204 0, /* tp_dictoffset */
207 Signature_tp_new
, /* tp_new */
214 if (PyType_Ready(&SignatureIterType
) < 0) return 0;
216 SignatureType
.tp_base
= &PyString_Type
;
217 if (PyType_Ready(&SignatureType
) < 0) return 0;
218 /* disable the tp_print copied from PyString_Type, so tp_repr gets called as
220 SignatureType
.tp_print
= NULL
;
226 insert_signature (PyObject
*this_module
)
228 Py_INCREF(&SignatureType
);
229 if (PyModule_AddObject(this_module
, "Signature",
230 (PyObject
*)&SignatureType
) < 0) return 0;
231 Py_INCREF(&SignatureIterType
);
232 if (PyModule_AddObject(this_module
, "_SignatureIter",
233 (PyObject
*)&SignatureIterType
) < 0) return 0;
238 /* vim:set ft=c cino< sw=4 sts=4 et: */