1 /* D-Bus Byte and ByteArray types.
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(Byte_tp_doc
,
35 "An unsigned byte: a subtype of int, with range restricted to [0, 255].\n"
37 "A Byte b may be converted to a str of length 1 via str(b) == chr(b).\n"
39 "Most of the time you don't want to use this class - it mainly exists\n"
40 "for symmetry with the other D-Bus types. See `dbus.ByteArray` for a\n"
41 "better way to handle arrays of Byte.\n"
45 " dbus.Byte(integer or str of length 1[, variant_level])\n"
47 "``variant_level`` must be non-negative; the default is 0.\n"
50 " `variant_level` : int\n"
51 " Indicates how many nested Variant containers this object\n"
52 " is contained in: if a message's wire format has a variant containing a\n"
53 " variant containing a byte, this is represented in Python by a\n"
54 " Byte with variant_level==2.\n"
58 Byte_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
63 static char *argnames
[] = {"variant_level", NULL
};
65 if (PyTuple_Size(args
) > 1) {
66 PyErr_SetString(PyExc_TypeError
, "Byte constructor takes no more "
67 "than one positional argument");
70 if (!PyArg_ParseTupleAndKeywords(dbus_py_empty_tuple
, kwargs
,
71 "|l:__new__", argnames
,
72 &variantness
)) return NULL
;
73 if (variantness
< 0) {
74 PyErr_SetString(PyExc_ValueError
,
75 "variant_level must be non-negative");
79 /* obj is only a borrowed ref for the moment */
80 obj
= PyTuple_GetItem(args
, 0);
82 if (PyString_Check(obj
)) {
83 /* string of length 1, we hope */
84 if (PyString_GET_SIZE(obj
) != 1) {
87 obj
= PyInt_FromLong((unsigned char)(PyString_AS_STRING(obj
)[0]));
89 else if (PyInt_Check(obj
)) {
90 long i
= PyInt_AS_LONG(obj
);
92 if (obj
->ob_type
== cls
&&
93 ((DBusPyIntBase
*)obj
)->variant_level
== variantness
) {
97 if (i
< 0 || i
> 255) goto bad_range
;
98 /* else make it a new reference */
105 tuple
= Py_BuildValue("(O)", obj
);
106 if (!tuple
) return NULL
;
110 obj
= DBusPyIntBase_Type
.tp_new(cls
, tuple
, kwargs
);
116 PyErr_SetString(PyExc_TypeError
, "Expected a string of length 1, "
117 "or an int in the range 0-255");
120 PyErr_SetString(PyExc_ValueError
, "Integer outside range 0-255");
125 Byte_tp_str(PyObject
*self
)
127 unsigned char str
[2] = { (unsigned char)PyInt_AS_LONG(self
), 0 };
128 return PyString_FromStringAndSize((char *)str
, 1);
131 PyTypeObject DBusPyByte_Type
= {
132 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
143 0, /* tp_as_number */
144 0, /* tp_as_sequence */
145 0, /* tp_as_mapping */
148 Byte_tp_str
, /* tp_str */
151 0, /* tp_as_buffer */
152 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
153 Byte_tp_doc
, /* tp_doc */
156 0, /* tp_richcompare */
157 0, /* tp_weaklistoffset */
163 DEFERRED_ADDRESS(&PyInt_Type
), /* tp_base */
165 0, /* tp_descr_get */
166 0, /* tp_descr_set */
167 0, /* tp_dictoffset */
170 Byte_new
, /* tp_new */
173 PyDoc_STRVAR(ByteArray_tp_doc
,
174 "ByteArray is a subtype of str which can be used when you want an\n"
175 "efficient immutable representation of a D-Bus byte array (signature 'ay').\n"
177 "By default, when byte arrays are converted from D-Bus to Python, they\n"
178 "come out as a `dbus.Array` of `dbus.Byte`. This is just for symmetry with\n"
179 "the other D-Bus types - in practice, what you usually want is the byte\n"
180 "array represented as a string, using this class. To get this, pass the\n"
181 "``byte_arrays=True`` keyword argument to any of these methods:\n"
183 "* any D-Bus method proxy, or ``connect_to_signal``, on the objects returned\n"
184 " by `Bus.get_object`\n"
185 "* any D-Bus method on a `dbus.Interface`\n"
186 "* `dbus.Interface.connect_to_signal`\n"
187 "* `Bus.add_signal_receiver`\n"
191 " from dbus import ByteArray\n"
198 PyTypeObject DBusPyByteArray_Type
= {
199 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
210 0, /* tp_as_number */
211 0, /* tp_as_sequence */
212 0, /* tp_as_mapping */
218 0, /* tp_as_buffer */
219 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
220 ByteArray_tp_doc
, /* tp_doc */
223 0, /* tp_richcompare */
224 0, /* tp_weaklistoffset */
230 DEFERRED_ADDRESS(&DBusPyStrBase_Type
), /* tp_base */
232 0, /* tp_descr_get */
233 0, /* tp_descr_set */
234 0, /* tp_dictoffset */
241 dbus_py_init_byte_types(void)
243 DBusPyByte_Type
.tp_base
= &DBusPyIntBase_Type
;
244 if (PyType_Ready(&DBusPyByte_Type
) < 0) return 0;
245 DBusPyByte_Type
.tp_print
= NULL
;
247 DBusPyByteArray_Type
.tp_base
= &DBusPyStrBase_Type
;
248 if (PyType_Ready(&DBusPyByteArray_Type
) < 0) return 0;
249 DBusPyByteArray_Type
.tp_print
= NULL
;
255 dbus_py_insert_byte_types(PyObject
*this_module
)
257 Py_INCREF(&DBusPyByte_Type
);
258 if (PyModule_AddObject(this_module
, "Byte",
259 (PyObject
*)&DBusPyByte_Type
) < 0) return 0;
260 Py_INCREF(&DBusPyByteArray_Type
);
261 if (PyModule_AddObject(this_module
, "ByteArray",
262 (PyObject
*)&DBusPyByteArray_Type
) < 0) return 0;
267 /* vim:set ft=c cino< sw=4 sts=4 et: */