Actually commit the numerous copyright-statement changes.
[dbus-python-phuang.git] / _dbus_bindings / string.c
blob1a9dfa7199bd5ba40756dc11060d269bf0e957bc
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"
30 "\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"
33 "\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"
40 "\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"
46 "\n"
47 "\n"
48 "Constructor::\n"
49 "\n"
50 " dbus.UTF8String(value: str or unicode[, variant_level: int]) -> UTF8String\n"
51 "\n"
52 "If value is a str object it must be valid UTF-8.\n"
53 "\n"
54 "variant_level must be non-negative; the default is 0.\n"
55 "\n"
56 ":IVariables:\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"
65 static PyObject *
66 UTF8String_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
68 const char *str = NULL;
69 long variantness = 0;
70 static char *argnames[] = {"value", "variant_level", NULL};
71 PyObject *unicode;
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;
77 Py_DECREF(unicode);
78 return (DBusPyStrBase_Type.tp_new)(cls, args, kwargs);
81 PyTypeObject DBusPyUTF8String_Type = {
82 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
84 "dbus.UTF8String",
87 0, /* tp_dealloc */
88 0, /* tp_print */
89 0, /* tp_getattr */
90 0, /* tp_setattr */
91 0, /* tp_compare */
92 0, /* tp_repr */
93 0, /* tp_as_number */
94 0, /* tp_as_sequence */
95 0, /* tp_as_mapping */
96 0, /* tp_hash */
97 0, /* tp_call */
98 0, /* tp_str */
99 0, /* tp_getattro */
100 0, /* tp_setattro */
101 0, /* tp_as_buffer */
102 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
103 UTF8String_tp_doc, /* tp_doc */
104 0, /* tp_traverse */
105 0, /* tp_clear */
106 0, /* tp_richcompare */
107 0, /* tp_weaklistoffset */
108 0, /* tp_iter */
109 0, /* tp_iternext */
110 0, /* tp_methods */
111 0, /* tp_members */
112 0, /* tp_getset */
113 DEFERRED_ADDRESS(&DBusPyStrBase_Type), /* tp_base */
114 0, /* tp_dict */
115 0, /* tp_descr_get */
116 0, /* tp_descr_set */
117 0, /* tp_dictoffset */
118 0, /* tp_init */
119 0, /* tp_alloc */
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"
127 "\n"
128 "ObjectPath is a subtype of str, and object-paths behave like strings.\n"
129 "\n"
130 "Constructor::\n"
131 "\n"
132 " dbus.ObjectPath(path: str, variant_level: int) -> ObjectPath\n"
133 "\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"
136 "\n"
137 ":IVariables:\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"
145 static PyObject *
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)) {
155 return NULL;
157 return (DBusPyStrBase_Type.tp_new)(cls, args, kwargs);
160 PyTypeObject DBusPyObjectPath_Type = {
161 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
163 "dbus.ObjectPath",
166 0, /* tp_dealloc */
167 0, /* tp_print */
168 0, /* tp_getattr */
169 0, /* tp_setattr */
170 0, /* tp_compare */
171 0, /* tp_repr */
172 0, /* tp_as_number */
173 0, /* tp_as_sequence */
174 0, /* tp_as_mapping */
175 0, /* tp_hash */
176 0, /* tp_call */
177 0, /* tp_str */
178 0, /* tp_getattro */
179 0, /* tp_setattro */
180 0, /* tp_as_buffer */
181 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
182 ObjectPath_tp_doc, /* tp_doc */
183 0, /* tp_traverse */
184 0, /* tp_clear */
185 0, /* tp_richcompare */
186 0, /* tp_weaklistoffset */
187 0, /* tp_iter */
188 0, /* tp_iternext */
189 0, /* tp_methods */
190 0, /* tp_members */
191 0, /* tp_getset */
192 DEFERRED_ADDRESS(&DBusPyStrBase_Type), /* tp_base */
193 0, /* tp_dict */
194 0, /* tp_descr_get */
195 0, /* tp_descr_set */
196 0, /* tp_dictoffset */
197 0, /* tp_init */
198 0, /* tp_alloc */
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"
206 "\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"
209 "\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"
215 "\n"
216 "Constructor::\n"
217 "\n"
218 " String(value: str or unicode[, variant_level: int]) -> String\n"
219 "\n"
220 "variant_level must be non-negative; the default is 0.\n"
221 "\n"
222 ":IVariables:\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),
232 READONLY,
233 "The number of nested variants wrapping the real data. "
234 "0 if not in a variant"},
235 {NULL},
238 static PyObject *
239 String_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
241 PyObject *self;
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");
248 return NULL;
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");
256 return NULL;
258 self = (PyUnicode_Type.tp_new)(cls, args, NULL);
259 if (self) {
260 ((DBusPyString *)self)->variant_level = variantness;
262 return self;
265 static PyObject *
266 String_tp_repr(PyObject *self)
268 PyObject *parent_repr = (PyUnicode_Type.tp_repr)(self);
269 PyObject *my_repr;
271 if (!parent_repr) {
272 return NULL;
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);
280 else {
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);
286 return my_repr;
289 PyTypeObject DBusPyString_Type = {
290 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
292 "dbus.String",
293 sizeof(DBusPyString),
295 0, /* tp_dealloc */
296 0, /* tp_print */
297 0, /* tp_getattr */
298 0, /* tp_setattr */
299 0, /* tp_compare */
300 String_tp_repr, /* tp_repr */
301 0, /* tp_as_number */
302 0, /* tp_as_sequence */
303 0, /* tp_as_mapping */
304 0, /* tp_hash */
305 0, /* tp_call */
306 0, /* tp_str */
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 */
312 0, /* tp_traverse */
313 0, /* tp_clear */
314 0, /* tp_richcompare */
315 0, /* tp_weaklistoffset */
316 0, /* tp_iter */
317 0, /* tp_iternext */
318 0, /* tp_methods */
319 String_tp_members, /* tp_members */
320 0, /* tp_getset */
321 DEFERRED_ADDRESS(&PyUnicode_Type), /* tp_base */
322 0, /* tp_dict */
323 0, /* tp_descr_get */
324 0, /* tp_descr_set */
325 0, /* tp_dictoffset */
326 0, /* tp_init */
327 0, /* tp_alloc */
328 String_tp_new, /* tp_new */
331 dbus_bool_t
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)");
340 return 0;
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;
358 return 1;
361 dbus_bool_t
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;
374 return 1;
377 /* vim:set ft=c cino< sw=4 sts=4 et: */