Revert "Make sure extensions are built before docs; get rid of strange inter-director...
[dbus-python-phuang.git] / _dbus_bindings / bytes.c
bloba5648fe5a64da59155522603baec2fa16e4b23ba
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.
26 #include <Python.h>
27 #include <structmember.h>
29 #include <stdint.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"
36 "\n"
37 "A Byte b may be converted to a str of length 1 via str(b) == chr(b).\n"
38 "\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"
42 "\n"
43 "Constructor::\n"
44 "\n"
45 " dbus.Byte(integer or str of length 1[, variant_level])\n"
46 "\n"
47 "``variant_level`` must be non-negative; the default is 0.\n"
48 "\n"
49 ":IVariables:\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"
57 static PyObject *
58 Byte_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
60 PyObject *obj;
61 PyObject *tuple;
62 long variantness = 0;
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");
68 return NULL;
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");
76 return NULL;
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) {
85 goto bad_arg;
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) {
94 Py_INCREF(obj);
95 return obj;
97 if (i < 0 || i > 255) goto bad_range;
98 /* else make it a new reference */
99 Py_INCREF(obj);
101 else {
102 goto bad_arg;
105 tuple = Py_BuildValue("(O)", obj);
106 if (!tuple) return NULL;
107 Py_DECREF(obj);
108 obj = NULL;
110 obj = DBusPyIntBase_Type.tp_new(cls, tuple, kwargs);
111 Py_DECREF(tuple);
112 tuple = NULL;
113 return obj;
115 bad_arg:
116 PyErr_SetString(PyExc_TypeError, "Expected a string of length 1, "
117 "or an int in the range 0-255");
118 return NULL;
119 bad_range:
120 PyErr_SetString(PyExc_ValueError, "Integer outside range 0-255");
121 return NULL;
124 static PyObject *
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))
134 "dbus.Byte",
137 0, /* tp_dealloc */
138 0, /* tp_print */
139 0, /* tp_getattr */
140 0, /* tp_setattr */
141 0, /* tp_compare */
142 0, /* tp_repr */
143 0, /* tp_as_number */
144 0, /* tp_as_sequence */
145 0, /* tp_as_mapping */
146 0, /* tp_hash */
147 0, /* tp_call */
148 Byte_tp_str, /* tp_str */
149 0, /* tp_getattro */
150 0, /* tp_setattro */
151 0, /* tp_as_buffer */
152 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
153 Byte_tp_doc, /* tp_doc */
154 0, /* tp_traverse */
155 0, /* tp_clear */
156 0, /* tp_richcompare */
157 0, /* tp_weaklistoffset */
158 0, /* tp_iter */
159 0, /* tp_iternext */
160 0, /* tp_methods */
161 0, /* tp_members */
162 0, /* tp_getset */
163 DEFERRED_ADDRESS(&PyInt_Type), /* tp_base */
164 0, /* tp_dict */
165 0, /* tp_descr_get */
166 0, /* tp_descr_set */
167 0, /* tp_dictoffset */
168 0, /* tp_init */
169 0, /* tp_alloc */
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"
176 "\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"
182 "\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"
188 "\n"
189 "Import via::\n"
190 "\n"
191 " from dbus import ByteArray\n"
192 "\n"
193 "Constructor::\n"
194 "\n"
195 " ByteArray(str)\n"
198 PyTypeObject DBusPyByteArray_Type = {
199 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
201 "dbus.ByteArray",
204 0, /* tp_dealloc */
205 0, /* tp_print */
206 0, /* tp_getattr */
207 0, /* tp_setattr */
208 0, /* tp_compare */
209 0, /* tp_repr */
210 0, /* tp_as_number */
211 0, /* tp_as_sequence */
212 0, /* tp_as_mapping */
213 0, /* tp_hash */
214 0, /* tp_call */
215 0, /* tp_str */
216 0, /* tp_getattro */
217 0, /* tp_setattro */
218 0, /* tp_as_buffer */
219 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
220 ByteArray_tp_doc, /* tp_doc */
221 0, /* tp_traverse */
222 0, /* tp_clear */
223 0, /* tp_richcompare */
224 0, /* tp_weaklistoffset */
225 0, /* tp_iter */
226 0, /* tp_iternext */
227 0, /* tp_methods */
228 0, /* tp_members */
229 0, /* tp_getset */
230 DEFERRED_ADDRESS(&DBusPyStrBase_Type), /* tp_base */
231 0, /* tp_dict */
232 0, /* tp_descr_get */
233 0, /* tp_descr_set */
234 0, /* tp_dictoffset */
235 0, /* tp_init */
236 0, /* tp_alloc */
237 0, /* tp_new */
240 dbus_bool_t
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;
251 return 1;
254 dbus_bool_t
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;
264 return 1;
267 /* vim:set ft=c cino< sw=4 sts=4 et: */