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"
39 " Signature(value: str or unicode[, variant_level: int]) -> Signature\n"
41 " value must be a valid D-Bus signature (zero or more single complete\n"
44 " variant_level must be non-negative; the default is 0.\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"
57 DBusSignatureIter iter
;
61 SignatureIter_tp_dealloc (SignatureIter
*self
)
63 Py_XDECREF(self
->string
);
69 SignatureIter_tp_iternext (SignatureIter
*self
)
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
);
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
);
93 SignatureIter_tp_iter(PyObject
*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 */
111 0, /* tp_as_number */
112 0, /* tp_as_sequence */
113 0, /* tp_as_mapping */
119 0, /* tp_as_buffer */
120 Py_TPFLAGS_DEFAULT
, /* tp_flags */
124 0, /* tp_richcompare */
125 0, /* tp_weaklistoffset */
126 SignatureIter_tp_iter
, /* tp_iter */
127 (iternextfunc
)SignatureIter_tp_iternext
, /* tp_iternext */
133 0, /* tp_descr_get */
134 0, /* tp_descr_set */
135 0, /* tp_dictoffset */
138 /* deliberately not callable! Use iter(Signature) instead */
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]) {
152 dbus_signature_iter_init(&(iter
->iter
), PyString_AS_STRING(self
));
155 /* this is a null string, make a null iterator */
158 return (PyObject
*)iter
;
162 Signature_tp_new (PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
164 const char *str
= NULL
;
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");
174 return (DBusPyStrBase_Type
.tp_new
)(cls
, args
, kwargs
);
177 PyTypeObject DBusPySignature_Type
= {
178 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
189 0, /* tp_as_number */
190 0, /* tp_as_sequence */
191 0, /* tp_as_mapping */
197 0, /* tp_as_buffer */
198 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
199 Signature_tp_doc
, /* tp_doc */
202 0, /* tp_richcompare */
203 0, /* tp_weaklistoffset */
204 Signature_tp_iter
, /* tp_iter */
209 DEFERRED_ADDRESS(&DBusPythonStringType
), /* tp_base */
211 0, /* tp_descr_get */
212 0, /* tp_descr_set */
213 0, /* tp_dictoffset */
216 Signature_tp_new
, /* tp_new */
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
;
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;
245 /* vim:set ft=c cino< sw=4 sts=4 et: */