Remove from EXTRA_DIST files we'd already be distributing
[dbus-python-phuang.git] / _dbus_bindings / int.c
bloba148d3aae835935d11e463d026b3605ff29176f6
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"
34 "\n"
35 "Constructor::\n"
36 "\n"
37 " dbus.Boolean(value[, variant_level]) -> Boolean\n"
38 "\n"
39 "``value`` is converted to 0 or 1 as if by ``int(bool(value))``.\n"
40 "\n"
41 "``variant_level`` must be non-negative; the default is 0.\n"
42 "\n"
43 ":IVariables:\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"
51 static PyObject *
52 Boolean_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
54 PyObject *tuple, *self, *value = Py_None;
55 long variantness = 0;
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");
63 return NULL;
65 tuple = Py_BuildValue("(i)", PyObject_IsTrue(value) ? 1 : 0);
66 if (!tuple) return NULL;
67 self = (DBusPyIntBase_Type.tp_new)(cls, tuple, kwargs);
68 Py_DECREF(tuple);
69 return self;
72 static PyObject *
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",
80 variant_level);
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))
90 "dbus.Boolean",
93 0, /* tp_dealloc */
94 0, /* tp_print */
95 0, /* tp_getattr */
96 0, /* tp_setattr */
97 0, /* tp_compare */
98 Boolean_tp_repr, /* tp_repr */
99 0, /* tp_as_number */
100 0, /* tp_as_sequence */
101 0, /* tp_as_mapping */
102 0, /* tp_hash */
103 0, /* tp_call */
104 0, /* tp_str */
105 0, /* tp_getattro */
106 0, /* tp_setattro */
107 0, /* tp_as_buffer */
108 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
109 Boolean_tp_doc, /* tp_doc */
110 0, /* tp_traverse */
111 0, /* tp_clear */
112 0, /* tp_richcompare */
113 0, /* tp_weaklistoffset */
114 0, /* tp_iter */
115 0, /* tp_iternext */
116 0, /* tp_methods */
117 0, /* tp_members */
118 0, /* tp_getset */
119 DEFERRED_ADDRESS(&DBusPyIntBase_Type), /* tp_base */
120 0, /* tp_dict */
121 0, /* tp_descr_get */
122 0, /* tp_descr_set */
123 0, /* tp_dictoffset */
124 0, /* tp_init */
125 0, /* tp_alloc */
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"
134 "\n"
135 "Constructor::\n"
136 "\n"
137 " dbus.Int16(value: int[, variant_level: int]) -> Int16\n"
138 "\n"
139 "value must be within the allowed range, or OverflowError will be\n"
140 "raised.\n"
141 "\n"
142 " variant_level must be non-negative; the default is 0.\n"
143 "\n"
144 ":IVariables:\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"
152 dbus_int16_t
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",
159 (int)i);
160 return -1;
162 return i;
165 static PyObject *
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()) {
170 Py_DECREF(self);
171 return NULL;
173 return self;
176 PyTypeObject DBusPyInt16_Type = {
177 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
179 "dbus.Int16",
182 0, /* tp_dealloc */
183 0, /* tp_print */
184 0, /* tp_getattr */
185 0, /* tp_setattr */
186 0, /* tp_compare */
187 0, /* tp_repr */
188 0, /* tp_as_number */
189 0, /* tp_as_sequence */
190 0, /* tp_as_mapping */
191 0, /* tp_hash */
192 0, /* tp_call */
193 0, /* tp_str */
194 0, /* tp_getattro */
195 0, /* tp_setattro */
196 0, /* tp_as_buffer */
197 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
198 Int16_tp_doc, /* tp_doc */
199 0, /* tp_traverse */
200 0, /* tp_clear */
201 0, /* tp_richcompare */
202 0, /* tp_weaklistoffset */
203 0, /* tp_iter */
204 0, /* tp_iternext */
205 0, /* tp_methods */
206 0, /* tp_members */
207 0, /* tp_getset */
208 DEFERRED_ADDRESS(&DBusPyIntBase_Type), /* tp_base */
209 0, /* tp_dict */
210 0, /* tp_descr_get */
211 0, /* tp_descr_set */
212 0, /* tp_dictoffset */
213 0, /* tp_init */
214 0, /* tp_alloc */
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"
223 "\n"
224 "Constructor::\n"
225 "\n"
226 " dbus.UInt16(value: int[, variant_level: int]) -> UInt16\n"
227 "\n"
228 "``value`` must be within the allowed range, or `OverflowError` will be\n"
229 "raised.\n"
230 "\n"
231 "``variant_level`` must be non-negative; the default is 0.\n"
232 "\n"
233 ":IVariables:\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"
241 dbus_uint16_t
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",
248 (int)i);
249 return (dbus_uint16_t)(-1);
251 return i;
254 static PyObject *
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()) {
260 Py_DECREF (self);
261 return NULL;
263 return self;
266 PyTypeObject DBusPyUInt16_Type = {
267 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
269 "dbus.UInt16",
272 0, /* tp_dealloc */
273 0, /* tp_print */
274 0, /* tp_getattr */
275 0, /* tp_setattr */
276 0, /* tp_compare */
277 0, /* tp_repr */
278 0, /* tp_as_number */
279 0, /* tp_as_sequence */
280 0, /* tp_as_mapping */
281 0, /* tp_hash */
282 0, /* tp_call */
283 0, /* tp_str */
284 0, /* tp_getattro */
285 0, /* tp_setattro */
286 0, /* tp_as_buffer */
287 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
288 UInt16_tp_doc, /* tp_doc */
289 0, /* tp_traverse */
290 0, /* tp_clear */
291 0, /* tp_richcompare */
292 0, /* tp_weaklistoffset */
293 0, /* tp_iter */
294 0, /* tp_iternext */
295 0, /* tp_methods */
296 0, /* tp_members */
297 0, /* tp_getset */
298 DEFERRED_ADDRESS(&DBusPyIntBase_Type), /* tp_base */
299 0, /* tp_dict */
300 0, /* tp_descr_get */
301 0, /* tp_descr_set */
302 0, /* tp_dictoffset */
303 0, /* tp_init */
304 0, /* tp_alloc */
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"
313 "\n"
314 "Constructor::\n"
315 "\n"
316 " dbus.Int32(value: int[, variant_level: int]) -> Int32\n"
317 "\n"
318 "``value`` must be within the allowed range, or `OverflowError` will be\n"
319 "raised.\n"
320 "\n"
321 "``variant_level`` must be non-negative; the default is 0.\n"
322 "\n"
323 ":IVariables:\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"
331 dbus_int32_t
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",
338 (int)i);
339 return -1;
341 return i;
344 static PyObject *
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()) {
349 Py_DECREF(self);
350 return NULL;
352 return self;
355 PyTypeObject DBusPyInt32_Type = {
356 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
358 "dbus.Int32",
361 0, /* tp_dealloc */
362 0, /* tp_print */
363 0, /* tp_getattr */
364 0, /* tp_setattr */
365 0, /* tp_compare */
366 0, /* tp_repr */
367 0, /* tp_as_number */
368 0, /* tp_as_sequence */
369 0, /* tp_as_mapping */
370 0, /* tp_hash */
371 0, /* tp_call */
372 0, /* tp_str */
373 0, /* tp_getattro */
374 0, /* tp_setattro */
375 0, /* tp_as_buffer */
376 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
377 Int32_tp_doc, /* tp_doc */
378 0, /* tp_traverse */
379 0, /* tp_clear */
380 0, /* tp_richcompare */
381 0, /* tp_weaklistoffset */
382 0, /* tp_iter */
383 0, /* tp_iternext */
384 0, /* tp_methods */
385 0, /* tp_members */
386 0, /* tp_getset */
387 DEFERRED_ADDRESS(&DBusPyIntBase_Type), /* tp_base */
388 0, /* tp_dict */
389 0, /* tp_descr_get */
390 0, /* tp_descr_set */
391 0, /* tp_dictoffset */
392 0, /* tp_init */
393 0, /* tp_alloc */
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"
402 "\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"
405 "\n"
406 "Constructor::\n"
407 "\n"
408 " dbus.UInt32(value: long[, variant_level: int]) -> UInt32\n"
409 "\n"
410 "``value`` must be within the allowed range, or `OverflowError` will be\n"
411 "raised.\n"
412 "\n"
413 "``variant_level`` must be non-negative; the default is 0.\n"
414 "\n"
415 ":IVariables:\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"
423 dbus_uint32_t
424 dbus_py_uint32_range_check(PyObject *obj)
426 unsigned long i;
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()) {
432 Py_DECREF(long_obj);
433 return (dbus_uint32_t)(-1);
435 if (i > UINT32_MAX) {
436 PyErr_Format(PyExc_OverflowError, "Value %d out of range for UInt32",
437 (int)i);
438 Py_DECREF(long_obj);
439 return (dbus_uint32_t)(-1);
441 Py_DECREF(long_obj);
442 return i;
445 static PyObject *
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()) {
451 Py_DECREF(self);
452 return NULL;
454 return self;
457 PyTypeObject DBusPyUInt32_Type = {
458 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
460 "dbus.UInt32",
463 0, /* tp_dealloc */
464 0, /* tp_print */
465 0, /* tp_getattr */
466 0, /* tp_setattr */
467 0, /* tp_compare */
468 0, /* tp_repr */
469 0, /* tp_as_number */
470 0, /* tp_as_sequence */
471 0, /* tp_as_mapping */
472 0, /* tp_hash */
473 0, /* tp_call */
474 0, /* tp_str */
475 0, /* tp_getattro */
476 0, /* tp_setattro */
477 0, /* tp_as_buffer */
478 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
479 UInt32_tp_doc, /* tp_doc */
480 0, /* tp_traverse */
481 0, /* tp_clear */
482 0, /* tp_richcompare */
483 0, /* tp_weaklistoffset */
484 0, /* tp_iter */
485 0, /* tp_iternext */
486 0, /* tp_methods */
487 0, /* tp_members */
488 0, /* tp_getset */
489 DEFERRED_ADDRESS(&DBusPyLongBase_Type), /* tp_base */
490 0, /* tp_dict */
491 0, /* tp_descr_get */
492 0, /* tp_descr_set */
493 0, /* tp_dictoffset */
494 0, /* tp_init */
495 0, /* tp_alloc */
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"
504 "\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"
507 "\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) */
517 "\n"
518 "Constructor::\n"
519 "\n"
520 " dbus.Int64(value: long[, variant_level: int]) -> Int64\n"
521 "\n"
522 "``value`` must be within the allowed range, or `OverflowError` will be\n"
523 "raised.\n"
524 "\n"
525 "``variant_level`` must be non-negative; the default is 0.\n"
526 "\n"
527 ":IVariables:\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
536 dbus_int64_t
537 dbus_py_int64_range_check(PyObject *obj)
539 PY_LONG_LONG i;
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()) {
545 Py_DECREF(long_obj);
546 return -1;
548 if (i < INT64_MIN || i > INT64_MAX) {
549 PyErr_SetString(PyExc_OverflowError, "Value out of range for Int64");
550 Py_DECREF(long_obj);
551 return -1;
553 Py_DECREF(long_obj);
554 return i;
556 #endif
558 static PyObject *
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()) {
564 Py_DECREF(self);
565 return NULL;
567 return self;
568 #else
569 PyErr_SetString(PyExc_NotImplementedError,
570 "64-bit types are not available on this platform");
571 return NULL;
572 #endif
575 PyTypeObject DBusPyInt64_Type = {
576 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
578 "dbus.Int64",
581 0, /* tp_dealloc */
582 0, /* tp_print */
583 0, /* tp_getattr */
584 0, /* tp_setattr */
585 0, /* tp_compare */
586 0, /* tp_repr */
587 0, /* tp_as_number */
588 0, /* tp_as_sequence */
589 0, /* tp_as_mapping */
590 0, /* tp_hash */
591 0, /* tp_call */
592 0, /* tp_str */
593 0, /* tp_getattro */
594 0, /* tp_setattro */
595 0, /* tp_as_buffer */
596 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
597 Int64_tp_doc, /* tp_doc */
598 0, /* tp_traverse */
599 0, /* tp_clear */
600 0, /* tp_richcompare */
601 0, /* tp_weaklistoffset */
602 0, /* tp_iter */
603 0, /* tp_iternext */
604 0, /* tp_methods */
605 0, /* tp_members */
606 0, /* tp_getset */
607 DEFERRED_ADDRESS(&DBusPyLongBase_Type), /* tp_base */
608 0, /* tp_dict */
609 0, /* tp_descr_get */
610 0, /* tp_descr_set */
611 0, /* tp_dictoffset */
612 0, /* tp_init */
613 0, /* tp_alloc */
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"
622 "\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) */
632 "\n"
633 "Constructor::\n"
634 "\n"
635 " dbus.UInt64(value: long[, variant_level: int]) -> UInt64\n"
636 "\n"
637 "``value`` must be within the allowed range, or `OverflowError` will be\n"
638 "raised.\n"
639 "\n"
640 "``variant_level`` must be non-negative; the default is 0.\n"
641 "\n"
642 ":IVariables:\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"
650 dbus_uint64_t
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()) {
659 Py_DECREF(long_obj);
660 return (dbus_uint64_t)(-1);
662 if (i > UINT64_MAX) {
663 PyErr_SetString(PyExc_OverflowError, "Value out of range for UInt64");
664 Py_DECREF(long_obj);
665 return (dbus_uint64_t)(-1);
667 Py_DECREF(long_obj);
668 return i;
671 static PyObject *
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()) {
678 Py_DECREF(self);
679 return NULL;
681 return self;
682 #else
683 PyErr_SetString(PyExc_NotImplementedError,
684 "64-bit integer types are not supported on this platform");
685 return NULL;
686 #endif
689 PyTypeObject DBusPyUInt64_Type = {
690 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
692 "dbus.UInt64",
695 0, /* tp_dealloc */
696 0, /* tp_print */
697 0, /* tp_getattr */
698 0, /* tp_setattr */
699 0, /* tp_compare */
700 0, /* tp_repr */
701 0, /* tp_as_number */
702 0, /* tp_as_sequence */
703 0, /* tp_as_mapping */
704 0, /* tp_hash */
705 0, /* tp_call */
706 0, /* tp_str */
707 0, /* tp_getattro */
708 0, /* tp_setattro */
709 0, /* tp_as_buffer */
710 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
711 UInt64_tp_doc, /* tp_doc */
712 0, /* tp_traverse */
713 0, /* tp_clear */
714 0, /* tp_richcompare */
715 0, /* tp_weaklistoffset */
716 0, /* tp_iter */
717 0, /* tp_iternext */
718 0, /* tp_methods */
719 0, /* tp_members */
720 0, /* tp_getset */
721 DEFERRED_ADDRESS(&DBusPyLongBase_Type), /* tp_base */
722 0, /* tp_dict */
723 0, /* tp_descr_get */
724 0, /* tp_descr_set */
725 0, /* tp_dictoffset */
726 0, /* tp_init */
727 0, /* tp_alloc */
728 UInt64_tp_new, /* tp_new */
731 dbus_bool_t
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
737 desired */
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;
760 #endif
761 return 1;
764 dbus_bool_t
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;
789 return 1;
792 /* vim:set ft=c cino< sw=4 sts=4 et: */