Actually commit the numerous copyright-statement changes.
[dbus-python-phuang.git] / _dbus_bindings / bytes.c
blobd15f00dc7e98922c10576374e16dbb24a261b8b0
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
23 #include <Python.h>
24 #include <structmember.h>
26 #include <stdint.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"
33 "\n"
34 "A Byte b may be converted to a str of length 1 via str(b) == chr(b).\n"
35 "\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"
39 "\n"
40 "Constructor::\n"
41 "\n"
42 " dbus.Byte(integer or str of length 1[, variant_level])\n"
43 "\n"
44 "``variant_level`` must be non-negative; the default is 0.\n"
45 "\n"
46 ":IVariables:\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"
54 static PyObject *
55 Byte_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
57 PyObject *obj;
58 PyObject *tuple;
59 long variantness = 0;
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");
65 return NULL;
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");
73 return NULL;
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) {
82 goto bad_arg;
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) {
91 Py_INCREF(obj);
92 return obj;
94 if (i < 0 || i > 255) goto bad_range;
95 /* else make it a new reference */
96 Py_INCREF(obj);
98 else {
99 goto bad_arg;
102 tuple = Py_BuildValue("(O)", obj);
103 if (!tuple) return NULL;
104 Py_DECREF(obj);
105 obj = NULL;
107 obj = DBusPyIntBase_Type.tp_new(cls, tuple, kwargs);
108 Py_DECREF(tuple);
109 tuple = NULL;
110 return obj;
112 bad_arg:
113 PyErr_SetString(PyExc_TypeError, "Expected a string of length 1, "
114 "or an int in the range 0-255");
115 return NULL;
116 bad_range:
117 PyErr_SetString(PyExc_ValueError, "Integer outside range 0-255");
118 return NULL;
121 static PyObject *
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))
131 "dbus.Byte",
134 0, /* tp_dealloc */
135 0, /* tp_print */
136 0, /* tp_getattr */
137 0, /* tp_setattr */
138 0, /* tp_compare */
139 0, /* tp_repr */
140 0, /* tp_as_number */
141 0, /* tp_as_sequence */
142 0, /* tp_as_mapping */
143 0, /* tp_hash */
144 0, /* tp_call */
145 Byte_tp_str, /* tp_str */
146 0, /* tp_getattro */
147 0, /* tp_setattro */
148 0, /* tp_as_buffer */
149 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
150 Byte_tp_doc, /* tp_doc */
151 0, /* tp_traverse */
152 0, /* tp_clear */
153 0, /* tp_richcompare */
154 0, /* tp_weaklistoffset */
155 0, /* tp_iter */
156 0, /* tp_iternext */
157 0, /* tp_methods */
158 0, /* tp_members */
159 0, /* tp_getset */
160 DEFERRED_ADDRESS(&PyInt_Type), /* tp_base */
161 0, /* tp_dict */
162 0, /* tp_descr_get */
163 0, /* tp_descr_set */
164 0, /* tp_dictoffset */
165 0, /* tp_init */
166 0, /* tp_alloc */
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"
173 "\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"
179 "\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"
185 "\n"
186 "Import via::\n"
187 "\n"
188 " from dbus import ByteArray\n"
189 "\n"
190 "Constructor::\n"
191 "\n"
192 " ByteArray(str)\n"
195 PyTypeObject DBusPyByteArray_Type = {
196 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
198 "dbus.ByteArray",
201 0, /* tp_dealloc */
202 0, /* tp_print */
203 0, /* tp_getattr */
204 0, /* tp_setattr */
205 0, /* tp_compare */
206 0, /* tp_repr */
207 0, /* tp_as_number */
208 0, /* tp_as_sequence */
209 0, /* tp_as_mapping */
210 0, /* tp_hash */
211 0, /* tp_call */
212 0, /* tp_str */
213 0, /* tp_getattro */
214 0, /* tp_setattro */
215 0, /* tp_as_buffer */
216 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
217 ByteArray_tp_doc, /* tp_doc */
218 0, /* tp_traverse */
219 0, /* tp_clear */
220 0, /* tp_richcompare */
221 0, /* tp_weaklistoffset */
222 0, /* tp_iter */
223 0, /* tp_iternext */
224 0, /* tp_methods */
225 0, /* tp_members */
226 0, /* tp_getset */
227 DEFERRED_ADDRESS(&DBusPyStrBase_Type), /* tp_base */
228 0, /* tp_dict */
229 0, /* tp_descr_get */
230 0, /* tp_descr_set */
231 0, /* tp_dictoffset */
232 0, /* tp_init */
233 0, /* tp_alloc */
234 0, /* tp_new */
237 dbus_bool_t
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;
248 return 1;
251 dbus_bool_t
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;
261 return 1;
264 /* vim:set ft=c cino< sw=4 sts=4 et: */