1 /* D-Bus Byte and ByteArray types.
3 * Copyright (C) 2006 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
24 #include <structmember.h>
28 #include "dbus_bindings-internal.h"
29 #include "types-internal.h"
31 PyDoc_STRVAR(Byte_tp_doc
,
32 "An unsigned byte: a subtype of int, with range restricted to [0, 255].\n"
34 "A Byte b may be converted to a str of length 1 via str(b) == chr(b).\n"
36 "Most of the time you don't want to use this class - it mainly exists\n"
37 "for symmetry with the other D-Bus types. See `dbus.ByteArray` for a\n"
38 "better way to handle arrays of Byte.\n"
42 " dbus.Byte(integer or str of length 1[, variant_level])\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 byte, this is represented in Python by a\n"
51 " Byte with variant_level==2.\n"
55 Byte_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
60 static char *argnames
[] = {"variant_level", NULL
};
62 if (PyTuple_Size(args
) > 1) {
63 PyErr_SetString(PyExc_TypeError
, "Byte constructor takes no more "
64 "than one positional argument");
67 if (!PyArg_ParseTupleAndKeywords(dbus_py_empty_tuple
, kwargs
,
68 "|l:__new__", argnames
,
69 &variantness
)) return NULL
;
70 if (variantness
< 0) {
71 PyErr_SetString(PyExc_ValueError
,
72 "variant_level must be non-negative");
76 /* obj is only a borrowed ref for the moment */
77 obj
= PyTuple_GetItem(args
, 0);
79 if (PyString_Check(obj
)) {
80 /* string of length 1, we hope */
81 if (PyString_GET_SIZE(obj
) != 1) {
84 obj
= PyInt_FromLong((unsigned char)(PyString_AS_STRING(obj
)[0]));
86 else if (PyInt_Check(obj
)) {
87 long i
= PyInt_AS_LONG(obj
);
89 if (obj
->ob_type
== cls
&&
90 ((DBusPyIntBase
*)obj
)->variant_level
== variantness
) {
94 if (i
< 0 || i
> 255) goto bad_range
;
95 /* else make it a new reference */
102 tuple
= Py_BuildValue("(O)", obj
);
103 if (!tuple
) return NULL
;
107 obj
= DBusPyIntBase_Type
.tp_new(cls
, tuple
, kwargs
);
113 PyErr_SetString(PyExc_TypeError
, "Expected a string of length 1, "
114 "or an int in the range 0-255");
117 PyErr_SetString(PyExc_ValueError
, "Integer outside range 0-255");
122 Byte_tp_str(PyObject
*self
)
124 unsigned char str
[2] = { (unsigned char)PyInt_AS_LONG(self
), 0 };
125 return PyString_FromStringAndSize((char *)str
, 1);
128 PyTypeObject DBusPyByte_Type
= {
129 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
140 0, /* tp_as_number */
141 0, /* tp_as_sequence */
142 0, /* tp_as_mapping */
145 Byte_tp_str
, /* tp_str */
148 0, /* tp_as_buffer */
149 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
150 Byte_tp_doc
, /* tp_doc */
153 0, /* tp_richcompare */
154 0, /* tp_weaklistoffset */
160 DEFERRED_ADDRESS(&PyInt_Type
), /* tp_base */
162 0, /* tp_descr_get */
163 0, /* tp_descr_set */
164 0, /* tp_dictoffset */
167 Byte_new
, /* tp_new */
170 PyDoc_STRVAR(ByteArray_tp_doc
,
171 "ByteArray is a subtype of str which can be used when you want an\n"
172 "efficient immutable representation of a D-Bus byte array (signature 'ay').\n"
174 "By default, when byte arrays are converted from D-Bus to Python, they\n"
175 "come out as a `dbus.Array` of `dbus.Byte`. This is just for symmetry with\n"
176 "the other D-Bus types - in practice, what you usually want is the byte\n"
177 "array represented as a string, using this class. To get this, pass the\n"
178 "``byte_arrays=True`` keyword argument to any of these methods:\n"
180 "* any D-Bus method proxy, or ``connect_to_signal``, on the objects returned\n"
181 " by `Bus.get_object`\n"
182 "* any D-Bus method on a `dbus.Interface`\n"
183 "* `dbus.Interface.connect_to_signal`\n"
184 "* `Bus.add_signal_receiver`\n"
188 " from dbus import ByteArray\n"
195 PyTypeObject DBusPyByteArray_Type
= {
196 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
207 0, /* tp_as_number */
208 0, /* tp_as_sequence */
209 0, /* tp_as_mapping */
215 0, /* tp_as_buffer */
216 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
217 ByteArray_tp_doc
, /* tp_doc */
220 0, /* tp_richcompare */
221 0, /* tp_weaklistoffset */
227 DEFERRED_ADDRESS(&DBusPyStrBase_Type
), /* tp_base */
229 0, /* tp_descr_get */
230 0, /* tp_descr_set */
231 0, /* tp_dictoffset */
238 dbus_py_init_byte_types(void)
240 DBusPyByte_Type
.tp_base
= &DBusPyIntBase_Type
;
241 if (PyType_Ready(&DBusPyByte_Type
) < 0) return 0;
242 DBusPyByte_Type
.tp_print
= NULL
;
244 DBusPyByteArray_Type
.tp_base
= &DBusPyStrBase_Type
;
245 if (PyType_Ready(&DBusPyByteArray_Type
) < 0) return 0;
246 DBusPyByteArray_Type
.tp_print
= NULL
;
252 dbus_py_insert_byte_types(PyObject
*this_module
)
254 Py_INCREF(&DBusPyByte_Type
);
255 if (PyModule_AddObject(this_module
, "Byte",
256 (PyObject
*)&DBusPyByte_Type
) < 0) return 0;
257 Py_INCREF(&DBusPyByteArray_Type
);
258 if (PyModule_AddObject(this_module
, "ByteArray",
259 (PyObject
*)&DBusPyByteArray_Type
) < 0) return 0;
264 /* vim:set ft=c cino< sw=4 sts=4 et: */