summaryrefslogtreecommitdiff
path: root/dbus_bindings/int.c
blob: 109d4c7a1e3f5871d5ebdf7ce9b587d24e9114b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/* Simple D-Bus types: integers of various sizes, and ObjectPath.
 *
 * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "dbus_bindings-internal.h"

#include "types-internal.h"

#ifdef PY3
#define INTBASE (DBusPyLongBase_Type)
#define LONG_TYPE_NAME "int"
#else
#define INTBASE (DBusPyIntBase_Type)
#define LONG_TYPE_NAME "long"
#endif

/* Specific types =================================================== */

/* Boolean, a subclass of DBusPythonInt ============================= */

PyDoc_STRVAR(Boolean_tp_doc,
"dbus.Boolean(value: bool[, variant_level: int])\n"
"\n"
"A boolean, represented as a subtype of ``int`` (not ``bool``, because ``bool``\n"
"cannot be subclassed).\n"
"\n"
"``value`` is converted to 0 or 1 as if by ``int(bool(value))``.\n"
"\n"
":py:attr:`variant_level` must be non-negative; the default is 0.\n"
"\n"
".. py:attribute:: variant_level\n"
"\n"
"    Indicates how many nested Variant containers this object\n"
"    is contained in: if a message's wire format has a variant containing a\n"
"    variant containing a boolean, this is represented in Python by a\n"
"    Boolean with variant_level==2.\n"
);

static PyObject *
Boolean_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{
    PyObject *tuple, *self, *value = Py_None;
    long variantness = 0;
    static char *argnames[] = {"_", "variant_level", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ol:__new__", argnames,
                                     &value, &variantness)) return NULL;
    if (variantness < 0) {
        PyErr_SetString(PyExc_ValueError,
                        "variant_level must be non-negative");
        return NULL;
    }
    tuple = Py_BuildValue("(i)", PyObject_IsTrue(value) ? 1 : 0);
    if (!tuple) return NULL;
    self = (INTBASE.tp_new)(cls, tuple, kwargs);
    Py_CLEAR(tuple);
    return self;
}

static PyObject *
Boolean_tp_repr(PyObject *self)
{
    int is_true = PyObject_IsTrue(self);
#ifdef PY3
    long variant_level = dbus_py_variant_level_get(self);
    if (variant_level < 0)
        return NULL;
#else
    long variant_level = ((DBusPyIntBase *)self)->variant_level;
#endif

    if (is_true == -1)
        return NULL;

    if (variant_level > 0) {
        return PyUnicode_FromFormat("%s(%s, variant_level=%ld)",
                                    Py_TYPE(self)->tp_name,
                                    is_true ? "True" : "False",
                                    variant_level);
    }
    return PyUnicode_FromFormat("%s(%s)",
                                Py_TYPE(self)->tp_name,
                                is_true ? "True" : "False");
}

PyTypeObject DBusPyBoolean_Type = {
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
    "dbus.Boolean",
    0,
    0,
    0,                                      /* tp_dealloc */
    0,                                      /* tp_print */
    0,                                      /* tp_getattr */
    0,                                      /* tp_setattr */
    0,                                      /* tp_compare */
    Boolean_tp_repr,                        /* tp_repr */
    0,                                      /* tp_as_number */
    0,                                      /* tp_as_sequence */
    0,                                      /* tp_as_mapping */
    0,                                      /* tp_hash */
    0,                                      /* tp_call */
    0,                                      /* tp_str */
    0,                                      /* tp_getattro */
    0,                                      /* tp_setattro */
    0,                                      /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
    Boolean_tp_doc,                         /* tp_doc */
    0,                                      /* tp_traverse */
    0,                                      /* tp_clear */
    0,                                      /* tp_richcompare */
    0,                                      /* tp_weaklistoffset */
    0,                                      /* tp_iter */
    0,                                      /* tp_iternext */
    0,                                      /* tp_methods */
    0,                                      /* tp_members */
    0,                                      /* tp_getset */
    DEFERRED_ADDRESS(&INTBASE),             /* tp_base */
    0,                                      /* tp_dict */
    0,                                      /* tp_descr_get */
    0,                                      /* tp_descr_set */
    0,                                      /* tp_dictoffset */
    0,                                      /* tp_init */
    0,                                      /* tp_alloc */
    Boolean_tp_new,                         /* tp_new */
};

/* Int16 ============================================================ */

PyDoc_STRVAR(Int16_tp_doc,
"dbus.Int16(value: int[, variant_level: int])\n"
"\n"
"A signed 16-bit integer between -0x8000 and +0x7FFF, represented as\n"
"a subtype of `int`.\n"
"\n"
"value must be within the allowed range, or OverflowError will be\n"
"raised.\n"
"\n"
"    variant_level must be non-negative; the default is 0.\n"
"\n"
".. py:attribute:: variant_level\n"
"\n"
"    Indicates how many nested Variant containers this object\n"
"    is contained in: if a message's wire format has a variant containing a\n"
"    variant containing an int16, this is represented in Python by an\n"
"    Int16 with variant_level==2.\n"
);

dbus_int16_t
dbus_py_int16_range_check(PyObject *obj)
{
    long i = PyLong_AsLong(obj);
    if (i == -1 && PyErr_Occurred())
        return -1;

    if (i < -0x8000 || i > 0x7fff) {
        PyErr_Format(PyExc_OverflowError, "Value %d out of range for Int16",
                     (int)i);
        return -1;
    }
    return (dbus_int16_t)i;
}

static PyObject *
Int16_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{
    PyObject *self = (INTBASE.tp_new)(cls, args, kwargs);
    if (self && dbus_py_int16_range_check(self) == -1 && PyErr_Occurred()) {
        Py_CLEAR(self);
        return NULL;
    }
    return self;
}

PyTypeObject DBusPyInt16_Type = {
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
    "dbus.Int16",
    0,
    0,
    0,                                      /* tp_dealloc */
    0,                                      /* tp_print */
    0,                                      /* tp_getattr */
    0,                                      /* tp_setattr */
    0,                                      /* tp_compare */
    0,                                      /* tp_repr */
    0,                                      /* tp_as_number */
    0,                                      /* tp_as_sequence */
    0,                                      /* tp_as_mapping */
    0,                                      /* tp_hash */
    0,                                      /* tp_call */
    0,                                      /* tp_str */
    0,                                      /* tp_getattro */
    0,                                      /* tp_setattro */
    0,                                      /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
    Int16_tp_doc,                           /* tp_doc */
    0,                                      /* tp_traverse */
    0,                                      /* tp_clear */
    0,                                      /* tp_richcompare */
    0,                                      /* tp_weaklistoffset */
    0,                                      /* tp_iter */
    0,                                      /* tp_iternext */
    0,                                      /* tp_methods */
    0,                                      /* tp_members */
    0,                                      /* tp_getset */
    DEFERRED_ADDRESS(&INTBASE),             /* tp_base */
    0,                                      /* tp_dict */
    0,                                      /* tp_descr_get */
    0,                                      /* tp_descr_set */
    0,                                      /* tp_dictoffset */
    0,                                      /* tp_init */
    0,                                      /* tp_alloc */
    Int16_tp_new,                           /* tp_new */
};

/* UInt16 =========================================================== */

PyDoc_STRVAR(UInt16_tp_doc,
"dbus.UInt16(value: int[, variant_level: int])\n"
"\n"
"An unsigned 16-bit integer between 0 and 0xFFFF, represented as\n"
"a subtype of ``int``.\n"
"\n"
"``value`` must be within the allowed range, or `OverflowError` will be\n"
"raised.\n"
"\n"
":py:attr:`variant_level` must be non-negative; the default is 0.\n"
"\n"
".. py:attribute:: variant_level\n"
"\n"
"    Indicates how many nested Variant containers this object\n"
"    is contained in: if a message's wire format has a variant containing a\n"
"    variant containing a uint16, this is represented in Python by a\n"
"    UInt16 with variant_level==2.\n"
);

dbus_uint16_t
dbus_py_uint16_range_check(PyObject *obj)
{
    long i = PyLong_AsLong(obj);
    if (i == -1 && PyErr_Occurred())
        return (dbus_uint16_t)(-1);

    if (i < 0 || i > 0xffff) {
        PyErr_Format(PyExc_OverflowError, "Value %d out of range for UInt16",
                     (int)i);
        return (dbus_uint16_t)(-1);
    }
    return (dbus_uint16_t)i;
}

static PyObject *
UInt16_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{
    PyObject *self = (INTBASE.tp_new)(cls, args, kwargs);
    if (self && dbus_py_uint16_range_check(self) == (dbus_uint16_t)(-1)
        && PyErr_Occurred())
    {
        Py_CLEAR (self);
        return NULL;
    }
    return self;
}

PyTypeObject DBusPyUInt16_Type = {
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
    "dbus.UInt16",
    0,
    0,
    0,                                      /* tp_dealloc */
    0,                                      /* tp_print */
    0,                                      /* tp_getattr */
    0,                                      /* tp_setattr */
    0,                                      /* tp_compare */
    0,                                      /* tp_repr */
    0,                                      /* tp_as_number */
    0,                                      /* tp_as_sequence */
    0,                                      /* tp_as_mapping */
    0,                                      /* tp_hash */
    0,                                      /* tp_call */
    0,                                      /* tp_str */
    0,                                      /* tp_getattro */
    0,                                      /* tp_setattro */
    0,                                      /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
    UInt16_tp_doc,                          /* tp_doc */
    0,                                      /* tp_traverse */
    0,                                      /* tp_clear */
    0,                                      /* tp_richcompare */
    0,                                      /* tp_weaklistoffset */
    0,                                      /* tp_iter */
    0,                                      /* tp_iternext */
    0,                                      /* tp_methods */
    0,                                      /* tp_members */
    0,                                      /* tp_getset */
    DEFERRED_ADDRESS(&INTBASE),             /* tp_base */
    0,                                      /* tp_dict */
    0,                                      /* tp_descr_get */
    0,                                      /* tp_descr_set */
    0,                                      /* tp_dictoffset */
    0,                                      /* tp_init */
    0,                                      /* tp_alloc */
    UInt16_tp_new,                          /* tp_new */
};

/* Int32 ============================================================ */

PyDoc_STRVAR(Int32_tp_doc,
"dbus.Int32(value: int[, variant_level: int])\n"
"\n"
"A signed 32-bit integer between -0x8000 0000 and +0x7FFF FFFF, represented as\n"
"a subtype of ``int``.\n"
"\n"
"``value`` must be within the allowed range, or `OverflowError` will be\n"
"raised.\n"
"\n"
":py:attr:`variant_level` must be non-negative; the default is 0.\n"
"\n"
".. py:attribute:: variant_level\n"
"\n"
"    Indicates how many nested Variant containers this object\n"
"    is contained in: if a message's wire format has a variant containing a\n"
"    variant containing an int32, this is represented in Python by an\n"
"    Int32 with variant_level==2.\n"
);

dbus_int32_t
dbus_py_int32_range_check(PyObject *obj)
{
    long i = PyLong_AsLong(obj);
    if (i == -1 && PyErr_Occurred())
        return -1;

    if (i < INT32_MIN || i > INT32_MAX) {
        PyErr_Format(PyExc_OverflowError, "Value %d out of range for Int32",
                     (int)i);
        return -1;
    }
    return (dbus_int32_t)i;
}

static PyObject *
Int32_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{
    PyObject *self = (INTBASE.tp_new)(cls, args, kwargs);
    if (self && dbus_py_int32_range_check(self) == -1 && PyErr_Occurred()) {
        Py_CLEAR(self);
        return NULL;
    }
    return self;
}

PyTypeObject DBusPyInt32_Type = {
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
    "dbus.Int32",
    0,
    0,
    0,                                      /* tp_dealloc */
    0,                                      /* tp_print */
    0,                                      /* tp_getattr */
    0,                                      /* tp_setattr */
    0,                                      /* tp_compare */
    0,                                      /* tp_repr */
    0,                                      /* tp_as_number */
    0,                                      /* tp_as_sequence */
    0,                                      /* tp_as_mapping */
    0,                                      /* tp_hash */
    0,                                      /* tp_call */
    0,                                      /* tp_str */
    0,                                      /* tp_getattro */
    0,                                      /* tp_setattro */
    0,                                      /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
    Int32_tp_doc,                           /* tp_doc */
    0,                                      /* tp_traverse */
    0,                                      /* tp_clear */
    0,                                      /* tp_richcompare */
    0,                                      /* tp_weaklistoffset */
    0,                                      /* tp_iter */
    0,                                      /* tp_iternext */
    0,                                      /* tp_methods */
    0,                                      /* tp_members */
    0,                                      /* tp_getset */
    DEFERRED_ADDRESS(&INTBASE),             /* tp_base */
    0,                                      /* tp_dict */
    0,                                      /* tp_descr_get */
    0,                                      /* tp_descr_set */
    0,                                      /* tp_dictoffset */
    0,                                      /* tp_init */
    0,                                      /* tp_alloc */
    Int32_tp_new,                           /* tp_new */
};

/* UInt32 =========================================================== */

PyDoc_STRVAR(UInt32_tp_doc,
"dbus.UInt32(value: " LONG_TYPE_NAME "[, variant_level: int])\n"
"\n"
"An unsigned 32-bit integer between 0 and 0xFFFF FFFF, represented as a\n"
"subtype of ``long`` in Python 2 or ``int`` in Python 3.\n"
"\n"
"``value`` must be within the allowed range, or `OverflowError` will be\n"
"raised.\n"
"\n"
":py:attr:`variant_level` must be non-negative; the default is 0.\n"
"\n"
".. py:attribute:: variant_level\n"
"\n"
"    Indicates how many nested Variant containers this object\n"
"    is contained in: if a message's wire format has a variant containing a\n"
"    variant containing a uint32, this is represented in Python by a\n"
"    UInt32 with variant_level==2.\n"
);

dbus_uint32_t
dbus_py_uint32_range_check(PyObject *obj)
{
    unsigned long i;
    PyObject *long_obj = PyNumber_Long(obj);

    if (!long_obj) return (dbus_uint32_t)(-1);
    i = PyLong_AsUnsignedLong(long_obj);
    if (i == (unsigned long)(-1) && PyErr_Occurred()) {
        Py_CLEAR(long_obj);
        return (dbus_uint32_t)(-1);
    }
    if (i > UINT32_MAX) {
        PyErr_Format(PyExc_OverflowError, "Value %d out of range for UInt32",
                     (int)i);
        Py_CLEAR(long_obj);
        return (dbus_uint32_t)(-1);
    }
    Py_CLEAR(long_obj);
    return i;
}

static PyObject *
UInt32_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{
    PyObject *self = (DBusPyLongBase_Type.tp_new)(cls, args, kwargs);
    if (self && dbus_py_uint32_range_check(self) == (dbus_uint32_t)(-1)
        && PyErr_Occurred()) {
        Py_CLEAR(self);
        return NULL;
    }
    return self;
}

PyTypeObject DBusPyUInt32_Type = {
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
    "dbus.UInt32",
    0,
    0,
    0,                                      /* tp_dealloc */
    0,                                      /* tp_print */
    0,                                      /* tp_getattr */
    0,                                      /* tp_setattr */
    0,                                      /* tp_compare */
    0,                                      /* tp_repr */
    0,                                      /* tp_as_number */
    0,                                      /* tp_as_sequence */
    0,                                      /* tp_as_mapping */
    0,                                      /* tp_hash */
    0,                                      /* tp_call */
    0,                                      /* tp_str */
    0,                                      /* tp_getattro */
    0,                                      /* tp_setattro */
    0,                                      /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
    UInt32_tp_doc,                          /* tp_doc */
    0,                                      /* tp_traverse */
    0,                                      /* tp_clear */
    0,                                      /* tp_richcompare */
    0,                                      /* tp_weaklistoffset */
    0,                                      /* tp_iter */
    0,                                      /* tp_iternext */
    0,                                      /* tp_methods */
    0,                                      /* tp_members */
    0,                                      /* tp_getset */
    DEFERRED_ADDRESS(&DBusPyLongBase_Type),  /* tp_base */
    0,                                      /* tp_dict */
    0,                                      /* tp_descr_get */
    0,                                      /* tp_descr_set */
    0,                                      /* tp_dictoffset */
    0,                                      /* tp_init */
    0,                                      /* tp_alloc */
    UInt32_tp_new,                          /* tp_new */
};

/* Int64 =========================================================== */

PyDoc_STRVAR(Int64_tp_doc,
"dbus.Int64(value: " LONG_TYPE_NAME "[, variant_level: int])\n"
"\n"
"A signed 64-bit integer between -0x8000 0000 0000 0000 and\n"
"+0x7FFF FFFF FFFF FFFF, represented as a\n"
"subtype of ``long`` in Python 2 or ``int`` in Python 3.\n"
"\n"
"Note that this may be changed in future to be a subtype of `int` on\n"
"64-bit platforms; applications should not rely on either behaviour.\n"
"\n"
"``value`` must be within the allowed range, or `OverflowError` will be\n"
"raised.\n"
"\n"
":py:attr:`variant_level` must be non-negative; the default is 0.\n"
"\n"
".. py:attribute:: variant_level\n"
"\n"
"    Indicates how many nested Variant containers this object\n"
"    is contained in: if a message's wire format has a variant containing a\n"
"    variant containing an int64, this is represented in Python by an\n"
"    Int64 with variant_level==2.\n"
);

#ifdef DBUS_PYTHON_64_BIT_WORKS
dbus_int64_t
dbus_py_int64_range_check(PyObject *obj)
{
    PY_LONG_LONG i;
    PyObject *long_obj = PyNumber_Long(obj);

    if (!long_obj) return -1;
    i = PyLong_AsLongLong(long_obj);
    if (i == -1 && PyErr_Occurred()) {
        Py_CLEAR(long_obj);
        return -1;
    }
    if (i < INT64_MIN || i > INT64_MAX) {
        PyErr_SetString(PyExc_OverflowError, "Value out of range for Int64");
        Py_CLEAR(long_obj);
        return -1;
    }
    Py_CLEAR(long_obj);
    return i;
}
#endif

static PyObject *
Int64_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{
#ifdef DBUS_PYTHON_64_BIT_WORKS
    PyObject *self = (DBusPyLongBase_Type.tp_new)(cls, args, kwargs);
    if (self && dbus_py_int64_range_check(self) == -1 && PyErr_Occurred()) {
        Py_CLEAR(self);
        return NULL;
    }
    return self;
#else
    PyErr_SetString(PyExc_NotImplementedError,
                    "64-bit types are not available on this platform");
    return NULL;
#endif
}

PyTypeObject DBusPyInt64_Type = {
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
    "dbus.Int64",
    0,
    0,
    0,                                      /* tp_dealloc */
    0,                                      /* tp_print */
    0,                                      /* tp_getattr */
    0,                                      /* tp_setattr */
    0,                                      /* tp_compare */
    0,                                      /* tp_repr */
    0,                                      /* tp_as_number */
    0,                                      /* tp_as_sequence */
    0,                                      /* tp_as_mapping */
    0,                                      /* tp_hash */
    0,                                      /* tp_call */
    0,                                      /* tp_str */
    0,                                      /* tp_getattro */
    0,                                      /* tp_setattro */
    0,                                      /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
    Int64_tp_doc,                           /* tp_doc */
    0,                                      /* tp_traverse */
    0,                                      /* tp_clear */
    0,                                      /* tp_richcompare */
    0,                                      /* tp_weaklistoffset */
    0,                                      /* tp_iter */
    0,                                      /* tp_iternext */
    0,                                      /* tp_methods */
    0,                                      /* tp_members */
    0,                                      /* tp_getset */
    DEFERRED_ADDRESS(&DBusPyLongBase_Type),  /* tp_base */
    0,                                      /* tp_dict */
    0,                                      /* tp_descr_get */
    0,                                      /* tp_descr_set */
    0,                                      /* tp_dictoffset */
    0,                                      /* tp_init */
    0,                                      /* tp_alloc */
    Int64_tp_new,                           /* tp_new */
};

/* UInt64 =========================================================== */

PyDoc_STRVAR(UInt64_tp_doc,
"dbus.UInt64(value: " LONG_TYPE_NAME "[, variant_level: int])\n"
"\n"
"An unsigned 64-bit integer between 0 and 0xFFFF FFFF FFFF FFFF,\n"
"subtype of ``long`` in Python 2 or ``int`` in Python 3.\n"
"\n"
"``value`` must be within the allowed range, or `OverflowError` will be\n"
"raised.\n"
"\n"
":py:attr:`variant_level` must be non-negative; the default is 0.\n"
"\n"
".. py:attribute:: variant_level\n"
"\n"
"    Indicates how many nested Variant containers this object\n"
"    is contained in: if a message's wire format has a variant containing a\n"
"    variant containing a uint64, this is represented in Python by a\n"
"    UInt64 with variant_level==2.\n"
);

dbus_uint64_t
dbus_py_uint64_range_check(PyObject *obj)
{
    unsigned PY_LONG_LONG i;
    PyObject *long_obj = PyNumber_Long(obj);

    if (!long_obj) return (dbus_uint64_t)(-1);
    i = PyLong_AsUnsignedLongLong(long_obj);
    if (i == (unsigned PY_LONG_LONG)(-1) && PyErr_Occurred()) {
        Py_CLEAR(long_obj);
        return (dbus_uint64_t)(-1);
    }
    if (i > UINT64_MAX) {
        PyErr_SetString(PyExc_OverflowError, "Value out of range for UInt64");
        Py_CLEAR(long_obj);
        return (dbus_uint64_t)(-1);
    }
    Py_CLEAR(long_obj);
    return i;
}

static PyObject *
UInt64_tp_new (PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{
#ifdef DBUS_PYTHON_64_BIT_WORKS
    PyObject *self = (DBusPyLongBase_Type.tp_new)(cls, args, kwargs);
    if (self && dbus_py_uint64_range_check(self) == (dbus_uint64_t)(-1)
        && PyErr_Occurred()) {
        Py_CLEAR(self);
        return NULL;
    }
    return self;
#else
    PyErr_SetString(PyExc_NotImplementedError,
                    "64-bit integer types are not supported on this platform");
    return NULL;
#endif
}

PyTypeObject DBusPyUInt64_Type = {
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
    "dbus.UInt64",
    0,
    0,
    0,                                      /* tp_dealloc */
    0,                                      /* tp_print */
    0,                                      /* tp_getattr */
    0,                                      /* tp_setattr */
    0,                                      /* tp_compare */
    0,                                      /* tp_repr */
    0,                                      /* tp_as_number */
    0,                                      /* tp_as_sequence */
    0,                                      /* tp_as_mapping */
    0,                                      /* tp_hash */
    0,                                      /* tp_call */
    0,                                      /* tp_str */
    0,                                      /* tp_getattro */
    0,                                      /* tp_setattro */
    0,                                      /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
    UInt64_tp_doc,                          /* tp_doc */
    0,                                      /* tp_traverse */
    0,                                      /* tp_clear */
    0,                                      /* tp_richcompare */
    0,                                      /* tp_weaklistoffset */
    0,                                      /* tp_iter */
    0,                                      /* tp_iternext */
    0,                                      /* tp_methods */
    0,                                      /* tp_members */
    0,                                      /* tp_getset */
    DEFERRED_ADDRESS(&DBusPyLongBase_Type),  /* tp_base */
    0,                                      /* tp_dict */
    0,                                      /* tp_descr_get */
    0,                                      /* tp_descr_set */
    0,                                      /* tp_dictoffset */
    0,                                      /* tp_init */
    0,                                      /* tp_alloc */
    UInt64_tp_new,                          /* tp_new */
};

dbus_bool_t
dbus_py_init_int_types(void)
{
    DBusPyInt16_Type.tp_base = &INTBASE;
    if (PyType_Ready(&DBusPyInt16_Type) < 0) return 0;
    /* disable the tp_print copied from PyInt_Type, so tp_repr gets called as
    desired */
    DBusPyInt16_Type.tp_print = NULL;

    DBusPyUInt16_Type.tp_base = &INTBASE;
    if (PyType_Ready(&DBusPyUInt16_Type) < 0) return 0;
    DBusPyUInt16_Type.tp_print = NULL;

    DBusPyInt32_Type.tp_base = &INTBASE;
    if (PyType_Ready(&DBusPyInt32_Type) < 0) return 0;
    DBusPyInt32_Type.tp_print = NULL;

    DBusPyUInt32_Type.tp_base = &DBusPyLongBase_Type;
    if (PyType_Ready(&DBusPyUInt32_Type) < 0) return 0;
    DBusPyUInt32_Type.tp_print = NULL;

#if defined(DBUS_HAVE_INT64) && defined(HAVE_LONG_LONG)
    DBusPyInt64_Type.tp_base = &DBusPyLongBase_Type;
    if (PyType_Ready(&DBusPyInt64_Type) < 0) return 0;
    DBusPyInt64_Type.tp_print = NULL;

    DBusPyUInt64_Type.tp_base = &DBusPyLongBase_Type;
    if (PyType_Ready(&DBusPyUInt64_Type) < 0) return 0;
    DBusPyUInt64_Type.tp_print = NULL;
#endif

    DBusPyBoolean_Type.tp_base = &INTBASE;
    if (PyType_Ready(&DBusPyBoolean_Type) < 0) return 0;
    DBusPyBoolean_Type.tp_print = NULL;

    return 1;
}

dbus_bool_t
dbus_py_insert_int_types(PyObject *this_module)
{
    /* PyModule_AddObject steals a ref */
    Py_INCREF(&DBusPyInt16_Type);
    Py_INCREF(&DBusPyUInt16_Type);
    Py_INCREF(&DBusPyInt32_Type);
    Py_INCREF(&DBusPyUInt32_Type);
    Py_INCREF(&DBusPyInt64_Type);
    Py_INCREF(&DBusPyUInt64_Type);
    Py_INCREF(&DBusPyBoolean_Type);
    if (PyModule_AddObject(this_module, "Int16",
                           (PyObject *)&DBusPyInt16_Type) < 0) return 0;
    if (PyModule_AddObject(this_module, "UInt16",
                           (PyObject *)&DBusPyUInt16_Type) < 0) return 0;
    if (PyModule_AddObject(this_module, "Int32",
                           (PyObject *)&DBusPyInt32_Type) < 0) return 0;
    if (PyModule_AddObject(this_module, "UInt32",
                           (PyObject *)&DBusPyUInt32_Type) < 0) return 0;
    if (PyModule_AddObject(this_module, "Int64",
                           (PyObject *)&DBusPyInt64_Type) < 0) return 0;
    if (PyModule_AddObject(this_module, "UInt64",
                           (PyObject *)&DBusPyUInt64_Type) < 0) return 0;
    if (PyModule_AddObject(this_module, "Boolean",
                           (PyObject *)&DBusPyBoolean_Type) < 0) return 0;

    return 1;
}

/* vim:set ft=c cino< sw=4 sts=4 et: */