dbus.bus: In watch_name_owner, only watch the desired name!
[dbus-python-phuang.git] / _dbus_bindings / int.c
blobba00f824eeae6d326a59c9b7302a94767b8d1fb8
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 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program 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 General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "types-internal.h"
25 /* Specific types =================================================== */
27 /* Boolean, a subclass of DBusPythonInt ============================= */
29 PyDoc_STRVAR(Boolean_tp_doc,
30 "A boolean, represented as a subtype of `int` (not `bool`, because `bool`\n"
31 "cannot be subclassed).\n"
32 "\n"
33 "Constructor::\n"
34 "\n"
35 " dbus.Boolean(value[, variant_level]) -> Boolean\n"
36 "\n"
37 "``value`` is converted to 0 or 1 as if by ``int(bool(value))``.\n"
38 "\n"
39 "``variant_level`` must be non-negative; the default is 0.\n"
40 "\n"
41 ":IVariables:\n"
42 " `variant_level` : int\n"
43 " Indicates how many nested Variant containers this object\n"
44 " is contained in: if a message's wire format has a variant containing a\n"
45 " variant containing a boolean, this is represented in Python by a\n"
46 " Boolean with variant_level==2.\n"
49 static PyObject *
50 Boolean_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
52 PyObject *tuple, *self, *value = Py_None;
53 long variantness = 0;
54 static char *argnames[] = {"_", "variant_level", NULL};
56 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ol:__new__", argnames,
57 &value, &variantness)) return NULL;
58 if (variantness < 0) {
59 PyErr_SetString(PyExc_ValueError,
60 "variant_level must be non-negative");
61 return NULL;
63 tuple = Py_BuildValue("(i)", PyObject_IsTrue(value) ? 1 : 0);
64 if (!tuple) return NULL;
65 self = (DBusPyIntBase_Type.tp_new)(cls, tuple, kwargs);
66 Py_DECREF(tuple);
67 return self;
70 static PyObject *
71 Boolean_tp_repr (PyObject *self)
73 long variant_level = ((DBusPyIntBase *)self)->variant_level;
74 if (variant_level > 0) {
75 return PyString_FromFormat("%s(%s, variant_level=%ld)",
76 self->ob_type->tp_name,
77 PyInt_AsLong(self) ? "True" : "False",
78 variant_level);
80 return PyString_FromFormat("%s(%s)",
81 self->ob_type->tp_name,
82 PyInt_AsLong(self) ? "True" : "False");
85 PyTypeObject DBusPyBoolean_Type = {
86 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
88 "dbus.Boolean",
91 0, /* tp_dealloc */
92 0, /* tp_print */
93 0, /* tp_getattr */
94 0, /* tp_setattr */
95 0, /* tp_compare */
96 Boolean_tp_repr, /* tp_repr */
97 0, /* tp_as_number */
98 0, /* tp_as_sequence */
99 0, /* tp_as_mapping */
100 0, /* tp_hash */
101 0, /* tp_call */
102 0, /* tp_str */
103 0, /* tp_getattro */
104 0, /* tp_setattro */
105 0, /* tp_as_buffer */
106 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
107 Boolean_tp_doc, /* tp_doc */
108 0, /* tp_traverse */
109 0, /* tp_clear */
110 0, /* tp_richcompare */
111 0, /* tp_weaklistoffset */
112 0, /* tp_iter */
113 0, /* tp_iternext */
114 0, /* tp_methods */
115 0, /* tp_members */
116 0, /* tp_getset */
117 DEFERRED_ADDRESS(&DBusPyIntBase_Type), /* tp_base */
118 0, /* tp_dict */
119 0, /* tp_descr_get */
120 0, /* tp_descr_set */
121 0, /* tp_dictoffset */
122 0, /* tp_init */
123 0, /* tp_alloc */
124 Boolean_tp_new, /* tp_new */
127 /* Int16 ============================================================ */
129 PyDoc_STRVAR(Int16_tp_doc,
130 "A signed 16-bit integer between -0x8000 and +0x7FFF, represented as\n"
131 "a subtype of `int`.\n"
132 "\n"
133 "Constructor::\n"
134 "\n"
135 " dbus.Int16(value: int[, variant_level: int]) -> Int16\n"
136 "\n"
137 "value must be within the allowed range, or OverflowError will be\n"
138 "raised.\n"
139 "\n"
140 " variant_level must be non-negative; the default is 0.\n"
141 "\n"
142 ":IVariables:\n"
143 " `variant_level` : int\n"
144 " Indicates how many nested Variant containers this object\n"
145 " is contained in: if a message's wire format has a variant containing a\n"
146 " variant containing an int16, this is represented in Python by an\n"
147 " Int16 with variant_level==2.\n"
150 dbus_int16_t
151 dbus_py_int16_range_check(PyObject *obj)
153 long i = PyInt_AsLong (obj);
154 if (i == -1 && PyErr_Occurred ()) return -1;
155 if (i < -0x8000 || i > 0x7fff) {
156 PyErr_Format(PyExc_OverflowError, "Value %d out of range for Int16",
157 (int)i);
158 return -1;
160 return i;
163 static PyObject *
164 Int16_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
166 PyObject *self = (DBusPyIntBase_Type.tp_new)(cls, args, kwargs);
167 if (self && dbus_py_int16_range_check(self) == -1 && PyErr_Occurred()) {
168 Py_DECREF(self);
169 return NULL;
171 return self;
174 PyTypeObject DBusPyInt16_Type = {
175 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
177 "dbus.Int16",
180 0, /* tp_dealloc */
181 0, /* tp_print */
182 0, /* tp_getattr */
183 0, /* tp_setattr */
184 0, /* tp_compare */
185 0, /* tp_repr */
186 0, /* tp_as_number */
187 0, /* tp_as_sequence */
188 0, /* tp_as_mapping */
189 0, /* tp_hash */
190 0, /* tp_call */
191 0, /* tp_str */
192 0, /* tp_getattro */
193 0, /* tp_setattro */
194 0, /* tp_as_buffer */
195 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
196 Int16_tp_doc, /* tp_doc */
197 0, /* tp_traverse */
198 0, /* tp_clear */
199 0, /* tp_richcompare */
200 0, /* tp_weaklistoffset */
201 0, /* tp_iter */
202 0, /* tp_iternext */
203 0, /* tp_methods */
204 0, /* tp_members */
205 0, /* tp_getset */
206 DEFERRED_ADDRESS(&DBusPyIntBase_Type), /* tp_base */
207 0, /* tp_dict */
208 0, /* tp_descr_get */
209 0, /* tp_descr_set */
210 0, /* tp_dictoffset */
211 0, /* tp_init */
212 0, /* tp_alloc */
213 Int16_tp_new, /* tp_new */
216 /* UInt16 =========================================================== */
218 PyDoc_STRVAR(UInt16_tp_doc,
219 "An unsigned 16-bit integer between 0 and 0xFFFF, represented as\n"
220 "a subtype of `int`.\n"
221 "\n"
222 "Constructor::\n"
223 "\n"
224 " dbus.UInt16(value: int[, variant_level: int]) -> UInt16\n"
225 "\n"
226 "``value`` must be within the allowed range, or `OverflowError` will be\n"
227 "raised.\n"
228 "\n"
229 "``variant_level`` must be non-negative; the default is 0.\n"
230 "\n"
231 ":IVariables:\n"
232 " `variant_level` : int\n"
233 " Indicates how many nested Variant containers this object\n"
234 " is contained in: if a message's wire format has a variant containing a\n"
235 " variant containing a uint16, this is represented in Python by a\n"
236 " UInt16 with variant_level==2.\n"
239 dbus_uint16_t
240 dbus_py_uint16_range_check(PyObject *obj)
242 long i = PyInt_AsLong(obj);
243 if (i == -1 && PyErr_Occurred()) return (dbus_uint16_t)(-1);
244 if (i < 0 || i > 0xffff) {
245 PyErr_Format(PyExc_OverflowError, "Value %d out of range for UInt16",
246 (int)i);
247 return (dbus_uint16_t)(-1);
249 return i;
252 static PyObject *
253 UInt16_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
255 PyObject *self = (DBusPyIntBase_Type.tp_new)(cls, args, kwargs);
256 if (self && dbus_py_uint16_range_check(self) == (dbus_uint16_t)(-1)
257 && PyErr_Occurred()) {
258 Py_DECREF (self);
259 return NULL;
261 return self;
264 PyTypeObject DBusPyUInt16_Type = {
265 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
267 "dbus.UInt16",
270 0, /* tp_dealloc */
271 0, /* tp_print */
272 0, /* tp_getattr */
273 0, /* tp_setattr */
274 0, /* tp_compare */
275 0, /* tp_repr */
276 0, /* tp_as_number */
277 0, /* tp_as_sequence */
278 0, /* tp_as_mapping */
279 0, /* tp_hash */
280 0, /* tp_call */
281 0, /* tp_str */
282 0, /* tp_getattro */
283 0, /* tp_setattro */
284 0, /* tp_as_buffer */
285 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
286 UInt16_tp_doc, /* tp_doc */
287 0, /* tp_traverse */
288 0, /* tp_clear */
289 0, /* tp_richcompare */
290 0, /* tp_weaklistoffset */
291 0, /* tp_iter */
292 0, /* tp_iternext */
293 0, /* tp_methods */
294 0, /* tp_members */
295 0, /* tp_getset */
296 DEFERRED_ADDRESS(&DBusPyIntBase_Type), /* tp_base */
297 0, /* tp_dict */
298 0, /* tp_descr_get */
299 0, /* tp_descr_set */
300 0, /* tp_dictoffset */
301 0, /* tp_init */
302 0, /* tp_alloc */
303 UInt16_tp_new, /* tp_new */
306 /* Int32 ============================================================ */
308 PyDoc_STRVAR(Int32_tp_doc,
309 "A signed 32-bit integer between -0x8000 0000 and +0x7FFF FFFF, represented as\n"
310 "a subtype of `int`.\n"
311 "\n"
312 "Constructor::\n"
313 "\n"
314 " dbus.Int32(value: int[, variant_level: int]) -> Int32\n"
315 "\n"
316 "``value`` must be within the allowed range, or `OverflowError` will be\n"
317 "raised.\n"
318 "\n"
319 "``variant_level`` must be non-negative; the default is 0.\n"
320 "\n"
321 ":IVariables:\n"
322 " `variant_level` : int\n"
323 " Indicates how many nested Variant containers this object\n"
324 " is contained in: if a message's wire format has a variant containing a\n"
325 " variant containing an int32, this is represented in Python by an\n"
326 " Int32 with variant_level==2.\n"
329 dbus_int32_t
330 dbus_py_int32_range_check(PyObject *obj)
332 long i = PyInt_AsLong(obj);
333 if (i == -1 && PyErr_Occurred()) return -1;
334 if (i < INT32_MIN || i > INT32_MAX) {
335 PyErr_Format(PyExc_OverflowError, "Value %d out of range for Int32",
336 (int)i);
337 return -1;
339 return i;
342 static PyObject *
343 Int32_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
345 PyObject *self = (DBusPyIntBase_Type.tp_new)(cls, args, kwargs);
346 if (self && dbus_py_int32_range_check(self) == -1 && PyErr_Occurred()) {
347 Py_DECREF(self);
348 return NULL;
350 return self;
353 PyTypeObject DBusPyInt32_Type = {
354 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
356 "dbus.Int32",
359 0, /* tp_dealloc */
360 0, /* tp_print */
361 0, /* tp_getattr */
362 0, /* tp_setattr */
363 0, /* tp_compare */
364 0, /* tp_repr */
365 0, /* tp_as_number */
366 0, /* tp_as_sequence */
367 0, /* tp_as_mapping */
368 0, /* tp_hash */
369 0, /* tp_call */
370 0, /* tp_str */
371 0, /* tp_getattro */
372 0, /* tp_setattro */
373 0, /* tp_as_buffer */
374 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
375 Int32_tp_doc, /* tp_doc */
376 0, /* tp_traverse */
377 0, /* tp_clear */
378 0, /* tp_richcompare */
379 0, /* tp_weaklistoffset */
380 0, /* tp_iter */
381 0, /* tp_iternext */
382 0, /* tp_methods */
383 0, /* tp_members */
384 0, /* tp_getset */
385 DEFERRED_ADDRESS(&DBusPyIntBase_Type), /* tp_base */
386 0, /* tp_dict */
387 0, /* tp_descr_get */
388 0, /* tp_descr_set */
389 0, /* tp_dictoffset */
390 0, /* tp_init */
391 0, /* tp_alloc */
392 Int32_tp_new, /* tp_new */
395 /* UInt32 =========================================================== */
397 PyDoc_STRVAR(UInt32_tp_doc,
398 "An unsigned 32-bit integer between 0 and 0xFFFF FFFF, represented as a\n"
399 "subtype of `long`.\n"
400 "\n"
401 "Note that this may be changed in future to be a subtype of `int` on\n"
402 "64-bit platforms; applications should not rely on either behaviour.\n"
403 "\n"
404 "Constructor::\n"
405 "\n"
406 " dbus.UInt32(value: long[, variant_level: int]) -> UInt32\n"
407 "\n"
408 "``value`` must be within the allowed range, or `OverflowError` will be\n"
409 "raised.\n"
410 "\n"
411 "``variant_level`` must be non-negative; the default is 0.\n"
412 "\n"
413 ":IVariables:\n"
414 " `variant_level` : int\n"
415 " Indicates how many nested Variant containers this object\n"
416 " is contained in: if a message's wire format has a variant containing a\n"
417 " variant containing a uint32, this is represented in Python by a\n"
418 " UInt32 with variant_level==2.\n"
421 dbus_uint32_t
422 dbus_py_uint32_range_check(PyObject *obj)
424 unsigned long i;
425 PyObject *long_obj = PyNumber_Long(obj);
427 if (!long_obj) return (dbus_uint32_t)(-1);
428 i = PyLong_AsUnsignedLong(long_obj);
429 if (i == (unsigned long)(-1) && PyErr_Occurred()) {
430 Py_DECREF(long_obj);
431 return (dbus_uint32_t)(-1);
433 if (i > UINT32_MAX) {
434 PyErr_Format(PyExc_OverflowError, "Value %d out of range for UInt32",
435 (int)i);
436 Py_DECREF(long_obj);
437 return (dbus_uint32_t)(-1);
439 Py_DECREF(long_obj);
440 return i;
443 static PyObject *
444 UInt32_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
446 PyObject *self = (DBusPyLongBase_Type.tp_new)(cls, args, kwargs);
447 if (self && dbus_py_uint32_range_check(self) == (dbus_uint32_t)(-1)
448 && PyErr_Occurred()) {
449 Py_DECREF(self);
450 return NULL;
452 return self;
455 PyTypeObject DBusPyUInt32_Type = {
456 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
458 "dbus.UInt32",
461 0, /* tp_dealloc */
462 0, /* tp_print */
463 0, /* tp_getattr */
464 0, /* tp_setattr */
465 0, /* tp_compare */
466 0, /* tp_repr */
467 0, /* tp_as_number */
468 0, /* tp_as_sequence */
469 0, /* tp_as_mapping */
470 0, /* tp_hash */
471 0, /* tp_call */
472 0, /* tp_str */
473 0, /* tp_getattro */
474 0, /* tp_setattro */
475 0, /* tp_as_buffer */
476 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
477 UInt32_tp_doc, /* tp_doc */
478 0, /* tp_traverse */
479 0, /* tp_clear */
480 0, /* tp_richcompare */
481 0, /* tp_weaklistoffset */
482 0, /* tp_iter */
483 0, /* tp_iternext */
484 0, /* tp_methods */
485 0, /* tp_members */
486 0, /* tp_getset */
487 DEFERRED_ADDRESS(&DBusPyLongBase_Type), /* tp_base */
488 0, /* tp_dict */
489 0, /* tp_descr_get */
490 0, /* tp_descr_set */
491 0, /* tp_dictoffset */
492 0, /* tp_init */
493 0, /* tp_alloc */
494 UInt32_tp_new, /* tp_new */
497 /* Int64 =========================================================== */
499 PyDoc_STRVAR(Int64_tp_doc,
500 "A signed 64-bit integer between -0x8000 0000 0000 0000 and\n"
501 "+0x7FFF FFFF FFFF FFFF, represented as a subtype of `long`.\n"
502 "\n"
503 "Note that this may be changed in future to be a subtype of `int` on\n"
504 "64-bit platforms; applications should not rely on either behaviour.\n"
505 "\n"
506 "This type only works on platforms where the C compiler has suitable\n"
507 "64-bit types, such as C99 ``long long``.\n"
508 #ifdef DBUS_PYTHON_64_BIT_WORKS
509 "This is the case on your current platform.\n"
510 #else /* !defined(DBUS_PYTHON_64_BIT_WORKS) */
511 "This is not the case on your current platform, so this type's\n"
512 "constructor will always raise NotImplementedError. Try a better\n"
513 "compiler, if one is available.\n"
514 #endif /* !defined(DBUS_PYTHON_64_BIT_WORKS) */
515 "\n"
516 "Constructor::\n"
517 "\n"
518 " dbus.Int64(value: long[, variant_level: int]) -> Int64\n"
519 "\n"
520 "``value`` must be within the allowed range, or `OverflowError` will be\n"
521 "raised.\n"
522 "\n"
523 "``variant_level`` must be non-negative; the default is 0.\n"
524 "\n"
525 ":IVariables:\n"
526 " `variant_level` : int\n"
527 " Indicates how many nested Variant containers this object\n"
528 " is contained in: if a message's wire format has a variant containing a\n"
529 " variant containing an int64, this is represented in Python by an\n"
530 " Int64 with variant_level==2.\n"
533 #ifdef DBUS_PYTHON_64_BIT_WORKS
534 dbus_int64_t
535 dbus_py_int64_range_check(PyObject *obj)
537 PY_LONG_LONG i;
538 PyObject *long_obj = PyNumber_Long(obj);
540 if (!long_obj) return -1;
541 i = PyLong_AsLongLong(long_obj);
542 if (i == -1 && PyErr_Occurred()) {
543 Py_DECREF(long_obj);
544 return -1;
546 if (i < INT64_MIN || i > INT64_MAX) {
547 PyErr_SetString(PyExc_OverflowError, "Value out of range for Int64");
548 Py_DECREF(long_obj);
549 return -1;
551 Py_DECREF(long_obj);
552 return i;
554 #endif
556 static PyObject *
557 Int64_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
559 #ifdef DBUS_PYTHON_64_BIT_WORKS
560 PyObject *self = (DBusPyLongBase_Type.tp_new)(cls, args, kwargs);
561 if (self && dbus_py_int64_range_check(self) == -1 && PyErr_Occurred()) {
562 Py_DECREF(self);
563 return NULL;
565 return self;
566 #else
567 PyErr_SetString(PyExc_NotImplementedError,
568 "64-bit types are not available on this platform");
569 return NULL;
570 #endif
573 PyTypeObject DBusPyInt64_Type = {
574 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
576 "dbus.Int64",
579 0, /* tp_dealloc */
580 0, /* tp_print */
581 0, /* tp_getattr */
582 0, /* tp_setattr */
583 0, /* tp_compare */
584 0, /* tp_repr */
585 0, /* tp_as_number */
586 0, /* tp_as_sequence */
587 0, /* tp_as_mapping */
588 0, /* tp_hash */
589 0, /* tp_call */
590 0, /* tp_str */
591 0, /* tp_getattro */
592 0, /* tp_setattro */
593 0, /* tp_as_buffer */
594 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
595 Int64_tp_doc, /* tp_doc */
596 0, /* tp_traverse */
597 0, /* tp_clear */
598 0, /* tp_richcompare */
599 0, /* tp_weaklistoffset */
600 0, /* tp_iter */
601 0, /* tp_iternext */
602 0, /* tp_methods */
603 0, /* tp_members */
604 0, /* tp_getset */
605 DEFERRED_ADDRESS(&DBusPyLongBase_Type), /* tp_base */
606 0, /* tp_dict */
607 0, /* tp_descr_get */
608 0, /* tp_descr_set */
609 0, /* tp_dictoffset */
610 0, /* tp_init */
611 0, /* tp_alloc */
612 Int64_tp_new, /* tp_new */
615 /* UInt64 =========================================================== */
617 PyDoc_STRVAR(UInt64_tp_doc,
618 "An unsigned 64-bit integer between 0 and 0xFFFF FFFF FFFF FFFF,\n"
619 "represented as a subtype of `long`.\n"
620 "\n"
621 "This type only exists on platforms where the C compiler has suitable\n"
622 "64-bit types, such as C99 ``unsigned long long``.\n"
623 #ifdef DBUS_PYTHON_64_BIT_WORKS
624 "This is the case on your current platform.\n"
625 #else /* !defined(DBUS_PYTHON_64_BIT_WORKS) */
626 "This is not the case on your current platform, so this type's\n"
627 "constructor will always raise NotImplementedError. Try a better\n"
628 "compiler, if one is available.\n"
629 #endif /* !defined(DBUS_PYTHON_64_BIT_WORKS) */
630 "\n"
631 "Constructor::\n"
632 "\n"
633 " dbus.UInt64(value: long[, variant_level: int]) -> UInt64\n"
634 "\n"
635 "``value`` must be within the allowed range, or `OverflowError` will be\n"
636 "raised.\n"
637 "\n"
638 "``variant_level`` must be non-negative; the default is 0.\n"
639 "\n"
640 ":IVariables:\n"
641 " `variant_level` : int\n"
642 " Indicates how many nested Variant containers this object\n"
643 " is contained in: if a message's wire format has a variant containing a\n"
644 " variant containing a uint64, this is represented in Python by a\n"
645 " UInt64 with variant_level==2.\n"
648 dbus_uint64_t
649 dbus_py_uint64_range_check(PyObject *obj)
651 unsigned PY_LONG_LONG i;
652 PyObject *long_obj = PyNumber_Long(obj);
654 if (!long_obj) return (dbus_uint64_t)(-1);
655 i = PyLong_AsUnsignedLongLong(long_obj);
656 if (i == (unsigned PY_LONG_LONG)(-1) && PyErr_Occurred()) {
657 Py_DECREF(long_obj);
658 return (dbus_uint64_t)(-1);
660 if (i > UINT64_MAX) {
661 PyErr_SetString(PyExc_OverflowError, "Value out of range for UInt64");
662 Py_DECREF(long_obj);
663 return (dbus_uint64_t)(-1);
665 Py_DECREF(long_obj);
666 return i;
669 static PyObject *
670 UInt64_tp_new (PyTypeObject *cls, PyObject *args, PyObject *kwargs)
672 #ifdef DBUS_PYTHON_64_BIT_WORKS
673 PyObject *self = (DBusPyLongBase_Type.tp_new)(cls, args, kwargs);
674 if (self && dbus_py_uint64_range_check(self) == (dbus_uint64_t)(-1)
675 && PyErr_Occurred()) {
676 Py_DECREF(self);
677 return NULL;
679 return self;
680 #else
681 PyErr_SetString(PyExc_NotImplementedError,
682 "64-bit integer types are not supported on this platform");
683 return NULL;
684 #endif
687 PyTypeObject DBusPyUInt64_Type = {
688 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
690 "dbus.UInt64",
693 0, /* tp_dealloc */
694 0, /* tp_print */
695 0, /* tp_getattr */
696 0, /* tp_setattr */
697 0, /* tp_compare */
698 0, /* tp_repr */
699 0, /* tp_as_number */
700 0, /* tp_as_sequence */
701 0, /* tp_as_mapping */
702 0, /* tp_hash */
703 0, /* tp_call */
704 0, /* tp_str */
705 0, /* tp_getattro */
706 0, /* tp_setattro */
707 0, /* tp_as_buffer */
708 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
709 UInt64_tp_doc, /* tp_doc */
710 0, /* tp_traverse */
711 0, /* tp_clear */
712 0, /* tp_richcompare */
713 0, /* tp_weaklistoffset */
714 0, /* tp_iter */
715 0, /* tp_iternext */
716 0, /* tp_methods */
717 0, /* tp_members */
718 0, /* tp_getset */
719 DEFERRED_ADDRESS(&DBusPyLongBase_Type), /* tp_base */
720 0, /* tp_dict */
721 0, /* tp_descr_get */
722 0, /* tp_descr_set */
723 0, /* tp_dictoffset */
724 0, /* tp_init */
725 0, /* tp_alloc */
726 UInt64_tp_new, /* tp_new */
729 dbus_bool_t
730 dbus_py_init_int_types(void)
732 DBusPyInt16_Type.tp_base = &DBusPyIntBase_Type;
733 if (PyType_Ready(&DBusPyInt16_Type) < 0) return 0;
734 /* disable the tp_print copied from PyInt_Type, so tp_repr gets called as
735 desired */
736 DBusPyInt16_Type.tp_print = NULL;
738 DBusPyUInt16_Type.tp_base = &DBusPyIntBase_Type;
739 if (PyType_Ready(&DBusPyUInt16_Type) < 0) return 0;
740 DBusPyUInt16_Type.tp_print = NULL;
742 DBusPyInt32_Type.tp_base = &DBusPyIntBase_Type;
743 if (PyType_Ready(&DBusPyInt32_Type) < 0) return 0;
744 DBusPyInt32_Type.tp_print = NULL;
746 DBusPyUInt32_Type.tp_base = &DBusPyLongBase_Type;
747 if (PyType_Ready(&DBusPyUInt32_Type) < 0) return 0;
748 DBusPyUInt32_Type.tp_print = NULL;
750 #if defined(DBUS_HAVE_INT64) && defined(HAVE_LONG_LONG)
751 DBusPyInt64_Type.tp_base = &DBusPyLongBase_Type;
752 if (PyType_Ready(&DBusPyInt64_Type) < 0) return 0;
753 DBusPyInt64_Type.tp_print = NULL;
755 DBusPyUInt64_Type.tp_base = &DBusPyLongBase_Type;
756 if (PyType_Ready(&DBusPyUInt64_Type) < 0) return 0;
757 DBusPyUInt64_Type.tp_print = NULL;
758 #endif
759 return 1;
762 dbus_bool_t
763 dbus_py_insert_int_types(PyObject *this_module)
765 Py_INCREF(&DBusPyInt16_Type);
766 Py_INCREF(&DBusPyUInt16_Type);
767 Py_INCREF(&DBusPyInt32_Type);
768 Py_INCREF(&DBusPyUInt32_Type);
769 Py_INCREF(&DBusPyInt64_Type);
770 Py_INCREF(&DBusPyUInt64_Type);
771 Py_INCREF(&DBusPyBoolean_Type);
772 if (PyModule_AddObject(this_module, "Int16",
773 (PyObject *)&DBusPyInt16_Type) < 0) return 0;
774 if (PyModule_AddObject(this_module, "UInt16",
775 (PyObject *)&DBusPyUInt16_Type) < 0) return 0;
776 if (PyModule_AddObject(this_module, "Int32",
777 (PyObject *)&DBusPyInt32_Type) < 0) return 0;
778 if (PyModule_AddObject(this_module, "UInt32",
779 (PyObject *)&DBusPyUInt32_Type) < 0) return 0;
780 if (PyModule_AddObject(this_module, "Int64",
781 (PyObject *)&DBusPyInt64_Type) < 0) return 0;
782 if (PyModule_AddObject(this_module, "UInt64",
783 (PyObject *)&DBusPyUInt64_Type) < 0) return 0;
784 if (PyModule_AddObject(this_module, "Boolean",
785 (PyObject *)&DBusPyBoolean_Type) < 0) return 0;
787 return 1;
790 /* vim:set ft=c cino< sw=4 sts=4 et: */