Update NEWS, README for 0.80.0
[dbus-python-phuang.git] / _dbus_bindings / bytes.c
blobd771d8fe34a0a9284d6cf986860f938a94607172
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 program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <Python.h>
26 #include <structmember.h>
28 #include <stdint.h>
30 #include "dbus_bindings-internal.h"
31 #include "types-internal.h"
33 PyDoc_STRVAR(Byte_tp_doc,
34 "An unsigned byte: a subtype of int, with range restricted to [0, 255].\n"
35 "\n"
36 "A Byte b may be converted to a str of length 1 via str(b) == chr(b).\n"
37 "\n"
38 "Most of the time you don't want to use this class - it mainly exists\n"
39 "for symmetry with the other D-Bus types. See `dbus.ByteArray` for a\n"
40 "better way to handle arrays of Byte.\n"
41 "\n"
42 "Constructor::\n"
43 "\n"
44 " dbus.Byte(integer or str of length 1[, variant_level])\n"
45 "\n"
46 "``variant_level`` must be non-negative; the default is 0.\n"
47 "\n"
48 ":IVariables:\n"
49 " `variant_level` : int\n"
50 " Indicates how many nested Variant containers this object\n"
51 " is contained in: if a message's wire format has a variant containing a\n"
52 " variant containing a byte, this is represented in Python by a\n"
53 " Byte with variant_level==2.\n"
56 static PyObject *
57 Byte_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
59 PyObject *obj;
60 PyObject *tuple;
61 long variantness = 0;
62 static char *argnames[] = {"variant_level", NULL};
64 if (PyTuple_Size(args) > 1) {
65 PyErr_SetString(PyExc_TypeError, "Byte constructor takes no more "
66 "than one positional argument");
67 return NULL;
69 if (!PyArg_ParseTupleAndKeywords(dbus_py_empty_tuple, kwargs,
70 "|l:__new__", argnames,
71 &variantness)) return NULL;
72 if (variantness < 0) {
73 PyErr_SetString(PyExc_ValueError,
74 "variant_level must be non-negative");
75 return NULL;
78 /* obj is only a borrowed ref for the moment */
79 obj = PyTuple_GetItem(args, 0);
81 if (PyString_Check(obj)) {
82 /* string of length 1, we hope */
83 if (PyString_GET_SIZE(obj) != 1) {
84 goto bad_arg;
86 obj = PyInt_FromLong((unsigned char)(PyString_AS_STRING(obj)[0]));
88 else if (PyInt_Check(obj)) {
89 long i = PyInt_AS_LONG(obj);
91 if (obj->ob_type == cls &&
92 ((DBusPyIntBase *)obj)->variant_level == variantness) {
93 Py_INCREF(obj);
94 return obj;
96 if (i < 0 || i > 255) goto bad_range;
97 /* else make it a new reference */
98 Py_INCREF(obj);
100 else {
101 goto bad_arg;
104 tuple = Py_BuildValue("(O)", obj);
105 if (!tuple) return NULL;
106 Py_DECREF(obj);
107 obj = NULL;
109 obj = DBusPyIntBase_Type.tp_new(cls, tuple, kwargs);
110 Py_DECREF(tuple);
111 tuple = NULL;
112 return obj;
114 bad_arg:
115 PyErr_SetString(PyExc_TypeError, "Expected a string of length 1, "
116 "or an int in the range 0-255");
117 return NULL;
118 bad_range:
119 PyErr_SetString(PyExc_ValueError, "Integer outside range 0-255");
120 return NULL;
123 static PyObject *
124 Byte_tp_str(PyObject *self)
126 unsigned char str[2] = { (unsigned char)PyInt_AS_LONG(self), 0 };
127 return PyString_FromStringAndSize((char *)str, 1);
130 PyTypeObject DBusPyByte_Type = {
131 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
133 "dbus.Byte",
136 0, /* tp_dealloc */
137 0, /* tp_print */
138 0, /* tp_getattr */
139 0, /* tp_setattr */
140 0, /* tp_compare */
141 0, /* tp_repr */
142 0, /* tp_as_number */
143 0, /* tp_as_sequence */
144 0, /* tp_as_mapping */
145 0, /* tp_hash */
146 0, /* tp_call */
147 Byte_tp_str, /* tp_str */
148 0, /* tp_getattro */
149 0, /* tp_setattro */
150 0, /* tp_as_buffer */
151 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
152 Byte_tp_doc, /* tp_doc */
153 0, /* tp_traverse */
154 0, /* tp_clear */
155 0, /* tp_richcompare */
156 0, /* tp_weaklistoffset */
157 0, /* tp_iter */
158 0, /* tp_iternext */
159 0, /* tp_methods */
160 0, /* tp_members */
161 0, /* tp_getset */
162 DEFERRED_ADDRESS(&PyInt_Type), /* tp_base */
163 0, /* tp_dict */
164 0, /* tp_descr_get */
165 0, /* tp_descr_set */
166 0, /* tp_dictoffset */
167 0, /* tp_init */
168 0, /* tp_alloc */
169 Byte_new, /* tp_new */
172 PyDoc_STRVAR(ByteArray_tp_doc,
173 "ByteArray is a subtype of str which can be used when you want an\n"
174 "efficient immutable representation of a D-Bus byte array (signature 'ay').\n"
175 "\n"
176 "By default, when byte arrays are converted from D-Bus to Python, they\n"
177 "come out as a `dbus.Array` of `dbus.Byte`. This is just for symmetry with\n"
178 "the other D-Bus types - in practice, what you usually want is the byte\n"
179 "array represented as a string, using this class. To get this, pass the\n"
180 "``byte_arrays=True`` keyword argument to any of these methods:\n"
181 "\n"
182 "* any D-Bus method proxy, or ``connect_to_signal``, on the objects returned\n"
183 " by `Bus.get_object`\n"
184 "* any D-Bus method on a `dbus.Interface`\n"
185 "* `dbus.Interface.connect_to_signal`\n"
186 "* `Bus.add_signal_receiver`\n"
187 "\n"
188 "Import via::\n"
189 "\n"
190 " from dbus import ByteArray\n"
191 "\n"
192 "Constructor::\n"
193 "\n"
194 " ByteArray(str)\n"
197 PyTypeObject DBusPyByteArray_Type = {
198 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
200 "dbus.ByteArray",
203 0, /* tp_dealloc */
204 0, /* tp_print */
205 0, /* tp_getattr */
206 0, /* tp_setattr */
207 0, /* tp_compare */
208 0, /* tp_repr */
209 0, /* tp_as_number */
210 0, /* tp_as_sequence */
211 0, /* tp_as_mapping */
212 0, /* tp_hash */
213 0, /* tp_call */
214 0, /* tp_str */
215 0, /* tp_getattro */
216 0, /* tp_setattro */
217 0, /* tp_as_buffer */
218 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
219 ByteArray_tp_doc, /* tp_doc */
220 0, /* tp_traverse */
221 0, /* tp_clear */
222 0, /* tp_richcompare */
223 0, /* tp_weaklistoffset */
224 0, /* tp_iter */
225 0, /* tp_iternext */
226 0, /* tp_methods */
227 0, /* tp_members */
228 0, /* tp_getset */
229 DEFERRED_ADDRESS(&DBusPyStrBase_Type), /* tp_base */
230 0, /* tp_dict */
231 0, /* tp_descr_get */
232 0, /* tp_descr_set */
233 0, /* tp_dictoffset */
234 0, /* tp_init */
235 0, /* tp_alloc */
236 0, /* tp_new */
239 dbus_bool_t
240 dbus_py_init_byte_types(void)
242 DBusPyByte_Type.tp_base = &DBusPyIntBase_Type;
243 if (PyType_Ready(&DBusPyByte_Type) < 0) return 0;
244 DBusPyByte_Type.tp_print = NULL;
246 DBusPyByteArray_Type.tp_base = &DBusPyStrBase_Type;
247 if (PyType_Ready(&DBusPyByteArray_Type) < 0) return 0;
248 DBusPyByteArray_Type.tp_print = NULL;
250 return 1;
253 dbus_bool_t
254 dbus_py_insert_byte_types(PyObject *this_module)
256 Py_INCREF(&DBusPyByte_Type);
257 if (PyModule_AddObject(this_module, "Byte",
258 (PyObject *)&DBusPyByte_Type) < 0) return 0;
259 Py_INCREF(&DBusPyByteArray_Type);
260 if (PyModule_AddObject(this_module, "ByteArray",
261 (PyObject *)&DBusPyByteArray_Type) < 0) return 0;
263 return 1;
266 /* vim:set ft=c cino< sw=4 sts=4 et: */