1 /* Simple D-Bus types: ObjectPath and other string types.
3 * Copyright (C) 2006, 2007 Collabora Ltd.
5 * Licensed under the Academic Free License version 2.1
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "types-internal.h"
24 #include <structmember.h>
26 /* UTF-8 string representation ====================================== */
28 PyDoc_STRVAR(UTF8String_tp_doc
,
29 "A string represented using UTF-8 - a subtype of `str`.\n"
31 "All strings on D-Bus are required to be valid Unicode; in the \"wire\n"
32 "protocol\" they're transported as UTF-8.\n"
34 "By default, when byte arrays are converted from D-Bus to Python, they\n"
35 "come out as a `dbus.String`, which is a subtype of `unicode`.\n"
36 "If you prefer to get UTF-8 strings (as instances of this class) or you\n"
37 "want to avoid the conversion overhead of going from UTF-8 to Python's\n"
38 "internal Unicode representation, you can pass the ``utf8_strings=True``\n"
39 "keyword argument to any of these methods:\n"
41 "* any D-Bus method proxy, or ``connect_to_signal``, on the objects returned\n"
42 " by `Bus.get_object`\n"
43 "* any D-Bus method on a `dbus.Interface`\n"
44 "* `dbus.Interface.connect_to_signal`\n"
45 "* `Bus.add_signal_receiver`\n"
50 " dbus.UTF8String(value: str or unicode[, variant_level: int]) -> UTF8String\n"
52 "If value is a str object it must be valid UTF-8.\n"
54 "variant_level must be non-negative; the default is 0.\n"
57 " `variant_level` : int\n"
58 " Indicates how many nested Variant containers this object\n"
59 " is contained in: if a message's wire format has a variant containing a\n"
60 " variant containing a string, this is represented in Python by a\n"
61 " String or UTF8String with variant_level==2.\n"
62 ":Since: 0.80 (in older versions, use dbus.String)\n"
66 UTF8String_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
68 const char *str
= NULL
;
70 static char *argnames
[] = {"value", "variant_level", NULL
};
73 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|l:__new__", argnames
,
74 &str
, &variantness
)) return NULL
;
75 unicode
= PyUnicode_DecodeUTF8(str
, strlen(str
), NULL
);
76 if (!unicode
) return NULL
;
78 return (DBusPyStrBase_Type
.tp_new
)(cls
, args
, kwargs
);
81 PyTypeObject DBusPyUTF8String_Type
= {
82 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
94 0, /* tp_as_sequence */
95 0, /* tp_as_mapping */
101 0, /* tp_as_buffer */
102 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
103 UTF8String_tp_doc
, /* tp_doc */
106 0, /* tp_richcompare */
107 0, /* tp_weaklistoffset */
113 DEFERRED_ADDRESS(&DBusPyStrBase_Type
), /* tp_base */
115 0, /* tp_descr_get */
116 0, /* tp_descr_set */
117 0, /* tp_dictoffset */
120 UTF8String_tp_new
, /* tp_new */
123 /* Object path ====================================================== */
125 PyDoc_STRVAR(ObjectPath_tp_doc
,
126 "A D-Bus object path, such as '/com/example/MyApp/Documents/abc'.\n"
128 "ObjectPath is a subtype of str, and object-paths behave like strings.\n"
132 " dbus.ObjectPath(path: str, variant_level: int) -> ObjectPath\n"
134 "path must be an ASCII string following the syntax of object paths.\n"
135 "variant_level must be non-negative; the default is 0.\n"
138 " `variant_level` : int\n"
139 " Indicates how many nested Variant containers this object\n"
140 " is contained in: if a message's wire format has a variant containing a\n"
141 " variant containing an object path, this is represented in Python by an\n"
142 " ObjectPath with variant_level==2.\n"
146 ObjectPath_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
148 const char *str
= NULL
;
149 long variantness
= 0;
150 static char *argnames
[] = {"object_path", "variant_level", NULL
};
152 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|l:__new__", argnames
,
153 &str
, &variantness
)) return NULL
;
154 if (!dbus_py_validate_object_path(str
)) {
157 return (DBusPyStrBase_Type
.tp_new
)(cls
, args
, kwargs
);
160 PyTypeObject DBusPyObjectPath_Type
= {
161 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
172 0, /* tp_as_number */
173 0, /* tp_as_sequence */
174 0, /* tp_as_mapping */
180 0, /* tp_as_buffer */
181 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
182 ObjectPath_tp_doc
, /* tp_doc */
185 0, /* tp_richcompare */
186 0, /* tp_weaklistoffset */
192 DEFERRED_ADDRESS(&DBusPyStrBase_Type
), /* tp_base */
194 0, /* tp_descr_get */
195 0, /* tp_descr_set */
196 0, /* tp_dictoffset */
199 ObjectPath_tp_new
, /* tp_new */
202 /* Unicode string representation ==================================== */
204 PyDoc_STRVAR(String_tp_doc
,
205 "A string represented using Unicode - a subtype of `unicode`.\n"
207 "All strings on D-Bus are required to be valid Unicode; in the \"wire\n"
208 "protocol\" they're transported as UTF-8.\n"
210 "By default, when strings are converted from D-Bus to Python, they\n"
211 "come out as this class. If you prefer to get UTF-8 strings (as instances\n"
212 "of a subtype of `str`) or you want to avoid the conversion overhead of\n"
213 "going from UTF-8 to Python's internal Unicode representation, see the\n"
214 "documentation for `dbus.UTF8String`.\n"
218 " String(value: str or unicode[, variant_level: int]) -> String\n"
220 "variant_level must be non-negative; the default is 0.\n"
223 " `variant_level` : int\n"
224 " Indicates how many nested Variant containers this object\n"
225 " is contained in: if a message's wire format has a variant containing a\n"
226 " variant containing a string, this is represented in Python by a\n"
227 " String or UTF8String with variant_level==2.\n"
230 static PyMemberDef String_tp_members
[] = {
231 {"variant_level", T_LONG
, offsetof(DBusPyString
, variant_level
),
233 "The number of nested variants wrapping the real data. "
234 "0 if not in a variant"},
239 String_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
242 long variantness
= 0;
243 static char *argnames
[] = {"variant_level", NULL
};
245 if (PyTuple_Size(args
) > 1) {
246 PyErr_SetString(PyExc_TypeError
,
247 "__new__ takes at most one positional parameter");
250 if (!PyArg_ParseTupleAndKeywords(dbus_py_empty_tuple
, kwargs
,
251 "|l:__new__", argnames
,
252 &variantness
)) return NULL
;
253 if (variantness
< 0) {
254 PyErr_SetString(PyExc_ValueError
,
255 "variant_level must be non-negative");
258 self
= (PyUnicode_Type
.tp_new
)(cls
, args
, NULL
);
260 ((DBusPyString
*)self
)->variant_level
= variantness
;
266 String_tp_repr(PyObject
*self
)
268 PyObject
*parent_repr
= (PyUnicode_Type
.tp_repr
)(self
);
274 if (((DBusPyString
*)self
)->variant_level
> 0) {
275 my_repr
= PyString_FromFormat("%s(%s, variant_level=%ld)",
276 self
->ob_type
->tp_name
,
277 PyString_AS_STRING(parent_repr
),
278 ((DBusPyString
*)self
)->variant_level
);
281 my_repr
= PyString_FromFormat("%s(%s)", self
->ob_type
->tp_name
,
282 PyString_AS_STRING(parent_repr
));
284 /* whether my_repr is NULL or not: */
285 Py_DECREF(parent_repr
);
289 PyTypeObject DBusPyString_Type
= {
290 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
293 sizeof(DBusPyString
),
300 String_tp_repr
, /* tp_repr */
301 0, /* tp_as_number */
302 0, /* tp_as_sequence */
303 0, /* tp_as_mapping */
307 PyObject_GenericGetAttr
, /* tp_getattro */
308 dbus_py_immutable_setattro
, /* tp_setattro */
309 0, /* tp_as_buffer */
310 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
311 String_tp_doc
, /* tp_doc */
314 0, /* tp_richcompare */
315 0, /* tp_weaklistoffset */
319 String_tp_members
, /* tp_members */
321 DEFERRED_ADDRESS(&PyUnicode_Type
), /* tp_base */
323 0, /* tp_descr_get */
324 0, /* tp_descr_set */
325 0, /* tp_dictoffset */
328 String_tp_new
, /* tp_new */
332 dbus_py_init_string_types(void)
334 /* don't need to do strange contortions for unicode, since it's not a
335 * "variable-size" object (it has a pointer to its data instead)
337 if (PyUnicode_Type
.tp_itemsize
!= 0) {
338 fprintf(stderr
, "dbus-python is not compatible with this version of "
339 "Python (unicode objects are assumed to be fixed-size)");
342 DBusPyString_Type
.tp_base
= &PyUnicode_Type
;
343 if (PyType_Ready(&DBusPyString_Type
) < 0) return 0;
344 DBusPyString_Type
.tp_print
= NULL
;
346 DBusPyUTF8String_Type
.tp_base
= &DBusPyStrBase_Type
;
347 if (PyType_Ready(&DBusPyUTF8String_Type
) < 0) return 0;
348 DBusPyUTF8String_Type
.tp_print
= NULL
;
350 DBusPyObjectPath_Type
.tp_base
= &DBusPyStrBase_Type
;
351 if (PyType_Ready(&DBusPyObjectPath_Type
) < 0) return 0;
352 DBusPyObjectPath_Type
.tp_print
= NULL
;
354 DBusPyBoolean_Type
.tp_base
= &DBusPyIntBase_Type
;
355 if (PyType_Ready(&DBusPyBoolean_Type
) < 0) return 0;
356 DBusPyBoolean_Type
.tp_print
= NULL
;
362 dbus_py_insert_string_types(PyObject
*this_module
)
364 Py_INCREF(&DBusPyObjectPath_Type
);
365 Py_INCREF(&DBusPyUTF8String_Type
);
366 Py_INCREF(&DBusPyString_Type
);
367 if (PyModule_AddObject(this_module
, "ObjectPath",
368 (PyObject
*)&DBusPyObjectPath_Type
) < 0) return 0;
369 if (PyModule_AddObject(this_module
, "UTF8String",
370 (PyObject
*)&DBusPyUTF8String_Type
) < 0) return 0;
371 if (PyModule_AddObject(this_module
, "String",
372 (PyObject
*)&DBusPyString_Type
) < 0) return 0;
377 /* vim:set ft=c cino< sw=4 sts=4 et: */