Update NEWS, README for 0.80.0
[dbus-python-phuang.git] / _dbus_bindings / module.c
blobbbc6173151b701e861b8769b23da1bffcd054c23
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
24 #include "config.h"
26 #include <Python.h>
27 #include <structmember.h>
29 #include "dbus_bindings-internal.h"
31 PyDoc_STRVAR(module_doc,
32 "Low-level Python bindings for libdbus. Don't use this module directly -\n"
33 "the public API is provided by the `dbus`, `dbus.service`, `dbus.mainloop`\n"
34 "and `dbus.mainloop.glib` modules, with a lower-level API provided by the\n"
35 "`dbus.lowlevel` module.\n"
38 /* Global functions - validation wrappers ===========================*/
40 PyDoc_STRVAR(validate_bus_name__doc__,
41 "validate_bus_name(name, allow_unique=True, allow_well_known=True)\n"
42 "\n"
43 "Raise ValueError if the argument is not a valid bus name.\n"
44 "\n"
45 "By default both unique and well-known names are accepted.\n"
46 "\n"
47 ":Parameters:\n"
48 " `name` : str\n"
49 " The name to be validated\n"
50 " `allow_unique` : bool\n"
51 " If False, unique names of the form :1.123 will be rejected\n"
52 " `allow_well_known` : bool\n"
53 " If False, well-known names of the form com.example.Foo\n"
54 " will be rejected\n"
55 ":Since: 0.80\n"
58 static PyObject *
59 validate_bus_name(PyObject *unused UNUSED, PyObject *args, PyObject *kwargs)
61 const char *name;
62 int allow_unique = 1;
63 int allow_well_known = 1;
64 static char *argnames[] = { "name", "allow_unique", "allow_well_known",
65 NULL };
67 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
68 "s|ii:validate_bus_name", argnames,
69 &name, &allow_unique,
70 &allow_well_known)) {
71 return NULL;
73 if (!dbus_py_validate_bus_name(name, !!allow_unique, !!allow_well_known)) {
74 return NULL;
76 Py_RETURN_NONE;
79 PyDoc_STRVAR(validate_member_name__doc__,
80 "validate_member_name(name)\n"
81 "\n"
82 "Raise ValueError if the argument is not a valid member (signal or method) "
83 "name.\n"
84 "\n"
85 ":Since: 0.80\n"
88 static PyObject *
89 validate_member_name(PyObject *unused UNUSED, PyObject *args)
91 const char *name;
93 if (!PyArg_ParseTuple(args, "s:validate_member_name", &name)) {
94 return NULL;
96 if (!dbus_py_validate_member_name(name)) {
97 return NULL;
99 Py_RETURN_NONE;
102 PyDoc_STRVAR(validate_interface_name__doc__,
103 "validate_interface_name(name)\n\n"
104 "Raise ValueError if the given string is not a valid interface name.\n"
105 "\n"
106 ":Since: 0.80\n"
109 PyDoc_STRVAR(validate_error_name__doc__,
110 "validate_error_name(name)\n\n"
111 "Raise ValueError if the given string is not a valid error name.\n"
112 "\n"
113 ":Since: 0.80\n"
116 static PyObject *
117 validate_interface_name(PyObject *unused UNUSED, PyObject *args)
119 const char *name;
121 if (!PyArg_ParseTuple(args, "s:validate_interface_name", &name)) {
122 return NULL;
124 if (!dbus_py_validate_interface_name(name)) {
125 return NULL;
127 Py_RETURN_NONE;
130 PyDoc_STRVAR(validate_object_path__doc__,
131 "validate_object_path(name)\n\n"
132 "Raise ValueError if the given string is not a valid object path.\n"
133 "\n"
134 ":Since: 0.80\n"
137 static PyObject *
138 validate_object_path(PyObject *unused UNUSED, PyObject *args)
140 const char *name;
142 if (!PyArg_ParseTuple(args, "s:validate_object_path", &name)) {
143 return NULL;
145 if (!dbus_py_validate_object_path(name)) {
146 return NULL;
148 Py_RETURN_NONE;
151 /* Global functions - main loop =====================================*/
153 /* The main loop if none is passed to the constructor */
154 static PyObject *default_main_loop = NULL;
156 /* Return a new reference to the default main loop */
157 PyObject *
158 dbus_py_get_default_main_loop(void)
160 if (!default_main_loop) {
161 Py_RETURN_NONE;
163 Py_INCREF(default_main_loop);
164 return default_main_loop;
167 PyDoc_STRVAR(get_default_main_loop__doc__,
168 "get_default_main_loop() -> object\n\n"
169 "Return the global default dbus-python main loop wrapper, which is used\n"
170 "when no main loop wrapper is passed to the Connection constructor.\n"
171 "\n"
172 "If None, there is no default and you should always pass the mainloop\n"
173 "parameter to the constructor - if you don't, then asynchronous calls,\n"
174 "connecting to signals and exporting objects will raise an exception.\n"
175 "There is no default until 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 _dbus_py_func_ptr dbus_bindings_API[DBUS_BINDINGS_API_COUNT];
242 dbus_bindings_API[0] = (_dbus_py_func_ptr)&API_count;
243 dbus_bindings_API[1] = (_dbus_py_func_ptr)DBusPyConnection_BorrowDBusConnection;
244 dbus_bindings_API[2] = (_dbus_py_func_ptr)DBusPyNativeMainLoop_New4;
246 default_main_loop = NULL;
248 /* I'd rather not initialize threads if we can help it - dbus-python and
249 pygobject both release and re-obtain the GIL on a regular basis, which is
250 much simpler (basically free) before threads are initialized.
252 However, on Python < 2.4.2c1 you aren't allowed to call
253 PyGILState_Release without initializing threads first. */
254 if (strcmp(Py_GetVersion(), "2.4.2c1") < 0) {
255 PyEval_InitThreads();
258 if (!dbus_py_init_generic()) return;
259 if (!dbus_py_init_exception_types()) return;
260 if (!dbus_py_init_abstract()) return;
261 if (!dbus_py_init_signature()) return;
262 if (!dbus_py_init_int_types()) return;
263 if (!dbus_py_init_string_types()) return;
264 if (!dbus_py_init_float_types()) return;
265 if (!dbus_py_init_container_types()) return;
266 if (!dbus_py_init_byte_types()) return;
267 if (!dbus_py_init_message_types()) return;
268 if (!dbus_py_init_pending_call()) return;
269 if (!dbus_py_init_mainloop()) return;
270 if (!dbus_py_init_conn_types()) return;
271 if (!dbus_py_init_bus_types()) return;
273 this_module = Py_InitModule3("_dbus_bindings", module_functions, module_doc);
274 if (!this_module) return;
276 if (!dbus_py_insert_exception_types(this_module)) return;
277 if (!dbus_py_insert_abstract_types(this_module)) return;
278 if (!dbus_py_insert_signature(this_module)) return;
279 if (!dbus_py_insert_int_types(this_module)) return;
280 if (!dbus_py_insert_string_types(this_module)) return;
281 if (!dbus_py_insert_float_types(this_module)) return;
282 if (!dbus_py_insert_container_types(this_module)) return;
283 if (!dbus_py_insert_byte_types(this_module)) return;
284 if (!dbus_py_insert_message_types(this_module)) return;
285 if (!dbus_py_insert_pending_call(this_module)) return;
286 if (!dbus_py_insert_mainloop_types(this_module)) return;
287 if (!dbus_py_insert_conn_types(this_module)) return;
288 if (!dbus_py_insert_bus_types(this_module)) return;
290 #define ADD_CONST_VAL(x, v) \
291 if (PyModule_AddIntConstant(this_module, x, v) < 0) return;
292 #define ADD_CONST_PREFIXED(x) ADD_CONST_VAL(#x, DBUS_##x)
293 #define ADD_CONST(x) ADD_CONST_VAL(#x, x)
295 ADD_CONST(DBUS_START_REPLY_SUCCESS)
296 ADD_CONST(DBUS_START_REPLY_ALREADY_RUNNING)
298 ADD_CONST_PREFIXED(RELEASE_NAME_REPLY_RELEASED)
299 ADD_CONST_PREFIXED(RELEASE_NAME_REPLY_NON_EXISTENT)
300 ADD_CONST_PREFIXED(RELEASE_NAME_REPLY_NOT_OWNER)
302 ADD_CONST_PREFIXED(REQUEST_NAME_REPLY_PRIMARY_OWNER)
303 ADD_CONST_PREFIXED(REQUEST_NAME_REPLY_IN_QUEUE)
304 ADD_CONST_PREFIXED(REQUEST_NAME_REPLY_EXISTS)
305 ADD_CONST_PREFIXED(REQUEST_NAME_REPLY_ALREADY_OWNER)
307 ADD_CONST_PREFIXED(NAME_FLAG_ALLOW_REPLACEMENT)
308 ADD_CONST_PREFIXED(NAME_FLAG_REPLACE_EXISTING)
309 ADD_CONST_PREFIXED(NAME_FLAG_DO_NOT_QUEUE)
311 ADD_CONST_PREFIXED(BUS_SESSION)
312 ADD_CONST_PREFIXED(BUS_SYSTEM)
313 ADD_CONST_PREFIXED(BUS_STARTER)
315 ADD_CONST_PREFIXED(MESSAGE_TYPE_INVALID)
316 ADD_CONST_PREFIXED(MESSAGE_TYPE_METHOD_CALL)
317 ADD_CONST_PREFIXED(MESSAGE_TYPE_METHOD_RETURN)
318 ADD_CONST_PREFIXED(MESSAGE_TYPE_ERROR)
319 ADD_CONST_PREFIXED(MESSAGE_TYPE_SIGNAL)
321 ADD_CONST_PREFIXED(MESSAGE_TYPE_SIGNAL)
323 ADD_CONST_PREFIXED(TYPE_INVALID)
324 ADD_CONST_PREFIXED(TYPE_BYTE)
325 ADD_CONST_PREFIXED(TYPE_BOOLEAN)
326 ADD_CONST_PREFIXED(TYPE_INT16)
327 ADD_CONST_PREFIXED(TYPE_UINT16)
328 ADD_CONST_PREFIXED(TYPE_INT32)
329 ADD_CONST_PREFIXED(TYPE_UINT32)
330 ADD_CONST_PREFIXED(TYPE_INT64)
331 ADD_CONST_PREFIXED(TYPE_UINT64)
332 ADD_CONST_PREFIXED(TYPE_DOUBLE)
333 ADD_CONST_PREFIXED(TYPE_STRING)
334 ADD_CONST_PREFIXED(TYPE_OBJECT_PATH)
335 ADD_CONST_PREFIXED(TYPE_SIGNATURE)
336 ADD_CONST_PREFIXED(TYPE_ARRAY)
337 ADD_CONST_PREFIXED(TYPE_STRUCT)
338 ADD_CONST_VAL("STRUCT_BEGIN", DBUS_STRUCT_BEGIN_CHAR)
339 ADD_CONST_VAL("STRUCT_END", DBUS_STRUCT_END_CHAR)
340 ADD_CONST_PREFIXED(TYPE_VARIANT)
341 ADD_CONST_PREFIXED(TYPE_DICT_ENTRY)
342 ADD_CONST_VAL("DICT_ENTRY_BEGIN", DBUS_DICT_ENTRY_BEGIN_CHAR)
343 ADD_CONST_VAL("DICT_ENTRY_END", DBUS_DICT_ENTRY_END_CHAR)
345 ADD_CONST_PREFIXED(HANDLER_RESULT_HANDLED)
346 ADD_CONST_PREFIXED(HANDLER_RESULT_NOT_YET_HANDLED)
347 ADD_CONST_PREFIXED(HANDLER_RESULT_NEED_MEMORY)
349 ADD_CONST_PREFIXED(WATCH_READABLE)
350 ADD_CONST_PREFIXED(WATCH_WRITABLE)
351 ADD_CONST_PREFIXED(WATCH_HANGUP)
352 ADD_CONST_PREFIXED(WATCH_ERROR)
354 if (PyModule_AddStringConstant(this_module, "__docformat__",
355 "restructuredtext") < 0) return;
357 if (PyModule_AddStringConstant(this_module, "__version__",
358 PACKAGE_VERSION) < 0) return;
360 if (PyModule_AddIntConstant(this_module, "_python_version",
361 PY_VERSION_HEX) < 0) return;
363 c_api = PyCObject_FromVoidPtr ((void *)dbus_bindings_API, NULL);
364 if (!c_api) {
365 return;
367 PyModule_AddObject(this_module, "_C_API", c_api);
370 /* vim:set ft=c cino< sw=4 sts=4 et: */