Imported Upstream version 1.2.0
[platform/upstream/dbus-python.git] / _dbus_bindings / dbus_bindings-internal.h
1 /* _dbus_bindings internal API. For use within _dbus_bindings only.
2  *
3  * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25
26 #ifndef DBUS_BINDINGS_INTERNAL_H
27 #define DBUS_BINDINGS_INTERNAL_H
28
29 #define PY_SSIZE_T_CLEAN 1
30
31 #include <Python.h>
32
33 #define INSIDE_DBUS_PYTHON_BINDINGS
34 #include "dbus-python.h"
35
36 /* no need for extern "C", this is only for internal use */
37
38 /* on/off switch for debugging support (see below) */
39 #undef USING_DBG
40 #if 0 && !defined(DBG_IS_TOO_VERBOSE)
41 #   define USING_DBG 1
42 #endif
43
44 #define DEFINE_CHECK(type) \
45 static inline int type##_Check (PyObject *o) \
46 { \
47     return (PyObject_TypeCheck (o, &type##_Type)); \
48 } \
49 static inline int type##_CheckExact (PyObject *o) \
50 { \
51     return (Py_TYPE(o) == &type##_Type); \
52 }
53
54 /* This is a clever little trick to make writing the various object reprs
55  * easier.  It relies on Python's %V format option which consumes two
56  * arguments.  The first is a unicode object which may be NULL, and the second
57  * is a char* which will be used if the first parameter is NULL.
58  *
59  * The issue is that we don't know whether the `parent_repr` at the call site
60  * is a unicode or a bytes (a.k.a. 8-bit string).  Under Python 3, it will
61  * always be a unicode.  Under Python 2 it will *probably* be a bytes/str, but
62  * could potentially be a unicode.  So, we check the type, and if it's a
63  * unicode, we pass that as the first argument, leaving NULL as the second
64  * argument (since it will never be checked).  However, if the object is not a
65  * unicode, it better be a bytes.  In that case, we'll pass NULL as the first
66  * argument so that the second one gets used, and we'll dig the char* out of
67  * the bytes object for that purpose.
68  *
69  * You might think that this would crash if obj is neither a bytes/str or
70  * unicode, and you'd be right *except* that Python doesn't allow any other
71  * types to be returned in the reprs.  Also, since obj will always be the repr
72  * of a built-in type, it will never be anything other than a bytes or a
73  * unicode in any version of Python.  So in practice, this is safe.
74  */
75 #define REPRV(obj) \
76     (PyUnicode_Check(obj) ? (obj) : NULL), \
77     (PyUnicode_Check(obj) ? NULL : PyBytes_AS_STRING(obj))
78
79 #ifdef PY3
80 #define NATIVEINT_TYPE (PyLong_Type)
81 #define NATIVEINT_FROMLONG(x) (PyLong_FromLong(x))
82 #define NATIVEINT_ASLONG(x) (PyLong_AsLong(x))
83 #define INTORLONG_CHECK(obj) (PyLong_Check(obj))
84 #define NATIVESTR_TYPE (PyUnicode_Type)
85 #define NATIVESTR_CHECK(obj) (PyUnicode_Check(obj))
86 #define NATIVESTR_FROMSTR(obj) (PyUnicode_FromString(obj))
87 #else
88 #define NATIVEINT_TYPE (PyInt_Type)
89 #define NATIVEINT_FROMLONG(x) (PyInt_FromLong(x))
90 #define NATIVEINT_ASLONG(x) (PyInt_AsLong(x))
91 #define INTORLONG_CHECK(obj) (PyLong_Check(obj) || PyInt_Check(obj))
92 #define NATIVESTR_TYPE (PyBytes_Type)
93 #define NATIVESTR_CHECK(obj) (PyBytes_Check(obj))
94 #define NATIVESTR_FROMSTR(obj) (PyBytes_FromString(obj))
95 #endif
96
97 #ifdef PY3
98 PyMODINIT_FUNC PyInit__dbus_bindings(void);
99 #else
100 PyMODINIT_FUNC init_dbus_bindings(void);
101 #endif
102
103 /* conn.c */
104 extern PyTypeObject DBusPyConnection_Type;
105 DEFINE_CHECK(DBusPyConnection)
106 extern dbus_bool_t dbus_py_init_conn_types(void);
107 extern dbus_bool_t dbus_py_insert_conn_types(PyObject *this_module);
108
109 /* libdbusconn.c */
110 extern PyTypeObject DBusPyLibDBusConnection_Type;
111 DEFINE_CHECK(DBusPyLibDBusConnection)
112 PyObject *DBusPyLibDBusConnection_New(DBusConnection *conn);
113 extern dbus_bool_t dbus_py_init_libdbus_conn_types(void);
114 extern dbus_bool_t dbus_py_insert_libdbus_conn_types(PyObject *this_module);
115
116 /* bus.c */
117 extern dbus_bool_t dbus_py_init_bus_types(void);
118 extern dbus_bool_t dbus_py_insert_bus_types(PyObject *this_module);
119
120 /* exceptions.c */
121 extern PyObject *DBusPyException_SetString(const char *msg);
122 extern PyObject *DBusPyException_ConsumeError(DBusError *error);
123 extern dbus_bool_t dbus_py_init_exception_types(void);
124 extern dbus_bool_t dbus_py_insert_exception_types(PyObject *this_module);
125
126 /* types */
127 extern PyTypeObject DBusPyBoolean_Type;
128 DEFINE_CHECK(DBusPyBoolean)
129 extern PyTypeObject DBusPyObjectPath_Type, DBusPySignature_Type;
130 DEFINE_CHECK(DBusPyObjectPath)
131 DEFINE_CHECK(DBusPySignature)
132 extern PyTypeObject DBusPyArray_Type, DBusPyDict_Type, DBusPyStruct_Type;
133 DEFINE_CHECK(DBusPyArray)
134 DEFINE_CHECK(DBusPyDict)
135 DEFINE_CHECK(DBusPyStruct)
136 extern PyTypeObject DBusPyByte_Type, DBusPyByteArray_Type;
137 DEFINE_CHECK(DBusPyByteArray)
138 DEFINE_CHECK(DBusPyByte)
139 extern PyTypeObject DBusPyString_Type;
140 DEFINE_CHECK(DBusPyString)
141 #ifndef PY3
142 extern PyTypeObject DBusPyUTF8String_Type;
143 DEFINE_CHECK(DBusPyUTF8String)
144 #endif
145 extern PyTypeObject DBusPyDouble_Type;
146 DEFINE_CHECK(DBusPyDouble)
147 extern PyTypeObject DBusPyInt16_Type, DBusPyUInt16_Type;
148 DEFINE_CHECK(DBusPyInt16)
149 DEFINE_CHECK(DBusPyUInt16)
150 extern PyTypeObject DBusPyInt32_Type, DBusPyUInt32_Type;
151 DEFINE_CHECK(DBusPyInt32)
152 DEFINE_CHECK(DBusPyUInt32)
153 extern PyTypeObject DBusPyUnixFd_Type;
154 DEFINE_CHECK(DBusPyUnixFd)
155 extern PyTypeObject DBusPyInt64_Type, DBusPyUInt64_Type;
156 DEFINE_CHECK(DBusPyInt64)
157 DEFINE_CHECK(DBusPyUInt64)
158 extern dbus_bool_t dbus_py_init_abstract(void);
159 extern dbus_bool_t dbus_py_init_signature(void);
160 extern dbus_bool_t dbus_py_init_int_types(void);
161 extern dbus_bool_t dbus_py_init_unixfd_type(void);
162 extern dbus_bool_t dbus_py_init_string_types(void);
163 extern dbus_bool_t dbus_py_init_float_types(void);
164 extern dbus_bool_t dbus_py_init_container_types(void);
165 extern dbus_bool_t dbus_py_init_byte_types(void);
166 extern dbus_bool_t dbus_py_insert_abstract_types(PyObject *this_module);
167 extern dbus_bool_t dbus_py_insert_signature(PyObject *this_module);
168 extern dbus_bool_t dbus_py_insert_int_types(PyObject *this_module);
169 extern dbus_bool_t dbus_py_insert_unixfd_type(PyObject *this_module);
170 extern dbus_bool_t dbus_py_insert_string_types(PyObject *this_module);
171 extern dbus_bool_t dbus_py_insert_float_types(PyObject *this_module);
172 extern dbus_bool_t dbus_py_insert_container_types(PyObject *this_module);
173 extern dbus_bool_t dbus_py_insert_byte_types(PyObject *this_module);
174
175 int dbus_py_unix_fd_get_fd(PyObject *self);
176
177 /* generic */
178 extern void dbus_py_take_gil_and_xdecref(PyObject *);
179 extern int dbus_py_immutable_setattro(PyObject *, PyObject *, PyObject *);
180 extern PyObject *dbus_py_empty_tuple;
181 extern dbus_bool_t dbus_py_init_generic(void);
182
183 /* message.c */
184 extern DBusMessage *DBusPyMessage_BorrowDBusMessage(PyObject *msg);
185 extern PyObject *DBusPyMessage_ConsumeDBusMessage(DBusMessage *);
186 extern dbus_bool_t dbus_py_init_message_types(void);
187 extern dbus_bool_t dbus_py_insert_message_types(PyObject *this_module);
188
189 /* pending-call.c */
190 extern PyObject *DBusPyPendingCall_ConsumeDBusPendingCall(DBusPendingCall *,
191                                                           PyObject *);
192 extern dbus_bool_t dbus_py_init_pending_call(void);
193 extern dbus_bool_t dbus_py_insert_pending_call(PyObject *this_module);
194
195 /* mainloop.c */
196 extern dbus_bool_t dbus_py_set_up_connection(PyObject *conn,
197                                              PyObject *mainloop);
198 extern dbus_bool_t dbus_py_set_up_server(PyObject *server,
199                                          PyObject *mainloop);
200 extern PyObject *dbus_py_get_default_main_loop(void);
201 extern dbus_bool_t dbus_py_check_mainloop_sanity(PyObject *);
202 extern dbus_bool_t dbus_py_init_mainloop(void);
203 extern dbus_bool_t dbus_py_insert_mainloop_types(PyObject *);
204
205 /* server.c */
206 extern PyTypeObject DBusPyServer_Type;
207 DEFINE_CHECK(DBusPyServer)
208 extern dbus_bool_t dbus_py_init_server_types(void);
209 extern dbus_bool_t dbus_py_insert_server_types(PyObject *this_module);
210 extern DBusServer *DBusPyServer_BorrowDBusServer(PyObject *self);
211
212 /* validation.c */
213 dbus_bool_t dbus_py_validate_bus_name(const char *name,
214                                       dbus_bool_t may_be_unique,
215                                       dbus_bool_t may_be_not_unique);
216 dbus_bool_t dbus_py_validate_member_name(const char *name);
217 dbus_bool_t dbus_py_validate_interface_name(const char *name);
218 dbus_bool_t dbus_py_validate_object_path(const char *path);
219 #define dbus_py_validate_error_name dbus_py_validate_interface_name
220
221 /* debugging support */
222 void _dbus_py_assertion_failed(const char *);
223 #define DBUS_PY_RAISE_VIA_NULL_IF_FAIL(assertion) \
224     do { if (!(assertion)) { \
225             _dbus_py_assertion_failed(#assertion); \
226             return NULL; \
227         } \
228     } while (0)
229
230 #define DBUS_PY_RAISE_VIA_GOTO_IF_FAIL(assertion, label) \
231     do { if (!(assertion)) { \
232             _dbus_py_assertion_failed(#assertion); \
233             goto label; \
234         } \
235     } while (0)
236
237 #define DBUS_PY_RAISE_VIA_RETURN_IF_FAIL(assertion, value) \
238     do { if (!(assertion)) { \
239             _dbus_py_assertion_failed(#assertion); \
240             return value; \
241         } \
242     } while (0)
243
244 /* verbose debugging support */
245 #ifdef USING_DBG
246
247 #   include <sys/types.h>
248 #   include <unistd.h>
249
250 void _dbus_py_dbg_exc(void);
251 void _dbus_py_whereami(void);
252 void _dbus_py_dbg_dump_message(DBusMessage *);
253
254 #   define TRACE(self) do { \
255     fprintf(stderr, "TRACE: <%s at %p> in %s, "                 \
256             "%d refs\n",                                        \
257             self ? Py_TYPE(self)->tp_name : NULL,               \
258             self, __func__,                                     \
259             self ? (int)Py_REFCNT(self) : 0);                   \
260     } while (0)
261 #   define DBG(format, ...) fprintf(stderr, "DEBUG: " format "\n",\
262                                     __VA_ARGS__)
263 #   define DBG_EXC(format, ...) do {DBG(format, __VA_ARGS__); \
264                                     _dbus_py_dbg_exc();} while (0)
265 #   define DBG_DUMP_MESSAGE(x) _dbus_py_dbg_dump_message(x)
266 #   define DBG_WHEREAMI _dbus_py_whereami()
267
268 #else /* !defined(USING_DBG) */
269
270 #   define TRACE(self) do {} while (0)
271 #   define DBG(format, ...) do {} while (0)
272 #   define DBG_EXC(format, ...) do {} while (0)
273 #   define DBG_DUMP_MESSAGE(x) do {} while (0)
274 #   define DBG_WHEREAMI do {} while (0)
275
276 #endif /* !defined(USING_DBG) */
277
278 /* General-purpose Python glue */
279
280 #define DEFERRED_ADDRESS(ADDR) 0
281
282 #if defined(__GNUC__)
283 #   if __GNUC__ >= 3
284 #       define UNUSED __attribute__((__unused__))
285 #   else
286 #       define UNUSED /*nothing*/
287 #   endif
288 #else
289 #   define UNUSED /*nothing*/
290 #endif
291
292 #endif