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
26 #include <structmember.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"
40 " ``Signature(value: str or unicode[, variant_level: int]) -> Signature``\n"
42 "``value`` must be a valid D-Bus signature (zero or more single complete\n"
45 "``variant_level`` must be non-negative; the default is 0.\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"
58 DBusSignatureIter iter
;
62 SignatureIter_tp_dealloc (SignatureIter
*self
)
64 Py_XDECREF(self
->string
);
70 SignatureIter_tp_iternext (SignatureIter
*self
)
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
);
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
);
94 SignatureIter_tp_iter(PyObject
*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 */
112 0, /* tp_as_number */
113 0, /* tp_as_sequence */
114 0, /* tp_as_mapping */
120 0, /* tp_as_buffer */
121 Py_TPFLAGS_DEFAULT
, /* tp_flags */
125 0, /* tp_richcompare */
126 0, /* tp_weaklistoffset */
127 SignatureIter_tp_iter
, /* tp_iter */
128 (iternextfunc
)SignatureIter_tp_iternext
, /* tp_iternext */
134 0, /* tp_descr_get */
135 0, /* tp_descr_set */
136 0, /* tp_dictoffset */
139 /* deliberately not callable! Use iter(Signature) instead */
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]) {
153 dbus_signature_iter_init(&(iter
->iter
), PyString_AS_STRING(self
));
156 /* this is a null string, make a null iterator */
159 return (PyObject
*)iter
;
163 Signature_tp_new (PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
165 const char *str
= NULL
;
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");
175 return (DBusPyStrBase_Type
.tp_new
)(cls
, args
, kwargs
);
178 PyTypeObject DBusPySignature_Type
= {
179 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
190 0, /* tp_as_number */
191 0, /* tp_as_sequence */
192 0, /* tp_as_mapping */
198 0, /* tp_as_buffer */
199 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
200 Signature_tp_doc
, /* tp_doc */
203 0, /* tp_richcompare */
204 0, /* tp_weaklistoffset */
205 Signature_tp_iter
, /* tp_iter */
210 DEFERRED_ADDRESS(&DBusPythonStringType
), /* tp_base */
212 0, /* tp_descr_get */
213 0, /* tp_descr_set */
214 0, /* tp_dictoffset */
217 Signature_tp_new
, /* tp_new */
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
;
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;
246 /* vim:set ft=c cino< sw=4 sts=4 et: */