TIVI-320: Add dbus-python to repos to support connman-test
[profile/ivi/dbus-python.git] / _dbus_bindings / generic.c
1 /* General Python glue code, used in _dbus_bindings but not actually anything
2  * to do with D-Bus.
3  *
4  * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use, copy,
10  * modify, merge, publish, distribute, sublicense, and/or sell copies
11  * of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */
26
27 #include "dbus_bindings-internal.h"
28
29 /* The empty tuple, held globally since dbus-python turns out to use it quite
30  * a lot
31  */
32 PyObject *dbus_py_empty_tuple = NULL;
33
34 PyObject *
35 dbus_py_tp_richcompare_by_pointer(PyObject *self,
36                                   PyObject *other,
37                                   int op)
38 {
39     if (op == Py_EQ || op == Py_NE) {
40         if (self == other) {
41             return PyInt_FromLong(op == Py_EQ);
42         }
43         return PyInt_FromLong(op == Py_NE);
44     }
45     PyErr_SetString(PyExc_TypeError,
46                     "Instances of this type are not ordered");
47     return NULL;
48 }
49
50 long
51 dbus_py_tp_hash_by_pointer(PyObject *self)
52 {
53     long hash = (long)self;
54     return (hash == -1L ? -2L : hash);
55 }
56
57 int
58 dbus_py_immutable_setattro(PyObject *obj UNUSED,
59                         PyObject *name UNUSED,
60                         PyObject *value UNUSED)
61 {
62     PyErr_SetString(PyExc_AttributeError, "Object is immutable");
63     return -1;
64 }
65
66 /* Take the global interpreter lock and decrement the reference count.
67  * Suitable for calling from a C callback. */
68 void
69 dbus_py_take_gil_and_xdecref(PyObject *obj)
70 {
71     PyGILState_STATE gil = PyGILState_Ensure();
72     Py_XDECREF(obj);
73     PyGILState_Release(gil);
74 }
75
76 dbus_bool_t
77 dbus_py_init_generic(void)
78 {
79     dbus_py_empty_tuple = PyTuple_New(0);
80     if (!dbus_py_empty_tuple) return 0;
81     return 1;
82 }
83
84 /* vim:set ft=c cino< sw=4 sts=4 et: */