1 /* Simple D-Bus types: ObjectPath and other string types.
3 * Copyright (C) 2006-2007 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.
26 #include "types-internal.h"
27 #include <structmember.h>
29 /* UTF-8 string representation ====================================== */
31 PyDoc_STRVAR(UTF8String_tp_doc
,
32 "A string represented using UTF-8 - a subtype of `str`.\n"
34 "All strings on D-Bus are required to be valid Unicode; in the \"wire\n"
35 "protocol\" they're transported as UTF-8.\n"
37 "By default, when byte arrays are converted from D-Bus to Python, they\n"
38 "come out as a `dbus.String`, which is a subtype of `unicode`.\n"
39 "If you prefer to get UTF-8 strings (as instances of this class) or you\n"
40 "want to avoid the conversion overhead of going from UTF-8 to Python's\n"
41 "internal Unicode representation, you can pass the ``utf8_strings=True``\n"
42 "keyword argument to any of these methods:\n"
44 "* any D-Bus method proxy, or ``connect_to_signal``, on the objects returned\n"
45 " by `Bus.get_object`\n"
46 "* any D-Bus method on a `dbus.Interface`\n"
47 "* `dbus.Interface.connect_to_signal`\n"
48 "* `Bus.add_signal_receiver`\n"
53 " dbus.UTF8String(value: str or unicode[, variant_level: int]) -> UTF8String\n"
55 "If value is a str object it must be valid UTF-8.\n"
57 "variant_level must be non-negative; the default is 0.\n"
60 " `variant_level` : int\n"
61 " Indicates how many nested Variant containers this object\n"
62 " is contained in: if a message's wire format has a variant containing a\n"
63 " variant containing a string, this is represented in Python by a\n"
64 " String or UTF8String with variant_level==2.\n"
65 ":Since: 0.80 (in older versions, use dbus.String)\n"
69 UTF8String_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
71 const char *str
= NULL
;
73 static char *argnames
[] = {"value", "variant_level", NULL
};
76 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|l:__new__", argnames
,
77 &str
, &variantness
)) return NULL
;
78 unicode
= PyUnicode_DecodeUTF8(str
, strlen(str
), NULL
);
79 if (!unicode
) return NULL
;
81 return (DBusPyStrBase_Type
.tp_new
)(cls
, args
, kwargs
);
84 PyTypeObject DBusPyUTF8String_Type
= {
85 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
97 0, /* tp_as_sequence */
98 0, /* tp_as_mapping */
104 0, /* tp_as_buffer */
105 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
106 UTF8String_tp_doc
, /* tp_doc */
109 0, /* tp_richcompare */
110 0, /* tp_weaklistoffset */
116 DEFERRED_ADDRESS(&DBusPyStrBase_Type
), /* tp_base */
118 0, /* tp_descr_get */
119 0, /* tp_descr_set */
120 0, /* tp_dictoffset */
123 UTF8String_tp_new
, /* tp_new */
126 /* Object path ====================================================== */
128 PyDoc_STRVAR(ObjectPath_tp_doc
,
129 "A D-Bus object path, such as '/com/example/MyApp/Documents/abc'.\n"
131 "ObjectPath is a subtype of str, and object-paths behave like strings.\n"
135 " dbus.ObjectPath(path: str, variant_level: int) -> ObjectPath\n"
137 "path must be an ASCII string following the syntax of object paths.\n"
138 "variant_level must be non-negative; the default is 0.\n"
141 " `variant_level` : int\n"
142 " Indicates how many nested Variant containers this object\n"
143 " is contained in: if a message's wire format has a variant containing a\n"
144 " variant containing an object path, this is represented in Python by an\n"
145 " ObjectPath with variant_level==2.\n"
149 ObjectPath_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
151 const char *str
= NULL
;
152 long variantness
= 0;
153 static char *argnames
[] = {"object_path", "variant_level", NULL
};
155 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|l:__new__", argnames
,
156 &str
, &variantness
)) return NULL
;
157 if (!dbus_py_validate_object_path(str
)) {
160 return (DBusPyStrBase_Type
.tp_new
)(cls
, args
, kwargs
);
163 PyTypeObject DBusPyObjectPath_Type
= {
164 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
175 0, /* tp_as_number */
176 0, /* tp_as_sequence */
177 0, /* tp_as_mapping */
183 0, /* tp_as_buffer */
184 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
185 ObjectPath_tp_doc
, /* tp_doc */
188 0, /* tp_richcompare */
189 0, /* tp_weaklistoffset */
195 DEFERRED_ADDRESS(&DBusPyStrBase_Type
), /* tp_base */
197 0, /* tp_descr_get */
198 0, /* tp_descr_set */
199 0, /* tp_dictoffset */
202 ObjectPath_tp_new
, /* tp_new */
205 /* Unicode string representation ==================================== */
207 PyDoc_STRVAR(String_tp_doc
,
208 "A string represented using Unicode - a subtype of `unicode`.\n"
210 "All strings on D-Bus are required to be valid Unicode; in the \"wire\n"
211 "protocol\" they're transported as UTF-8.\n"
213 "By default, when strings are converted from D-Bus to Python, they\n"
214 "come out as this class. If you prefer to get UTF-8 strings (as instances\n"
215 "of a subtype of `str`) or you want to avoid the conversion overhead of\n"
216 "going from UTF-8 to Python's internal Unicode representation, see the\n"
217 "documentation for `dbus.UTF8String`.\n"
221 " String(value: str or unicode[, variant_level: int]) -> String\n"
223 "variant_level must be non-negative; the default is 0.\n"
226 " `variant_level` : int\n"
227 " Indicates how many nested Variant containers this object\n"
228 " is contained in: if a message's wire format has a variant containing a\n"
229 " variant containing a string, this is represented in Python by a\n"
230 " String or UTF8String with variant_level==2.\n"
233 static PyMemberDef String_tp_members
[] = {
234 {"variant_level", T_LONG
, offsetof(DBusPyString
, variant_level
),
236 "The number of nested variants wrapping the real data. "
237 "0 if not in a variant"},
242 String_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
245 long variantness
= 0;
246 static char *argnames
[] = {"variant_level", NULL
};
248 if (PyTuple_Size(args
) > 1) {
249 PyErr_SetString(PyExc_TypeError
,
250 "__new__ takes at most one positional parameter");
253 if (!PyArg_ParseTupleAndKeywords(dbus_py_empty_tuple
, kwargs
,
254 "|l:__new__", argnames
,
255 &variantness
)) return NULL
;
256 if (variantness
< 0) {
257 PyErr_SetString(PyExc_ValueError
,
258 "variant_level must be non-negative");
261 self
= (PyUnicode_Type
.tp_new
)(cls
, args
, NULL
);
263 ((DBusPyString
*)self
)->variant_level
= variantness
;
269 String_tp_repr(PyObject
*self
)
271 PyObject
*parent_repr
= (PyUnicode_Type
.tp_repr
)(self
);
277 if (((DBusPyString
*)self
)->variant_level
> 0) {
278 my_repr
= PyString_FromFormat("%s(%s, variant_level=%ld)",
279 self
->ob_type
->tp_name
,
280 PyString_AS_STRING(parent_repr
),
281 ((DBusPyString
*)self
)->variant_level
);
284 my_repr
= PyString_FromFormat("%s(%s)", self
->ob_type
->tp_name
,
285 PyString_AS_STRING(parent_repr
));
287 /* whether my_repr is NULL or not: */
288 Py_DECREF(parent_repr
);
292 PyTypeObject DBusPyString_Type
= {
293 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
296 sizeof(DBusPyString
),
303 String_tp_repr
, /* tp_repr */
304 0, /* tp_as_number */
305 0, /* tp_as_sequence */
306 0, /* tp_as_mapping */
310 PyObject_GenericGetAttr
, /* tp_getattro */
311 dbus_py_immutable_setattro
, /* tp_setattro */
312 0, /* tp_as_buffer */
313 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
314 String_tp_doc
, /* tp_doc */
317 0, /* tp_richcompare */
318 0, /* tp_weaklistoffset */
322 String_tp_members
, /* tp_members */
324 DEFERRED_ADDRESS(&PyUnicode_Type
), /* tp_base */
326 0, /* tp_descr_get */
327 0, /* tp_descr_set */
328 0, /* tp_dictoffset */
331 String_tp_new
, /* tp_new */
335 dbus_py_init_string_types(void)
337 /* don't need to do strange contortions for unicode, since it's not a
338 * "variable-size" object (it has a pointer to its data instead)
340 if (PyUnicode_Type
.tp_itemsize
!= 0) {
341 fprintf(stderr
, "dbus-python is not compatible with this version of "
342 "Python (unicode objects are assumed to be fixed-size)");
345 DBusPyString_Type
.tp_base
= &PyUnicode_Type
;
346 if (PyType_Ready(&DBusPyString_Type
) < 0) return 0;
347 DBusPyString_Type
.tp_print
= NULL
;
349 DBusPyUTF8String_Type
.tp_base
= &DBusPyStrBase_Type
;
350 if (PyType_Ready(&DBusPyUTF8String_Type
) < 0) return 0;
351 DBusPyUTF8String_Type
.tp_print
= NULL
;
353 DBusPyObjectPath_Type
.tp_base
= &DBusPyStrBase_Type
;
354 if (PyType_Ready(&DBusPyObjectPath_Type
) < 0) return 0;
355 DBusPyObjectPath_Type
.tp_print
= NULL
;
357 DBusPyBoolean_Type
.tp_base
= &DBusPyIntBase_Type
;
358 if (PyType_Ready(&DBusPyBoolean_Type
) < 0) return 0;
359 DBusPyBoolean_Type
.tp_print
= NULL
;
365 dbus_py_insert_string_types(PyObject
*this_module
)
367 Py_INCREF(&DBusPyObjectPath_Type
);
368 Py_INCREF(&DBusPyUTF8String_Type
);
369 Py_INCREF(&DBusPyString_Type
);
370 if (PyModule_AddObject(this_module
, "ObjectPath",
371 (PyObject
*)&DBusPyObjectPath_Type
) < 0) return 0;
372 if (PyModule_AddObject(this_module
, "UTF8String",
373 (PyObject
*)&DBusPyUTF8String_Type
) < 0) return 0;
374 if (PyModule_AddObject(this_module
, "String",
375 (PyObject
*)&DBusPyString_Type
) < 0) return 0;
380 /* vim:set ft=c cino< sw=4 sts=4 et: */