1 /* Implementation of Signature type for D-Bus bindings.
3 * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use, copy,
9 * modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
27 #include <structmember.h>
31 #include "dbus_bindings-internal.h"
32 #include "types-internal.h"
34 PyDoc_STRVAR(Signature_tp_doc
,
35 "A string subclass whose values are restricted to valid D-Bus\n"
36 "signatures. When iterated over, instead of individual characters it\n"
37 "produces Signature instances representing single complete types.\n"
41 " ``Signature(value: str or unicode[, variant_level: int]) -> Signature``\n"
43 "``value`` must be a valid D-Bus signature (zero or more single complete\n"
46 "``variant_level`` must be non-negative; the default is 0.\n"
49 " `variant_level` : int\n"
50 " Indicates how many nested Variant containers this object\n"
51 " is contained in: if a message's wire format has a variant containing a\n"
52 " variant containing a signature, this is represented in Python by a\n"
53 " Signature with variant_level==2.\n"
59 DBusSignatureIter iter
;
63 SignatureIter_tp_dealloc (SignatureIter
*self
)
65 Py_XDECREF(self
->string
);
71 SignatureIter_tp_iternext (SignatureIter
*self
)
76 /* Stop immediately if finished or not correctly initialized */
77 if (!self
->string
) return NULL
;
79 sig
= dbus_signature_iter_get_signature(&(self
->iter
));
80 if (!sig
) return PyErr_NoMemory();
81 obj
= PyObject_CallFunction((PyObject
*)&DBusPySignature_Type
, "s", sig
);
83 if (!obj
) return NULL
;
85 if (!dbus_signature_iter_next(&(self
->iter
))) {
86 /* mark object as having been finished with */
87 Py_DECREF(self
->string
);
95 SignatureIter_tp_iter(PyObject
*self
)
101 static PyTypeObject SignatureIterType
= {
102 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
104 "_dbus_bindings._SignatureIter",
105 sizeof(SignatureIter
),
107 (destructor
)SignatureIter_tp_dealloc
, /* tp_dealloc */
113 0, /* tp_as_number */
114 0, /* tp_as_sequence */
115 0, /* tp_as_mapping */
121 0, /* tp_as_buffer */
122 Py_TPFLAGS_DEFAULT
, /* tp_flags */
126 0, /* tp_richcompare */
127 0, /* tp_weaklistoffset */
128 SignatureIter_tp_iter
, /* tp_iter */
129 (iternextfunc
)SignatureIter_tp_iternext
, /* tp_iternext */
135 0, /* tp_descr_get */
136 0, /* tp_descr_set */
137 0, /* tp_dictoffset */
140 /* deliberately not callable! Use iter(Signature) instead */
146 Signature_tp_iter (PyObject
*self
)
148 SignatureIter
*iter
= PyObject_New(SignatureIter
, &SignatureIterType
);
149 if (!iter
) return NULL
;
151 if (PyString_AS_STRING (self
)[0]) {
154 dbus_signature_iter_init(&(iter
->iter
), PyString_AS_STRING(self
));
157 /* this is a null string, make a null iterator */
160 return (PyObject
*)iter
;
164 Signature_tp_new (PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
166 const char *str
= NULL
;
168 static char *argnames
[] = {"object_path", "variant_level", NULL
};
170 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|O:__new__", argnames
,
171 &str
, &ignored
)) return NULL
;
172 if (!dbus_signature_validate(str
, NULL
)) {
173 PyErr_SetString(PyExc_ValueError
, "Corrupt type signature");
176 return (DBusPyStrBase_Type
.tp_new
)(cls
, args
, kwargs
);
179 PyTypeObject DBusPySignature_Type
= {
180 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
191 0, /* tp_as_number */
192 0, /* tp_as_sequence */
193 0, /* tp_as_mapping */
199 0, /* tp_as_buffer */
200 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
201 Signature_tp_doc
, /* tp_doc */
204 0, /* tp_richcompare */
205 0, /* tp_weaklistoffset */
206 Signature_tp_iter
, /* tp_iter */
211 DEFERRED_ADDRESS(&DBusPythonStringType
), /* tp_base */
213 0, /* tp_descr_get */
214 0, /* tp_descr_set */
215 0, /* tp_dictoffset */
218 Signature_tp_new
, /* tp_new */
223 dbus_py_init_signature(void)
225 if (PyType_Ready(&SignatureIterType
) < 0) return 0;
227 DBusPySignature_Type
.tp_base
= &DBusPyStrBase_Type
;
228 if (PyType_Ready(&DBusPySignature_Type
) < 0) return 0;
229 DBusPySignature_Type
.tp_print
= NULL
;
235 dbus_py_insert_signature(PyObject
*this_module
)
237 Py_INCREF(&DBusPySignature_Type
);
238 if (PyModule_AddObject(this_module
, "Signature",
239 (PyObject
*)&DBusPySignature_Type
) < 0) return 0;
240 Py_INCREF(&SignatureIterType
);
241 if (PyModule_AddObject(this_module
, "_SignatureIter",
242 (PyObject
*)&SignatureIterType
) < 0) return 0;
247 /* vim:set ft=c cino< sw=4 sts=4 et: */