Add assertion macros and supporting functions
[dbus-python-phuang.git] / _dbus_bindings / module.c
blob6268f36da694a42e353ffd8533ff40ae315b1f2e
1 /* Main module source for the _dbus_bindings extension.
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 <Python.h>
26 #include <structmember.h>
28 #include "dbus_bindings-internal.h"
30 PyDoc_STRVAR(module_doc,
31 "Low-level Python bindings for libdbus. Don't use this module directly -\n"
32 "the public API is provided by the `dbus`, `dbus.service`, `dbus.mainloop`\n"
33 "and `dbus.mainloop.glib` modules.\n"
34 "\n"
35 ":NewField Constructor: Constructor\n"
36 ":NewField SupportedUsage: Supported usage\n"
39 /* Global functions - validation wrappers ===========================*/
41 PyDoc_STRVAR(validate_bus_name__doc__,
42 "validate_bus_name(name[, allow_unique=True[, allow_well_known=True]])\n"
43 "\n"
44 "Raise ValueError if the argument is not a valid bus name.\n"
45 "\n"
46 "By default both unique and well-known names are accepted.\n"
47 "\n"
48 ":Parameters:\n"
49 " `name` : str\n"
50 " The name to be validated\n"
51 " `allow_unique` : bool\n"
52 " If False, unique names of the form :1.123 will be rejected\n"
53 " `allow_well_known` : bool\n"
54 " If False, well-known names of the form com.example.Foo\n"
55 " will be rejected\n"
56 ":Since: 0.80\n"
59 static PyObject *
60 validate_bus_name(PyObject *unused UNUSED, PyObject *args, PyObject *kwargs)
62 const char *name;
63 int allow_unique = 1;
64 int allow_well_known = 1;
65 static char *argnames[] = { "name", "allow_unique", "allow_well_known",
66 NULL };
68 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
69 "s|ii:validate_bus_name", argnames,
70 &name, &allow_unique,
71 &allow_well_known)) {
72 return NULL;
74 if (!dbus_py_validate_bus_name(name, !!allow_unique, !!allow_well_known)) {
75 return NULL;
77 Py_RETURN_NONE;
80 PyDoc_STRVAR(validate_member_name__doc__,
81 "validate_member_name(name)\n"
82 "\n"
83 "Raise ValueError if the argument is not a valid member (signal or method) "
84 "name.\n"
85 "\n"
86 ":Since: 0.80\n"
89 static PyObject *
90 validate_member_name(PyObject *unused UNUSED, PyObject *args)
92 const char *name;
94 if (!PyArg_ParseTuple(args, "s:validate_member_name", &name)) {
95 return NULL;
97 if (!dbus_py_validate_member_name(name)) {
98 return NULL;
100 Py_RETURN_NONE;
103 PyDoc_STRVAR(validate_interface_name__doc__,
104 "validate_interface_name(name)\n\n"
105 "Raise ValueError if the given string is not a valid interface name.\n"
106 "\n"
107 ":Since: 0.80\n"
110 PyDoc_STRVAR(validate_error_name__doc__,
111 "validate_error_name(name)\n\n"
112 "Raise ValueError if the given string is not a valid error name.\n"
113 "\n"
114 ":Since: 0.80\n"
117 static PyObject *
118 validate_interface_name(PyObject *unused UNUSED, PyObject *args)
120 const char *name;
122 if (!PyArg_ParseTuple(args, "s:validate_interface_name", &name)) {
123 return NULL;
125 if (!dbus_py_validate_interface_name(name)) {
126 return NULL;
128 Py_RETURN_NONE;
131 PyDoc_STRVAR(validate_object_path__doc__,
132 "validate_object_path(name)\n\n"
133 "Raise ValueError if the given string is not a valid object path.\n"
134 "\n"
135 ":Since: 0.80\n"
138 static PyObject *
139 validate_object_path(PyObject *unused UNUSED, PyObject *args)
141 const char *name;
143 if (!PyArg_ParseTuple(args, "s:validate_object_path", &name)) {
144 return NULL;
146 if (!dbus_py_validate_object_path(name)) {
147 return NULL;
149 Py_RETURN_NONE;
152 /* Global functions - main loop =====================================*/
154 /* The main loop if none is passed to the constructor */
155 static PyObject *default_main_loop = NULL;
157 /* Return a new reference to the default main loop */
158 PyObject *
159 dbus_py_get_default_main_loop(void)
161 if (!default_main_loop) {
162 Py_RETURN_NONE;
164 Py_INCREF(default_main_loop);
165 return default_main_loop;
168 PyDoc_STRVAR(get_default_main_loop__doc__,
169 "get_default_main_loop() -> object\n\n"
170 "Return the global default dbus-python main loop wrapper, which is used\n"
171 "when no main loop wrapper is passed to the Connection constructor.\n"
172 "\n"
173 "If None, there is no default and you must always pass the mainloop\n"
174 "parameter to the constructor. This will be the case until\n"
175 "set_default_main_loop is called.\n");
176 static PyObject *
177 get_default_main_loop(PyObject *always_null UNUSED,
178 PyObject *no_args UNUSED)
180 return dbus_py_get_default_main_loop();
183 PyDoc_STRVAR(set_default_main_loop__doc__,
184 "set_default_main_loop(object)\n\n"
185 "Change the global default dbus-python main loop wrapper, which is used\n"
186 "when no main loop wrapper is passed to the Connection constructor.\n"
187 "\n"
188 "If None, return to the initial situation: there is no default, and you\n"
189 "must always pass the mainloop parameter to the constructor.\n"
190 "\n"
191 "Two types of main loop wrapper are planned in dbus-python.\n"
192 "Native main-loop wrappers are instances of dbus.mainloop.NativeMainLoop\n"
193 "supplied by extension modules like `dbus.mainloop.glib`: they have no\n"
194 "Python API, but connect themselves to ``libdbus`` using native code.\n"
196 "Python main-loop wrappers are not yet implemented. They will be objects\n"
197 "supporting the interface defined by `dbus.mainloop.MainLoop`, with an\n"
198 "API entirely based on Python methods.\n"
199 "\n"
201 static PyObject *
202 set_default_main_loop(PyObject *always_null UNUSED,
203 PyObject *args)
205 PyObject *new_loop, *old_loop;
207 if (!PyArg_ParseTuple(args, "O", &new_loop)) {
208 return NULL;
210 if (!dbus_py_check_mainloop_sanity(new_loop)) {
211 return NULL;
213 old_loop = default_main_loop;
214 Py_INCREF(new_loop);
215 default_main_loop = new_loop;
216 Py_XDECREF(old_loop);
217 Py_RETURN_NONE;
220 static PyMethodDef module_functions[] = {
221 #define ENTRY(name,flags) {#name, (PyCFunction)name, flags, name##__doc__}
222 ENTRY(validate_interface_name, METH_VARARGS),
223 ENTRY(validate_member_name, METH_VARARGS),
224 ENTRY(validate_bus_name, METH_VARARGS|METH_KEYWORDS),
225 ENTRY(validate_object_path, METH_VARARGS),
226 ENTRY(set_default_main_loop, METH_VARARGS),
227 ENTRY(get_default_main_loop, METH_NOARGS),
228 /* validate_error_name is just implemented as validate_interface_name */
229 {"validate_error_name", validate_interface_name,
230 METH_VARARGS, validate_error_name__doc__},
231 #undef ENTRY
232 {NULL, NULL, 0, NULL}
235 PyMODINIT_FUNC
236 init_dbus_bindings(void)
238 PyObject *this_module, *c_api;
239 static const int API_count = DBUS_BINDINGS_API_COUNT;
240 static void *dbus_bindings_API[DBUS_BINDINGS_API_COUNT];
242 dbus_bindings_API[0] = (void *)&API_count;
243 dbus_bindings_API[1] = (void *)Connection_BorrowDBusConnection;
244 dbus_bindings_API[2] = (void *)NativeMainLoop_New4;
246 default_main_loop = NULL;
248 if (!dbus_py_init_generic()) return;
249 if (!dbus_py_init_exception_types()) return;
250 if (!dbus_py_init_abstract()) return;
251 if (!dbus_py_init_signature()) return;
252 if (!dbus_py_init_int_types()) return;
253 if (!dbus_py_init_string_types()) return;
254 if (!dbus_py_init_float_types()) return;
255 if (!dbus_py_init_container_types()) return;
256 if (!dbus_py_init_byte_types()) return;
257 if (!dbus_py_init_message_types()) return;
258 if (!dbus_py_init_pending_call()) return;
259 if (!dbus_py_init_mainloop()) return;
260 if (!dbus_py_init_conn_types()) return;
261 if (!dbus_py_init_bus_types()) return;
263 this_module = Py_InitModule3("_dbus_bindings", module_functions, module_doc);
264 if (!this_module) return;
266 if (!dbus_py_insert_exception_types(this_module)) return;
267 if (!dbus_py_insert_abstract_types(this_module)) return;
268 if (!dbus_py_insert_signature(this_module)) return;
269 if (!dbus_py_insert_int_types(this_module)) return;
270 if (!dbus_py_insert_string_types(this_module)) return;
271 if (!dbus_py_insert_float_types(this_module)) return;
272 if (!dbus_py_insert_container_types(this_module)) return;
273 if (!dbus_py_insert_byte_types(this_module)) return;
274 if (!dbus_py_insert_message_types(this_module)) return;
275 if (!dbus_py_insert_pending_call(this_module)) return;
276 if (!dbus_py_insert_mainloop_types(this_module)) return;
277 if (!dbus_py_insert_conn_types(this_module)) return;
278 if (!dbus_py_insert_bus_types(this_module)) return;
280 #define ADD_CONST_VAL(x, v) \
281 if (PyModule_AddIntConstant(this_module, x, v) < 0) return;
282 #define ADD_CONST_PREFIXED(x) ADD_CONST_VAL(#x, DBUS_##x)
283 #define ADD_CONST(x) ADD_CONST_VAL(#x, x)
285 ADD_CONST(DBUS_START_REPLY_SUCCESS)
286 ADD_CONST(DBUS_START_REPLY_ALREADY_RUNNING)
288 ADD_CONST_PREFIXED(RELEASE_NAME_REPLY_RELEASED)
289 ADD_CONST_PREFIXED(RELEASE_NAME_REPLY_NON_EXISTENT)
290 ADD_CONST_PREFIXED(RELEASE_NAME_REPLY_NOT_OWNER)
292 ADD_CONST_PREFIXED(REQUEST_NAME_REPLY_PRIMARY_OWNER)
293 ADD_CONST_PREFIXED(REQUEST_NAME_REPLY_IN_QUEUE)
294 ADD_CONST_PREFIXED(REQUEST_NAME_REPLY_EXISTS)
295 ADD_CONST_PREFIXED(REQUEST_NAME_REPLY_ALREADY_OWNER)
297 ADD_CONST_PREFIXED(NAME_FLAG_ALLOW_REPLACEMENT)
298 ADD_CONST_PREFIXED(NAME_FLAG_REPLACE_EXISTING)
299 ADD_CONST_PREFIXED(NAME_FLAG_DO_NOT_QUEUE)
301 ADD_CONST_PREFIXED(BUS_SESSION)
302 ADD_CONST_PREFIXED(BUS_SYSTEM)
303 ADD_CONST_PREFIXED(BUS_STARTER)
305 ADD_CONST_PREFIXED(MESSAGE_TYPE_INVALID)
306 ADD_CONST_PREFIXED(MESSAGE_TYPE_METHOD_CALL)
307 ADD_CONST_PREFIXED(MESSAGE_TYPE_METHOD_RETURN)
308 ADD_CONST_PREFIXED(MESSAGE_TYPE_ERROR)
309 ADD_CONST_PREFIXED(MESSAGE_TYPE_SIGNAL)
311 ADD_CONST_PREFIXED(MESSAGE_TYPE_SIGNAL)
313 ADD_CONST_PREFIXED(TYPE_INVALID)
314 ADD_CONST_PREFIXED(TYPE_BYTE)
315 ADD_CONST_PREFIXED(TYPE_BOOLEAN)
316 ADD_CONST_PREFIXED(TYPE_INT16)
317 ADD_CONST_PREFIXED(TYPE_UINT16)
318 ADD_CONST_PREFIXED(TYPE_INT32)
319 ADD_CONST_PREFIXED(TYPE_UINT32)
320 ADD_CONST_PREFIXED(TYPE_INT64)
321 ADD_CONST_PREFIXED(TYPE_UINT64)
322 ADD_CONST_PREFIXED(TYPE_DOUBLE)
323 ADD_CONST_PREFIXED(TYPE_STRING)
324 ADD_CONST_PREFIXED(TYPE_OBJECT_PATH)
325 ADD_CONST_PREFIXED(TYPE_SIGNATURE)
326 ADD_CONST_PREFIXED(TYPE_ARRAY)
327 ADD_CONST_PREFIXED(TYPE_STRUCT)
328 ADD_CONST_VAL("STRUCT_BEGIN", DBUS_STRUCT_BEGIN_CHAR)
329 ADD_CONST_VAL("STRUCT_END", DBUS_STRUCT_END_CHAR)
330 ADD_CONST_PREFIXED(TYPE_VARIANT)
331 ADD_CONST_PREFIXED(TYPE_DICT_ENTRY)
332 ADD_CONST_VAL("DICT_ENTRY_BEGIN", DBUS_DICT_ENTRY_BEGIN_CHAR)
333 ADD_CONST_VAL("DICT_ENTRY_END", DBUS_DICT_ENTRY_END_CHAR)
335 ADD_CONST_PREFIXED(HANDLER_RESULT_HANDLED)
336 ADD_CONST_PREFIXED(HANDLER_RESULT_NOT_YET_HANDLED)
337 ADD_CONST_PREFIXED(HANDLER_RESULT_NEED_MEMORY)
339 ADD_CONST_PREFIXED(WATCH_READABLE)
340 ADD_CONST_PREFIXED(WATCH_WRITABLE)
341 ADD_CONST_PREFIXED(WATCH_HANGUP)
342 ADD_CONST_PREFIXED(WATCH_ERROR)
344 if (PyModule_AddStringConstant(this_module, "__docformat__",
345 "restructuredtext") < 0) return;
347 c_api = PyCObject_FromVoidPtr ((void *)dbus_bindings_API, NULL);
348 if (!c_api) {
349 return;
351 PyModule_AddObject(this_module, "_C_API", c_api);
354 /* vim:set ft=c cino< sw=4 sts=4 et: */