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
27 /* Specific types =================================================== */
29 /* Boolean, a subclass of DBusPythonInt ============================= */
31 static PyTypeObject BooleanType
;
35 PyDoc_STRVAR(Boolean_tp_doc
,
36 "A boolean, represented as a subtype of `int` (not `bool`, because `bool`\n"
37 "cannot be subclassed).\n"
40 " ``from dbus import Boolean`` or ``from dbus.types import Boolean``\n"
43 " Boolean(value: int[, variant_level: int]) -> Boolean\n"
45 " value is converted to 0 or 1.\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 boolean, this is represented in Python by a\n"
54 " Boolean with variant_level==2.\n"
58 Boolean_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
60 PyObject
*tuple
, *self
, *value
= Py_None
;
62 static char *argnames
[] = {"_", "variant_level", NULL
};
64 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|Ol:__new__", argnames
,
65 &value
, &variantness
)) return NULL
;
66 if (variantness
< 0) {
67 PyErr_SetString(PyExc_ValueError
,
68 "variant_level must be non-negative");
71 tuple
= Py_BuildValue("(i)", PyObject_IsTrue(value
) ? 1 : 0);
72 if (!tuple
) return NULL
;
73 self
= (DBusPythonIntType
.tp_new
)(cls
, tuple
, kwargs
);
79 Boolean_tp_repr (PyObject
*self
)
81 long variant_level
= ((DBusPythonInt
*)self
)->variant_level
;
82 if (variant_level
> 0) {
83 return PyString_FromFormat("%s(%s, variant_level=%ld)",
84 self
->ob_type
->tp_name
,
85 PyInt_AsLong(self
) ? "True" : "False",
88 return PyString_FromFormat("%s(%s)",
89 self
->ob_type
->tp_name
,
90 PyInt_AsLong(self
) ? "True" : "False");
93 static PyTypeObject BooleanType
= {
94 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
104 Boolean_tp_repr
, /* tp_repr */
105 0, /* tp_as_number */
106 0, /* tp_as_sequence */
107 0, /* tp_as_mapping */
113 0, /* tp_as_buffer */
114 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
115 Boolean_tp_doc
, /* tp_doc */
118 0, /* tp_richcompare */
119 0, /* tp_weaklistoffset */
125 DEFERRED_ADDRESS(&DBusPythonInt_Type
), /* tp_base */
127 0, /* tp_descr_get */
128 0, /* tp_descr_set */
129 0, /* tp_dictoffset */
132 Boolean_tp_new
, /* tp_new */
135 /* Int16 ============================================================ */
137 static PyTypeObject Int16Type
;
141 PyDoc_STRVAR(Int16_tp_doc
,
142 "A signed 16-bit integer between -0x8000 and +0x7FFF, represented as\n"
143 "a subtype of `int`.\n"
146 " ``from dbus import Int16`` or ``from dbus.types import Int16``\n"
149 " Int16(value: int[, variant_level: int]) -> Int16\n"
150 " value must be within the allowed range, or OverflowError will be\n"
152 " variant_level must be non-negative; the default is 0.\n"
155 " `variant_level` : int\n"
156 " Indicates how many nested Variant containers this object\n"
157 " is contained in: if a message's wire format has a variant containing a\n"
158 " variant containing an int16, this is represented in Python by an\n"
159 " Int16 with variant_level==2.\n"
163 int16_range_check(PyObject
*obj
)
165 long i
= PyInt_AsLong (obj
);
166 if (i
== -1 && PyErr_Occurred ()) return -1;
167 if (i
< -0x8000 || i
> 0x7fff) {
168 PyErr_Format(PyExc_OverflowError
, "Value %d out of range for Int16",
176 Int16_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
178 PyObject
*self
= (DBusPythonIntType
.tp_new
)(cls
, args
, kwargs
);
179 if (self
&& int16_range_check(self
) == -1 && PyErr_Occurred()) {
186 static PyTypeObject Int16Type
= {
187 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
198 0, /* tp_as_number */
199 0, /* tp_as_sequence */
200 0, /* tp_as_mapping */
206 0, /* tp_as_buffer */
207 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
208 Int16_tp_doc
, /* tp_doc */
211 0, /* tp_richcompare */
212 0, /* tp_weaklistoffset */
218 DEFERRED_ADDRESS(&DBusPythonIntType
), /* tp_base */
220 0, /* tp_descr_get */
221 0, /* tp_descr_set */
222 0, /* tp_dictoffset */
225 Int16_tp_new
, /* tp_new */
228 /* UInt16 =========================================================== */
230 static PyTypeObject UInt16Type
;
234 PyDoc_STRVAR(UInt16_tp_doc
,
235 "An unsigned 16-bit integer between 0 and 0xFFFF, represented as\n"
236 "a subtype of `int`.\n"
239 " ``from dbus import UInt16`` or ``from dbus.types import UInt16``\n"
242 " UInt16(value: int[, variant_level: int]) -> UInt16\n"
243 " value must be within the allowed range, or OverflowError will be\n"
245 " variant_level must be non-negative; the default is 0.\n"
248 " `variant_level` : int\n"
249 " Indicates how many nested Variant containers this object\n"
250 " is contained in: if a message's wire format has a variant containing a\n"
251 " variant containing a uint16, this is represented in Python by a\n"
252 " UInt16 with variant_level==2.\n"
256 uint16_range_check(PyObject
*obj
)
258 long i
= PyInt_AsLong(obj
);
259 if (i
== -1 && PyErr_Occurred()) return (dbus_uint16_t
)(-1);
260 if (i
< 0 || i
> 0xffff) {
261 PyErr_Format(PyExc_OverflowError
, "Value %d out of range for UInt16",
263 return (dbus_uint16_t
)(-1);
269 UInt16_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
271 PyObject
*self
= (DBusPythonIntType
.tp_new
)(cls
, args
, kwargs
);
272 if (self
&& uint16_range_check(self
) == (dbus_uint16_t
)(-1)
273 && PyErr_Occurred()) {
280 static PyTypeObject UInt16Type
= {
281 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
292 0, /* tp_as_number */
293 0, /* tp_as_sequence */
294 0, /* tp_as_mapping */
300 0, /* tp_as_buffer */
301 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
302 UInt16_tp_doc
, /* tp_doc */
305 0, /* tp_richcompare */
306 0, /* tp_weaklistoffset */
312 DEFERRED_ADDRESS(&DBusPythonIntType
), /* tp_base */
314 0, /* tp_descr_get */
315 0, /* tp_descr_set */
316 0, /* tp_dictoffset */
319 UInt16_tp_new
, /* tp_new */
322 /* Int32 ============================================================ */
324 static PyTypeObject Int32Type
;
328 PyDoc_STRVAR(Int32_tp_doc
,
329 "A signed 32-bit integer between -0x8000 0000 and +0x7FFF FFFF, represented as\n"
330 "a subtype of `int`.\n"
333 " ``from dbus import Int32`` or ``from dbus.types import Int32``\n"
336 " Int32(value: int[, variant_level: int]) -> Int32\n"
337 " value must be within the allowed range, or OverflowError will be\n"
339 " variant_level must be non-negative; the default is 0.\n"
342 " `variant_level` : int\n"
343 " Indicates how many nested Variant containers this object\n"
344 " is contained in: if a message's wire format has a variant containing a\n"
345 " variant containing an int32, this is represented in Python by an\n"
346 " Int32 with variant_level==2.\n"
350 int32_range_check(PyObject
*obj
)
352 long i
= PyInt_AsLong(obj
);
353 if (i
== -1 && PyErr_Occurred()) return -1;
354 if (i
< INT32_MIN
|| i
> INT32_MAX
) {
355 PyErr_Format(PyExc_OverflowError
, "Value %d out of range for Int32",
363 Int32_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
365 PyObject
*self
= (DBusPythonIntType
.tp_new
)(cls
, args
, kwargs
);
366 if (self
&& int32_range_check(self
) == -1 && PyErr_Occurred()) {
373 static PyTypeObject Int32Type
= {
374 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
385 0, /* tp_as_number */
386 0, /* tp_as_sequence */
387 0, /* tp_as_mapping */
393 0, /* tp_as_buffer */
394 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
395 Int32_tp_doc
, /* tp_doc */
398 0, /* tp_richcompare */
399 0, /* tp_weaklistoffset */
405 DEFERRED_ADDRESS(&DBusPythonIntType
), /* tp_base */
407 0, /* tp_descr_get */
408 0, /* tp_descr_set */
409 0, /* tp_dictoffset */
412 Int32_tp_new
, /* tp_new */
415 /* UInt32 =========================================================== */
417 static PyTypeObject UInt32Type
;
421 PyDoc_STRVAR(UInt32_tp_doc
,
422 "An unsigned 32-bit integer between 0 and 0xFFFF FFFF, represented as a\n"
423 "subtype of `long`.\n"
425 "Note that this may be changed in future to be a subtype of `int` on\n"
426 "64-bit platforms; applications should not rely on either behaviour.\n"
429 " ``from dbus import UInt32`` or ``from dbus.types import UInt32``\n"
432 " UInt32(value: long[, variant_level: int]) -> UInt32\n"
433 " value must be within the allowed range, or OverflowError will be\n"
435 " variant_level must be non-negative; the default is 0.\n"
438 " `variant_level` : int\n"
439 " Indicates how many nested Variant containers this object\n"
440 " is contained in: if a message's wire format has a variant containing a\n"
441 " variant containing a uint32, this is represented in Python by a\n"
442 " UInt32 with variant_level==2.\n"
446 uint32_range_check(PyObject
*obj
)
449 PyObject
*long_obj
= PyNumber_Long(obj
);
451 if (!long_obj
) return (dbus_uint32_t
)(-1);
452 i
= PyLong_AsUnsignedLong(long_obj
);
453 if (i
== (unsigned long)(-1) && PyErr_Occurred()) {
455 return (dbus_uint32_t
)(-1);
457 if (i
> UINT32_MAX
) {
458 PyErr_Format(PyExc_OverflowError
, "Value %d out of range for UInt32",
461 return (dbus_uint32_t
)(-1);
468 UInt32_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
470 PyObject
*self
= (DBusPythonLongType
.tp_new
)(cls
, args
, kwargs
);
471 if (self
&& uint32_range_check(self
) == (dbus_uint32_t
)(-1)
472 && PyErr_Occurred()) {
479 static PyTypeObject UInt32Type
= {
480 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
491 0, /* tp_as_number */
492 0, /* tp_as_sequence */
493 0, /* tp_as_mapping */
499 0, /* tp_as_buffer */
500 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
501 UInt32_tp_doc
, /* tp_doc */
504 0, /* tp_richcompare */
505 0, /* tp_weaklistoffset */
511 DEFERRED_ADDRESS(&DBusPythonLongType
), /* tp_base */
513 0, /* tp_descr_get */
514 0, /* tp_descr_set */
515 0, /* tp_dictoffset */
518 UInt32_tp_new
, /* tp_new */
521 /* Int64 =========================================================== */
523 #if defined(DBUS_HAVE_INT64) && defined(HAVE_LONG_LONG)
524 # define DBUS_PYTHON_64_BIT_WORKS 1
526 # undef DBUS_PYTHON_64_BIT_WORKS
527 #endif /* defined(DBUS_HAVE_INT64) && defined(HAVE_LONG_LONG) */
529 static PyTypeObject Int64Type
;
533 PyDoc_STRVAR(Int64_tp_doc
,
534 "A signed 64-bit integer between -0x8000 0000 0000 0000 and\n"
535 "+0x7FFF FFFF FFFF FFFF, represented as a subtype of `long`.\n"
537 "Note that this may be changed in future to be a subtype of `int` on\n"
538 "64-bit platforms; applications should not rely on either behaviour.\n"
540 "This type only works on platforms where the C compiler has suitable\n"
541 "64-bit types, such as C99 ``long long``.\n"
542 #ifdef DBUS_PYTHON_64_BIT_WORKS
543 "This is the case on your current platform.\n"
544 #else /* !defined(DBUS_PYTHON_64_BIT_WORKS) */
545 "This is not the case on your current platform, so this type's\n"
546 "constructor will always raise NotImplementedError. Try a better\n"
547 "compiler, if one is available.\n"
548 #endif /* !defined(DBUS_PYTHON_64_BIT_WORKS) */
551 " ``from dbus import Int64`` or ``from dbus.types import Int64``\n"
554 " Int64(value: long[, variant_level: int]) -> Int64\n"
555 " value must be within the allowed range, or OverflowError will be\n"
557 " variant_level must be non-negative; the default is 0.\n"
560 " `variant_level` : int\n"
561 " Indicates how many nested Variant containers this object\n"
562 " is contained in: if a message's wire format has a variant containing a\n"
563 " variant containing an int64, this is represented in Python by an\n"
564 " Int64 with variant_level==2.\n"
567 #ifdef DBUS_PYTHON_64_BIT_WORKS
569 int64_range_check(PyObject
*obj
)
572 PyObject
*long_obj
= PyNumber_Long(obj
);
574 if (!long_obj
) return -1;
575 i
= PyLong_AsLongLong(long_obj
);
576 if (i
== -1 && PyErr_Occurred()) {
580 if (i
< INT64_MIN
|| i
> INT64_MAX
) {
581 PyErr_SetString(PyExc_OverflowError
, "Value out of range for Int64");
591 Int64_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
593 #ifdef DBUS_PYTHON_64_BIT_WORKS
594 PyObject
*self
= (DBusPythonLongType
.tp_new
)(cls
, args
, kwargs
);
595 if (self
&& int64_range_check(self
) == -1 && PyErr_Occurred()) {
601 PyErr_SetString(PyExc_NotImplementedError
,
602 "64-bit types are not available on this platform");
607 static PyTypeObject Int64Type
= {
608 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
619 0, /* tp_as_number */
620 0, /* tp_as_sequence */
621 0, /* tp_as_mapping */
627 0, /* tp_as_buffer */
628 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
629 Int64_tp_doc
, /* tp_doc */
632 0, /* tp_richcompare */
633 0, /* tp_weaklistoffset */
639 DEFERRED_ADDRESS(&DBusPythonLongType
), /* tp_base */
641 0, /* tp_descr_get */
642 0, /* tp_descr_set */
643 0, /* tp_dictoffset */
646 Int64_tp_new
, /* tp_new */
649 /* UInt64 =========================================================== */
651 static PyTypeObject UInt64Type
;
655 PyDoc_STRVAR(UInt64_tp_doc
,
656 "An unsigned 64-bit integer between 0 and 0xFFFF FFFF FFFF FFFF,\n"
657 "represented as a subtype of `long`.\n"
659 "This type only exists on platforms where the C compiler has suitable\n"
660 "64-bit types, such as C99 ``unsigned long long``.\n"
661 #ifdef DBUS_PYTHON_64_BIT_WORKS
662 "This is the case on your current platform.\n"
663 #else /* !defined(DBUS_PYTHON_64_BIT_WORKS) */
664 "This is not the case on your current platform, so this type's\n"
665 "constructor will always raise NotImplementedError. Try a better\n"
666 "compiler, if one is available.\n"
667 #endif /* !defined(DBUS_PYTHON_64_BIT_WORKS) */
670 " UInt64(value: long[, variant_level: int]) -> UInt64\n"
671 " value must be within the allowed range, or OverflowError will be\n"
673 " variant_level must be non-negative; the default is 0.\n"
676 " `variant_level` : int\n"
677 " Indicates how many nested Variant containers this object\n"
678 " is contained in: if a message's wire format has a variant containing a\n"
679 " variant containing a uint64, this is represented in Python by a\n"
680 " UInt64 with variant_level==2.\n"
684 uint64_range_check(PyObject
*obj
)
686 unsigned PY_LONG_LONG i
;
687 PyObject
*long_obj
= PyNumber_Long(obj
);
689 if (!long_obj
) return (dbus_uint64_t
)(-1);
690 i
= PyLong_AsUnsignedLongLong(long_obj
);
691 if (i
== (unsigned PY_LONG_LONG
)(-1) && PyErr_Occurred()) {
693 return (dbus_uint64_t
)(-1);
695 if (i
> UINT64_MAX
) {
696 PyErr_SetString(PyExc_OverflowError
, "Value out of range for UInt64");
698 return (dbus_uint64_t
)(-1);
705 UInt64_tp_new (PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
707 #ifdef DBUS_PYTHON_64_BIT_WORKS
708 PyObject
*self
= (DBusPythonLongType
.tp_new
)(cls
, args
, kwargs
);
709 if (self
&& uint64_range_check(self
) == (dbus_uint64_t
)(-1)
710 && PyErr_Occurred()) {
716 PyErr_SetString(PyExc_NotImplementedError
,
717 "64-bit integer types are not supported on this platform");
722 static PyTypeObject UInt64Type
= {
723 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
734 0, /* tp_as_number */
735 0, /* tp_as_sequence */
736 0, /* tp_as_mapping */
742 0, /* tp_as_buffer */
743 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
744 UInt64_tp_doc
, /* tp_doc */
747 0, /* tp_richcompare */
748 0, /* tp_weaklistoffset */
754 DEFERRED_ADDRESS(&DBusPythonLongType
), /* tp_base */
756 0, /* tp_descr_get */
757 0, /* tp_descr_set */
758 0, /* tp_dictoffset */
761 UInt64_tp_new
, /* tp_new */
764 /* UTF-8 string representation ====================================== */
766 static PyTypeObject UTF8StringType
;
768 DEFINE_CHECK(UTF8String
)
770 PyDoc_STRVAR(UTF8String_tp_doc
,
771 "A string represented using UTF-8 - a subtype of `str`.\n"
774 " UTF8String(value: str or unicode[, variant_level: int]) -> UTF8String\n"
775 " If value is a str object it must be valid UTF-8.\n"
777 " variant_level must be non-negative; the default is 0.\n"
780 " `variant_level` : int\n"
781 " Indicates how many nested Variant containers this object\n"
782 " is contained in: if a message's wire format has a variant containing a\n"
783 " variant containing a string, this is represented in Python by a\n"
784 " String or UTF8String with variant_level==2.\n"
785 ":Since: 0.80 (in older versions, use dbus.String)\n"
789 UTF8String_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
791 const char *str
= NULL
;
792 long variantness
= 0;
793 static char *argnames
[] = {"value", "variant_level", NULL
};
796 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|l:__new__", argnames
,
797 &str
, &variantness
)) return NULL
;
798 unicode
= PyUnicode_DecodeUTF8(str
, strlen(str
), NULL
);
799 if (!unicode
) return NULL
;
801 return (DBusPythonStringType
.tp_new
)(cls
, args
, kwargs
);
804 static PyTypeObject UTF8StringType
= {
805 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
816 0, /* tp_as_number */
817 0, /* tp_as_sequence */
818 0, /* tp_as_mapping */
824 0, /* tp_as_buffer */
825 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
826 UTF8String_tp_doc
, /* tp_doc */
829 0, /* tp_richcompare */
830 0, /* tp_weaklistoffset */
836 DEFERRED_ADDRESS(&DBusPythonStringType
), /* tp_base */
838 0, /* tp_descr_get */
839 0, /* tp_descr_set */
840 0, /* tp_dictoffset */
843 UTF8String_tp_new
, /* tp_new */
846 /* Object path ====================================================== */
848 static PyTypeObject ObjectPathType
;
850 DEFINE_CHECK(ObjectPath
)
852 PyDoc_STRVAR(ObjectPath_tp_doc
,
853 "A D-Bus object path, such as '/com/example/MyApp/Documents/abc'.\n"
855 "ObjectPath is a subtype of str, and object-paths behave like strings.\n"
858 " ObjectPath(path: str[, variant_level: int]) -> ObjectPath\n"
859 " path must be an ASCII string following the syntax of object paths.\n"
860 " variant_level must be non-negative; the default is 0.\n"
863 " `variant_level` : int\n"
864 " Indicates how many nested Variant containers this object\n"
865 " is contained in: if a message's wire format has a variant containing a\n"
866 " variant containing an object path, this is represented in Python by an\n"
867 " ObjectPath with variant_level==2.\n"
871 ObjectPath_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
873 const char *str
= NULL
;
874 long variantness
= 0;
875 static char *argnames
[] = {"object_path", "variant_level", NULL
};
877 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|l:__new__", argnames
,
878 &str
, &variantness
)) return NULL
;
879 if (!dbus_py_validate_object_path(str
)) {
882 return (DBusPythonStringType
.tp_new
)(cls
, args
, kwargs
);
885 static PyTypeObject ObjectPathType
= {
886 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
897 0, /* tp_as_number */
898 0, /* tp_as_sequence */
899 0, /* tp_as_mapping */
905 0, /* tp_as_buffer */
906 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
907 ObjectPath_tp_doc
, /* tp_doc */
910 0, /* tp_richcompare */
911 0, /* tp_weaklistoffset */
917 DEFERRED_ADDRESS(&DBusPythonStringType
), /* tp_base */
919 0, /* tp_descr_get */
920 0, /* tp_descr_set */
921 0, /* tp_dictoffset */
924 ObjectPath_tp_new
, /* tp_new */
927 /* Unicode string representation ==================================== */
929 static PyTypeObject StringType
;
932 PyDoc_STRVAR(String_tp_doc
,
933 "A string represented using Unicode - a subtype of `unicode`.\n"
936 " String(value: str or unicode[, variant_level: int]) -> String\n"
938 " variant_level must be non-negative; the default is 0.\n"
941 " `variant_level` : int\n"
942 " Indicates how many nested Variant containers this object\n"
943 " is contained in: if a message's wire format has a variant containing a\n"
944 " variant containing a string, this is represented in Python by a\n"
945 " String or UTF8String with variant_level==2.\n"
949 String_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
952 PyObject
*variantness
= NULL
;
953 static char *argnames
[] = {"variant_level", NULL
};
955 if (PyTuple_Size(args
) > 1) {
956 PyErr_SetString(PyExc_TypeError
,
957 "__new__ takes at most one positional parameter");
960 if (!PyArg_ParseTupleAndKeywords(empty_tuple
, kwargs
,
961 "|O!:__new__", argnames
,
962 &PyInt_Type
, &variantness
)) return NULL
;
964 variantness
= PyInt_FromLong(0);
965 if (!variantness
) return NULL
;
967 if (PyInt_AS_LONG(variantness
) < 0) {
968 PyErr_SetString(PyExc_ValueError
,
969 "variant_level must be non-negative");
973 self
= (PyUnicode_Type
.tp_new
)(cls
, args
, NULL
);
975 PyObject_GenericSetAttr(self
, variant_level_const
, variantness
);
981 String_tp_repr(PyObject
*self
)
983 PyObject
*parent_repr
= (PyUnicode_Type
.tp_repr
)(self
);
988 if (!parent_repr
) return NULL
;
989 vl_obj
= PyObject_GetAttr(self
, variant_level_const
);
990 if (!vl_obj
) return NULL
;
991 variant_level
= PyInt_AsLong(vl_obj
);
992 if (variant_level
> 0) {
993 my_repr
= PyString_FromFormat("%s(%s, variant_level=%ld)",
994 self
->ob_type
->tp_name
,
995 PyString_AS_STRING(parent_repr
),
999 my_repr
= PyString_FromFormat("%s(%s)", self
->ob_type
->tp_name
,
1000 PyString_AS_STRING(parent_repr
));
1002 /* whether my_repr is NULL or not: */
1003 Py_DECREF(parent_repr
);
1007 static PyTypeObject StringType
= {
1008 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type
))
1011 INT_MAX
, /* placeholder */
1018 String_tp_repr
, /* tp_repr */
1019 0, /* tp_as_number */
1020 0, /* tp_as_sequence */
1021 0, /* tp_as_mapping */
1025 PyObject_GenericGetAttr
, /* tp_getattro */
1026 Glue_immutable_setattro
, /* tp_setattro */
1027 0, /* tp_as_buffer */
1028 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
1029 String_tp_doc
, /* tp_doc */
1030 0, /* tp_traverse */
1032 0, /* tp_richcompare */
1033 0, /* tp_weaklistoffset */
1035 0, /* tp_iternext */
1039 DEFERRED_ADDRESS(&PyUnicode_Type
), /* tp_base */
1041 0, /* tp_descr_get */
1042 0, /* tp_descr_set */
1043 -sizeof(void *), /* tp_dictoffset */
1046 String_tp_new
, /* tp_new */
1052 Int16Type
.tp_base
= &DBusPythonIntType
;
1053 if (PyType_Ready(&Int16Type
) < 0) return 0;
1054 /* disable the tp_print copied from PyInt_Type, so tp_repr gets called as
1056 Int16Type
.tp_print
= NULL
;
1058 UInt16Type
.tp_base
= &DBusPythonIntType
;
1059 if (PyType_Ready(&UInt16Type
) < 0) return 0;
1060 UInt16Type
.tp_print
= NULL
;
1062 Int32Type
.tp_base
= &DBusPythonIntType
;
1063 if (PyType_Ready(&Int32Type
) < 0) return 0;
1064 Int32Type
.tp_print
= NULL
;
1066 UInt32Type
.tp_base
= &DBusPythonLongType
;
1067 if (PyType_Ready(&UInt32Type
) < 0) return 0;
1068 UInt32Type
.tp_print
= NULL
;
1070 #if defined(DBUS_HAVE_INT64) && defined(HAVE_LONG_LONG)
1071 Int64Type
.tp_base
= &DBusPythonLongType
;
1072 if (PyType_Ready(&Int64Type
) < 0) return 0;
1073 Int64Type
.tp_print
= NULL
;
1075 UInt64Type
.tp_base
= &DBusPythonLongType
;
1076 if (PyType_Ready(&UInt64Type
) < 0) return 0;
1077 UInt64Type
.tp_print
= NULL
;
1080 StringType
.tp_basicsize
= PyUnicode_Type
.tp_basicsize
1081 + 2*sizeof(PyObject
*) - 1;
1082 StringType
.tp_basicsize
/= sizeof(PyObject
*);
1083 StringType
.tp_basicsize
*= sizeof(PyObject
*);
1084 StringType
.tp_base
= &PyUnicode_Type
;
1085 if (PyType_Ready(&StringType
) < 0) return 0;
1086 StringType
.tp_print
= NULL
;
1088 UTF8StringType
.tp_basicsize
= PyUnicode_Type
.tp_basicsize
1089 + 2*sizeof(PyObject
*) - 1;
1090 UTF8StringType
.tp_basicsize
/= sizeof(PyObject
*);
1091 UTF8StringType
.tp_basicsize
*= sizeof(PyObject
*);
1092 UTF8StringType
.tp_base
= &DBusPythonStringType
;
1093 if (PyType_Ready(&UTF8StringType
) < 0) return 0;
1094 UTF8StringType
.tp_print
= NULL
;
1096 ObjectPathType
.tp_base
= &DBusPythonStringType
;
1097 if (PyType_Ready(&ObjectPathType
) < 0) return 0;
1098 ObjectPathType
.tp_print
= NULL
;
1100 BooleanType
.tp_base
= &DBusPythonIntType
;
1101 if (PyType_Ready(&BooleanType
) < 0) return 0;
1102 BooleanType
.tp_print
= NULL
;
1108 insert_types(PyObject
*this_module
)
1110 Py_INCREF(&Int16Type
);
1111 Py_INCREF(&UInt16Type
);
1112 Py_INCREF(&Int32Type
);
1113 Py_INCREF(&UInt32Type
);
1114 Py_INCREF(&Int64Type
);
1115 Py_INCREF(&UInt64Type
);
1116 Py_INCREF(&BooleanType
);
1117 if (PyModule_AddObject(this_module
, "Int16",
1118 (PyObject
*)&Int16Type
) < 0) return 0;
1119 if (PyModule_AddObject(this_module
, "UInt16",
1120 (PyObject
*)&UInt16Type
) < 0) return 0;
1121 if (PyModule_AddObject(this_module
, "Int32",
1122 (PyObject
*)&Int32Type
) < 0) return 0;
1123 if (PyModule_AddObject(this_module
, "UInt32",
1124 (PyObject
*)&UInt32Type
) < 0) return 0;
1125 if (PyModule_AddObject(this_module
, "Int64",
1126 (PyObject
*)&Int64Type
) < 0) return 0;
1127 if (PyModule_AddObject(this_module
, "UInt64",
1128 (PyObject
*)&UInt64Type
) < 0) return 0;
1129 if (PyModule_AddObject(this_module
, "Boolean",
1130 (PyObject
*)&BooleanType
) < 0) return 0;
1132 Py_INCREF(&ObjectPathType
);
1133 Py_INCREF(&UTF8StringType
);
1134 Py_INCREF(&StringType
);
1135 if (PyModule_AddObject(this_module
, "ObjectPath",
1136 (PyObject
*)&ObjectPathType
) < 0) return 0;
1137 if (PyModule_AddObject(this_module
, "UTF8String",
1138 (PyObject
*)&UTF8StringType
) < 0) return 0;
1139 if (PyModule_AddObject(this_module
, "String",
1140 (PyObject
*)&StringType
) < 0) return 0;
1145 /* vim:set ft=c cino< sw=4 sts=4 et: */