1 /* Simple D-Bus types: integers of various sizes, and ObjectPath.
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 "types-internal.h"
27 /* Specific types =================================================== */
29 /* Boolean, a subclass of DBusPythonInt ============================= */
31 PyDoc_STRVAR(Boolean_tp_doc
,
32 "A boolean, represented as a subtype of `int` (not `bool`, because `bool`\n"
33 "cannot be subclassed).\n"
37 " dbus.Boolean(value[, variant_level]) -> Boolean\n"
39 "``value`` is converted to 0 or 1 as if by ``int(bool(value))``.\n"
41 "``variant_level`` must be non-negative; the default is 0.\n"
44 " `variant_level` : int\n"
45 " Indicates how many nested Variant containers this object\n"
46 " is contained in: if a message's wire format has a variant containing a\n"
47 " variant containing a boolean, this is represented in Python by a\n"
48 " Boolean with variant_level==2.\n"
52 Boolean_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
54 PyObject
*tuple
, *self
, *value
= Py_None
;
56 static char *argnames
[] = {"_", "variant_level", NULL
};
58 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|Ol:__new__", argnames
,
59 &value
, &variantness
)) return NULL
;
60 if (variantness
< 0) {
61 PyErr_SetString(PyExc_ValueError
,
62 "variant_level must be non-negative");
65 tuple
= Py_BuildValue("(i)", PyObject_IsTrue(value
) ? 1 : 0);
66 if (!tuple
) return NULL
;
67 self
= (DBusPyIntBase_Type
.tp_new
)(cls
, tuple
, kwargs
);
73 Boolean_tp_repr (PyObject
*self
)
75 long variant_level
= ((DBusPyIntBase
*)self
)->variant_level
;
76 if (variant_level
> 0) {
77 return PyString_FromFormat("%s(%s, variant_level=%ld)",
78 self
->ob_type
->tp_name
,
79 PyInt_AsLong(self
) ? "True" : "False",
82 return PyString_FromFormat("%s(%s)",
83 self
->ob_type
->tp_name
,
84 PyInt_AsLong(self
) ? "True" : "False");
87 PyTypeObject DBusPyBoolean_Type
= {
88 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
98 Boolean_tp_repr
, /* tp_repr */
100 0, /* tp_as_sequence */
101 0, /* tp_as_mapping */
107 0, /* tp_as_buffer */
108 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
109 Boolean_tp_doc
, /* tp_doc */
112 0, /* tp_richcompare */
113 0, /* tp_weaklistoffset */
119 DEFERRED_ADDRESS(&DBusPyIntBase_Type
), /* tp_base */
121 0, /* tp_descr_get */
122 0, /* tp_descr_set */
123 0, /* tp_dictoffset */
126 Boolean_tp_new
, /* tp_new */
129 /* Int16 ============================================================ */
131 PyDoc_STRVAR(Int16_tp_doc
,
132 "A signed 16-bit integer between -0x8000 and +0x7FFF, represented as\n"
133 "a subtype of `int`.\n"
137 " dbus.Int16(value: int[, variant_level: int]) -> Int16\n"
139 "value must be within the allowed range, or OverflowError will be\n"
142 " variant_level must be non-negative; the default is 0.\n"
145 " `variant_level` : int\n"
146 " Indicates how many nested Variant containers this object\n"
147 " is contained in: if a message's wire format has a variant containing a\n"
148 " variant containing an int16, this is represented in Python by an\n"
149 " Int16 with variant_level==2.\n"
153 dbus_py_int16_range_check(PyObject
*obj
)
155 long i
= PyInt_AsLong (obj
);
156 if (i
== -1 && PyErr_Occurred ()) return -1;
157 if (i
< -0x8000 || i
> 0x7fff) {
158 PyErr_Format(PyExc_OverflowError
, "Value %d out of range for Int16",
166 Int16_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
168 PyObject
*self
= (DBusPyIntBase_Type
.tp_new
)(cls
, args
, kwargs
);
169 if (self
&& dbus_py_int16_range_check(self
) == -1 && PyErr_Occurred()) {
176 PyTypeObject DBusPyInt16_Type
= {
177 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
188 0, /* tp_as_number */
189 0, /* tp_as_sequence */
190 0, /* tp_as_mapping */
196 0, /* tp_as_buffer */
197 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
198 Int16_tp_doc
, /* tp_doc */
201 0, /* tp_richcompare */
202 0, /* tp_weaklistoffset */
208 DEFERRED_ADDRESS(&DBusPyIntBase_Type
), /* tp_base */
210 0, /* tp_descr_get */
211 0, /* tp_descr_set */
212 0, /* tp_dictoffset */
215 Int16_tp_new
, /* tp_new */
218 /* UInt16 =========================================================== */
220 PyDoc_STRVAR(UInt16_tp_doc
,
221 "An unsigned 16-bit integer between 0 and 0xFFFF, represented as\n"
222 "a subtype of `int`.\n"
226 " dbus.UInt16(value: int[, variant_level: int]) -> UInt16\n"
228 "``value`` must be within the allowed range, or `OverflowError` will be\n"
231 "``variant_level`` must be non-negative; the default is 0.\n"
234 " `variant_level` : int\n"
235 " Indicates how many nested Variant containers this object\n"
236 " is contained in: if a message's wire format has a variant containing a\n"
237 " variant containing a uint16, this is represented in Python by a\n"
238 " UInt16 with variant_level==2.\n"
242 dbus_py_uint16_range_check(PyObject
*obj
)
244 long i
= PyInt_AsLong(obj
);
245 if (i
== -1 && PyErr_Occurred()) return (dbus_uint16_t
)(-1);
246 if (i
< 0 || i
> 0xffff) {
247 PyErr_Format(PyExc_OverflowError
, "Value %d out of range for UInt16",
249 return (dbus_uint16_t
)(-1);
255 UInt16_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
257 PyObject
*self
= (DBusPyIntBase_Type
.tp_new
)(cls
, args
, kwargs
);
258 if (self
&& dbus_py_uint16_range_check(self
) == (dbus_uint16_t
)(-1)
259 && PyErr_Occurred()) {
266 PyTypeObject DBusPyUInt16_Type
= {
267 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
278 0, /* tp_as_number */
279 0, /* tp_as_sequence */
280 0, /* tp_as_mapping */
286 0, /* tp_as_buffer */
287 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
288 UInt16_tp_doc
, /* tp_doc */
291 0, /* tp_richcompare */
292 0, /* tp_weaklistoffset */
298 DEFERRED_ADDRESS(&DBusPyIntBase_Type
), /* tp_base */
300 0, /* tp_descr_get */
301 0, /* tp_descr_set */
302 0, /* tp_dictoffset */
305 UInt16_tp_new
, /* tp_new */
308 /* Int32 ============================================================ */
310 PyDoc_STRVAR(Int32_tp_doc
,
311 "A signed 32-bit integer between -0x8000 0000 and +0x7FFF FFFF, represented as\n"
312 "a subtype of `int`.\n"
316 " dbus.Int32(value: int[, variant_level: int]) -> Int32\n"
318 "``value`` must be within the allowed range, or `OverflowError` will be\n"
321 "``variant_level`` must be non-negative; the default is 0.\n"
324 " `variant_level` : int\n"
325 " Indicates how many nested Variant containers this object\n"
326 " is contained in: if a message's wire format has a variant containing a\n"
327 " variant containing an int32, this is represented in Python by an\n"
328 " Int32 with variant_level==2.\n"
332 dbus_py_int32_range_check(PyObject
*obj
)
334 long i
= PyInt_AsLong(obj
);
335 if (i
== -1 && PyErr_Occurred()) return -1;
336 if (i
< INT32_MIN
|| i
> INT32_MAX
) {
337 PyErr_Format(PyExc_OverflowError
, "Value %d out of range for Int32",
345 Int32_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
347 PyObject
*self
= (DBusPyIntBase_Type
.tp_new
)(cls
, args
, kwargs
);
348 if (self
&& dbus_py_int32_range_check(self
) == -1 && PyErr_Occurred()) {
355 PyTypeObject DBusPyInt32_Type
= {
356 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
367 0, /* tp_as_number */
368 0, /* tp_as_sequence */
369 0, /* tp_as_mapping */
375 0, /* tp_as_buffer */
376 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
377 Int32_tp_doc
, /* tp_doc */
380 0, /* tp_richcompare */
381 0, /* tp_weaklistoffset */
387 DEFERRED_ADDRESS(&DBusPyIntBase_Type
), /* tp_base */
389 0, /* tp_descr_get */
390 0, /* tp_descr_set */
391 0, /* tp_dictoffset */
394 Int32_tp_new
, /* tp_new */
397 /* UInt32 =========================================================== */
399 PyDoc_STRVAR(UInt32_tp_doc
,
400 "An unsigned 32-bit integer between 0 and 0xFFFF FFFF, represented as a\n"
401 "subtype of `long`.\n"
403 "Note that this may be changed in future to be a subtype of `int` on\n"
404 "64-bit platforms; applications should not rely on either behaviour.\n"
408 " dbus.UInt32(value: long[, variant_level: int]) -> UInt32\n"
410 "``value`` must be within the allowed range, or `OverflowError` will be\n"
413 "``variant_level`` must be non-negative; the default is 0.\n"
416 " `variant_level` : int\n"
417 " Indicates how many nested Variant containers this object\n"
418 " is contained in: if a message's wire format has a variant containing a\n"
419 " variant containing a uint32, this is represented in Python by a\n"
420 " UInt32 with variant_level==2.\n"
424 dbus_py_uint32_range_check(PyObject
*obj
)
427 PyObject
*long_obj
= PyNumber_Long(obj
);
429 if (!long_obj
) return (dbus_uint32_t
)(-1);
430 i
= PyLong_AsUnsignedLong(long_obj
);
431 if (i
== (unsigned long)(-1) && PyErr_Occurred()) {
433 return (dbus_uint32_t
)(-1);
435 if (i
> UINT32_MAX
) {
436 PyErr_Format(PyExc_OverflowError
, "Value %d out of range for UInt32",
439 return (dbus_uint32_t
)(-1);
446 UInt32_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
448 PyObject
*self
= (DBusPyLongBase_Type
.tp_new
)(cls
, args
, kwargs
);
449 if (self
&& dbus_py_uint32_range_check(self
) == (dbus_uint32_t
)(-1)
450 && PyErr_Occurred()) {
457 PyTypeObject DBusPyUInt32_Type
= {
458 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
469 0, /* tp_as_number */
470 0, /* tp_as_sequence */
471 0, /* tp_as_mapping */
477 0, /* tp_as_buffer */
478 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
479 UInt32_tp_doc
, /* tp_doc */
482 0, /* tp_richcompare */
483 0, /* tp_weaklistoffset */
489 DEFERRED_ADDRESS(&DBusPyLongBase_Type
), /* tp_base */
491 0, /* tp_descr_get */
492 0, /* tp_descr_set */
493 0, /* tp_dictoffset */
496 UInt32_tp_new
, /* tp_new */
499 /* Int64 =========================================================== */
501 PyDoc_STRVAR(Int64_tp_doc
,
502 "A signed 64-bit integer between -0x8000 0000 0000 0000 and\n"
503 "+0x7FFF FFFF FFFF FFFF, represented as a subtype of `long`.\n"
505 "Note that this may be changed in future to be a subtype of `int` on\n"
506 "64-bit platforms; applications should not rely on either behaviour.\n"
508 "This type only works on platforms where the C compiler has suitable\n"
509 "64-bit types, such as C99 ``long long``.\n"
510 #ifdef DBUS_PYTHON_64_BIT_WORKS
511 "This is the case on your current platform.\n"
512 #else /* !defined(DBUS_PYTHON_64_BIT_WORKS) */
513 "This is not the case on your current platform, so this type's\n"
514 "constructor will always raise NotImplementedError. Try a better\n"
515 "compiler, if one is available.\n"
516 #endif /* !defined(DBUS_PYTHON_64_BIT_WORKS) */
520 " dbus.Int64(value: long[, variant_level: int]) -> Int64\n"
522 "``value`` must be within the allowed range, or `OverflowError` will be\n"
525 "``variant_level`` must be non-negative; the default is 0.\n"
528 " `variant_level` : int\n"
529 " Indicates how many nested Variant containers this object\n"
530 " is contained in: if a message's wire format has a variant containing a\n"
531 " variant containing an int64, this is represented in Python by an\n"
532 " Int64 with variant_level==2.\n"
535 #ifdef DBUS_PYTHON_64_BIT_WORKS
537 dbus_py_int64_range_check(PyObject
*obj
)
540 PyObject
*long_obj
= PyNumber_Long(obj
);
542 if (!long_obj
) return -1;
543 i
= PyLong_AsLongLong(long_obj
);
544 if (i
== -1 && PyErr_Occurred()) {
548 if (i
< INT64_MIN
|| i
> INT64_MAX
) {
549 PyErr_SetString(PyExc_OverflowError
, "Value out of range for Int64");
559 Int64_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
561 #ifdef DBUS_PYTHON_64_BIT_WORKS
562 PyObject
*self
= (DBusPyLongBase_Type
.tp_new
)(cls
, args
, kwargs
);
563 if (self
&& dbus_py_int64_range_check(self
) == -1 && PyErr_Occurred()) {
569 PyErr_SetString(PyExc_NotImplementedError
,
570 "64-bit types are not available on this platform");
575 PyTypeObject DBusPyInt64_Type
= {
576 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
587 0, /* tp_as_number */
588 0, /* tp_as_sequence */
589 0, /* tp_as_mapping */
595 0, /* tp_as_buffer */
596 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
597 Int64_tp_doc
, /* tp_doc */
600 0, /* tp_richcompare */
601 0, /* tp_weaklistoffset */
607 DEFERRED_ADDRESS(&DBusPyLongBase_Type
), /* tp_base */
609 0, /* tp_descr_get */
610 0, /* tp_descr_set */
611 0, /* tp_dictoffset */
614 Int64_tp_new
, /* tp_new */
617 /* UInt64 =========================================================== */
619 PyDoc_STRVAR(UInt64_tp_doc
,
620 "An unsigned 64-bit integer between 0 and 0xFFFF FFFF FFFF FFFF,\n"
621 "represented as a subtype of `long`.\n"
623 "This type only exists on platforms where the C compiler has suitable\n"
624 "64-bit types, such as C99 ``unsigned long long``.\n"
625 #ifdef DBUS_PYTHON_64_BIT_WORKS
626 "This is the case on your current platform.\n"
627 #else /* !defined(DBUS_PYTHON_64_BIT_WORKS) */
628 "This is not the case on your current platform, so this type's\n"
629 "constructor will always raise NotImplementedError. Try a better\n"
630 "compiler, if one is available.\n"
631 #endif /* !defined(DBUS_PYTHON_64_BIT_WORKS) */
635 " dbus.UInt64(value: long[, variant_level: int]) -> UInt64\n"
637 "``value`` must be within the allowed range, or `OverflowError` will be\n"
640 "``variant_level`` must be non-negative; the default is 0.\n"
643 " `variant_level` : int\n"
644 " Indicates how many nested Variant containers this object\n"
645 " is contained in: if a message's wire format has a variant containing a\n"
646 " variant containing a uint64, this is represented in Python by a\n"
647 " UInt64 with variant_level==2.\n"
651 dbus_py_uint64_range_check(PyObject
*obj
)
653 unsigned PY_LONG_LONG i
;
654 PyObject
*long_obj
= PyNumber_Long(obj
);
656 if (!long_obj
) return (dbus_uint64_t
)(-1);
657 i
= PyLong_AsUnsignedLongLong(long_obj
);
658 if (i
== (unsigned PY_LONG_LONG
)(-1) && PyErr_Occurred()) {
660 return (dbus_uint64_t
)(-1);
662 if (i
> UINT64_MAX
) {
663 PyErr_SetString(PyExc_OverflowError
, "Value out of range for UInt64");
665 return (dbus_uint64_t
)(-1);
672 UInt64_tp_new (PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
674 #ifdef DBUS_PYTHON_64_BIT_WORKS
675 PyObject
*self
= (DBusPyLongBase_Type
.tp_new
)(cls
, args
, kwargs
);
676 if (self
&& dbus_py_uint64_range_check(self
) == (dbus_uint64_t
)(-1)
677 && PyErr_Occurred()) {
683 PyErr_SetString(PyExc_NotImplementedError
,
684 "64-bit integer types are not supported on this platform");
689 PyTypeObject DBusPyUInt64_Type
= {
690 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
701 0, /* tp_as_number */
702 0, /* tp_as_sequence */
703 0, /* tp_as_mapping */
709 0, /* tp_as_buffer */
710 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
711 UInt64_tp_doc
, /* tp_doc */
714 0, /* tp_richcompare */
715 0, /* tp_weaklistoffset */
721 DEFERRED_ADDRESS(&DBusPyLongBase_Type
), /* tp_base */
723 0, /* tp_descr_get */
724 0, /* tp_descr_set */
725 0, /* tp_dictoffset */
728 UInt64_tp_new
, /* tp_new */
732 dbus_py_init_int_types(void)
734 DBusPyInt16_Type
.tp_base
= &DBusPyIntBase_Type
;
735 if (PyType_Ready(&DBusPyInt16_Type
) < 0) return 0;
736 /* disable the tp_print copied from PyInt_Type, so tp_repr gets called as
738 DBusPyInt16_Type
.tp_print
= NULL
;
740 DBusPyUInt16_Type
.tp_base
= &DBusPyIntBase_Type
;
741 if (PyType_Ready(&DBusPyUInt16_Type
) < 0) return 0;
742 DBusPyUInt16_Type
.tp_print
= NULL
;
744 DBusPyInt32_Type
.tp_base
= &DBusPyIntBase_Type
;
745 if (PyType_Ready(&DBusPyInt32_Type
) < 0) return 0;
746 DBusPyInt32_Type
.tp_print
= NULL
;
748 DBusPyUInt32_Type
.tp_base
= &DBusPyLongBase_Type
;
749 if (PyType_Ready(&DBusPyUInt32_Type
) < 0) return 0;
750 DBusPyUInt32_Type
.tp_print
= NULL
;
752 #if defined(DBUS_HAVE_INT64) && defined(HAVE_LONG_LONG)
753 DBusPyInt64_Type
.tp_base
= &DBusPyLongBase_Type
;
754 if (PyType_Ready(&DBusPyInt64_Type
) < 0) return 0;
755 DBusPyInt64_Type
.tp_print
= NULL
;
757 DBusPyUInt64_Type
.tp_base
= &DBusPyLongBase_Type
;
758 if (PyType_Ready(&DBusPyUInt64_Type
) < 0) return 0;
759 DBusPyUInt64_Type
.tp_print
= NULL
;
765 dbus_py_insert_int_types(PyObject
*this_module
)
767 Py_INCREF(&DBusPyInt16_Type
);
768 Py_INCREF(&DBusPyUInt16_Type
);
769 Py_INCREF(&DBusPyInt32_Type
);
770 Py_INCREF(&DBusPyUInt32_Type
);
771 Py_INCREF(&DBusPyInt64_Type
);
772 Py_INCREF(&DBusPyUInt64_Type
);
773 Py_INCREF(&DBusPyBoolean_Type
);
774 if (PyModule_AddObject(this_module
, "Int16",
775 (PyObject
*)&DBusPyInt16_Type
) < 0) return 0;
776 if (PyModule_AddObject(this_module
, "UInt16",
777 (PyObject
*)&DBusPyUInt16_Type
) < 0) return 0;
778 if (PyModule_AddObject(this_module
, "Int32",
779 (PyObject
*)&DBusPyInt32_Type
) < 0) return 0;
780 if (PyModule_AddObject(this_module
, "UInt32",
781 (PyObject
*)&DBusPyUInt32_Type
) < 0) return 0;
782 if (PyModule_AddObject(this_module
, "Int64",
783 (PyObject
*)&DBusPyInt64_Type
) < 0) return 0;
784 if (PyModule_AddObject(this_module
, "UInt64",
785 (PyObject
*)&DBusPyUInt64_Type
) < 0) return 0;
786 if (PyModule_AddObject(this_module
, "Boolean",
787 (PyObject
*)&DBusPyBoolean_Type
) < 0) return 0;
792 /* vim:set ft=c cino< sw=4 sts=4 et: */