Implement DBusException in pure Python; add get_dbus_name() method and name= keyword...
[dbus-python-phuang.git] / _dbus_bindings / message.c
blob3887aaa2f7d9c1af63425b0bdf99021801ce2924
1 /* Implementation of D-Bus Message and subclasses (but see message-get-args.h
2 * and message-append.h for unserialization and serialization code).
4 * Copyright (C) 2006 Collabora Ltd.
6 * Licensed under the Academic Free License version 2.1
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus_bindings-internal.h"
25 #include "message-internal.h"
27 static PyTypeObject MessageType, SignalMessageType, ErrorMessageType;
28 static PyTypeObject MethodReturnMessageType, MethodCallMessageType;
30 static inline int Message_Check(PyObject *o)
32 return (o->ob_type == &MessageType)
33 || PyObject_IsInstance(o, (PyObject *)&MessageType);
36 PyObject *
37 DBusPy_RaiseUnusableMessage(void)
39 DBusPyException_SetString("Message object is uninitialized, or has become "
40 "unusable due to error while appending "
41 "arguments");
42 return NULL;
45 PyDoc_STRVAR(Message_tp_doc,
46 "A message to be sent or received over a D-Bus Connection.\n");
48 static void Message_tp_dealloc(Message *self)
50 if (self->msg) {
51 dbus_message_unref(self->msg);
53 self->ob_type->tp_free((PyObject *)self);
56 static PyObject *
57 Message_tp_new(PyTypeObject *type,
58 PyObject *args UNUSED,
59 PyObject *kwargs UNUSED)
61 Message *self;
63 self = (Message *)type->tp_alloc(type, 0);
64 if (!self) return NULL;
65 self->msg = NULL;
66 return (PyObject *)self;
69 PyDoc_STRVAR(MethodCallMessage_tp_doc, "A method-call message.\n"
70 "\n"
71 "Constructor::\n"
72 "\n"
73 " dbus.lowlevel.MethodCallMessage(destination: str or None, path: str,\n"
74 " interface: str or None, method: str)\n"
75 "\n"
76 "``destination`` is the destination bus name, or None to send the\n"
77 "message directly to the peer (usually the bus daemon).\n"
78 "\n"
79 "``path`` is the object-path of the object whose method is to be called.\n"
80 "\n"
81 "``interface`` is the interface qualifying the method name, or None to omit\n"
82 "the interface from the message header.\n"
83 "\n"
84 "``method`` is the method name (member name).\n"
87 static int
88 MethodCallMessage_tp_init(Message *self, PyObject *args, PyObject *kwargs)
90 const char *destination, *path, *interface, *method;
91 static char *kwlist[] = {"destination", "path", "interface", "method", NULL};
93 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "zszs:__init__", kwlist,
94 &destination, &path, &interface,
95 &method)) {
96 return -1;
98 if (destination && !dbus_py_validate_bus_name(destination, 1, 1)) return -1;
99 if (!dbus_py_validate_object_path(path)) return -1;
100 if (interface && !dbus_py_validate_interface_name(interface)) return -1;
101 if (!dbus_py_validate_member_name(method)) return -1;
102 if (self->msg) {
103 dbus_message_unref(self->msg);
104 self->msg = NULL;
106 self->msg = dbus_message_new_method_call(destination, path, interface,
107 method);
108 if (!self->msg) {
109 PyErr_NoMemory();
110 return -1;
112 return 0;
115 PyDoc_STRVAR(MethodReturnMessage_tp_doc, "A method-return message.\n\n"
116 "Constructor::\n\n"
117 " dbus.lowlevel.MethodReturnMessage(method_call: MethodCallMessage)\n");
119 static int
120 MethodReturnMessage_tp_init(Message *self, PyObject *args, PyObject *kwargs)
122 Message *other;
123 static char *kwlist[] = {"method_call", NULL};
125 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!:__init__", kwlist,
126 &MessageType, &other)) {
127 return -1;
129 if (self->msg) {
130 dbus_message_unref(self->msg);
131 self->msg = NULL;
133 self->msg = dbus_message_new_method_return(other->msg);
134 if (!self->msg) {
135 PyErr_NoMemory();
136 return -1;
138 return 0;
141 PyDoc_STRVAR(SignalMessage_tp_doc, "A signal message.\n\n"
142 "Constructor::\n\n"
143 " dbus.lowlevel.SignalMessage(path: str, interface: str, method: str)\n");
144 static int
145 SignalMessage_tp_init(Message *self, PyObject *args, PyObject *kwargs)
147 const char *path, *interface, *name;
148 static char *kwlist[] = {"path", "interface", "name", NULL};
150 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sss:__init__", kwlist,
151 &path, &interface, &name)) {
152 return -1;
154 if (!dbus_py_validate_object_path(path)) return -1;
155 if (!dbus_py_validate_interface_name(interface)) return -1;
156 if (!dbus_py_validate_member_name(name)) return -1;
157 if (self->msg) {
158 dbus_message_unref(self->msg);
159 self->msg = NULL;
161 self->msg = dbus_message_new_signal(path, interface, name);
162 if (!self->msg) {
163 PyErr_NoMemory();
164 return -1;
166 return 0;
169 PyDoc_STRVAR(ErrorMessage_tp_doc, "An error message.\n\n"
170 "Constructor::\n\n"
171 " dbus.lowlevel.ErrorMessage(reply_to: Message, error_name: str,\n"
172 " error_message: str or None)\n");
173 static int
174 ErrorMessage_tp_init(Message *self, PyObject *args, PyObject *kwargs)
176 Message *reply_to;
177 const char *error_name, *error_message;
178 static char *kwlist[] = {"reply_to", "error_name", "error_message", NULL};
180 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!sz:__init__", kwlist,
181 &MessageType, &reply_to, &error_name,
182 &error_message)) {
183 return -1;
185 if (!dbus_py_validate_error_name(error_name)) return -1;
186 if (self->msg) {
187 dbus_message_unref(self->msg);
188 self->msg = NULL;
190 self->msg = dbus_message_new_error(reply_to->msg, error_name, error_message);
191 if (!self->msg) {
192 PyErr_NoMemory();
193 return -1;
195 return 0;
198 DBusMessage *
199 DBusPyMessage_BorrowDBusMessage(PyObject *msg)
201 if (!Message_Check(msg)) {
202 PyErr_SetString(PyExc_TypeError,
203 "A dbus.lowlevel.Message instance is required");
204 return NULL;
206 if (!((Message *)msg)->msg) {
207 DBusPy_RaiseUnusableMessage();
208 return NULL;
210 return ((Message *)msg)->msg;
213 PyObject *
214 DBusPyMessage_ConsumeDBusMessage(DBusMessage *msg)
216 PyTypeObject *type;
217 Message *self;
219 switch (dbus_message_get_type(msg)) {
220 case DBUS_MESSAGE_TYPE_METHOD_CALL:
221 type = &MethodCallMessageType;
222 break;
223 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
224 type = &MethodReturnMessageType;
225 break;
226 case DBUS_MESSAGE_TYPE_ERROR:
227 type = &ErrorMessageType;
228 break;
229 case DBUS_MESSAGE_TYPE_SIGNAL:
230 type = &SignalMessageType;
231 break;
232 default:
233 type = &MessageType;
236 self = (Message *)(type->tp_new) (type, dbus_py_empty_tuple, NULL);
237 if (!self) {
238 dbus_message_unref(msg);
239 return NULL;
241 self->msg = msg;
242 return (PyObject *)self;
245 PyDoc_STRVAR(Message_copy__doc__,
246 "message.copy() -> Message (or subclass)\n"
247 "Deep-copy the message, resetting the serial number to zero.\n");
248 static PyObject *
249 Message_copy(Message *self, PyObject *args UNUSED)
251 DBusMessage *msg;
252 if (!self->msg) return DBusPy_RaiseUnusableMessage();
253 msg = dbus_message_copy(self->msg);
254 if (!msg) return PyErr_NoMemory();
255 return DBusPyMessage_ConsumeDBusMessage(msg);
258 PyDoc_STRVAR(Message_get_auto_start__doc__,
259 "message.get_auto_start() -> bool\n"
260 "Return true if this message will cause an owner for the destination name\n"
261 "to be auto-started.\n");
262 static PyObject *
263 Message_get_auto_start(Message *self, PyObject *unused UNUSED)
265 if (!self->msg) return DBusPy_RaiseUnusableMessage();
266 return PyBool_FromLong(dbus_message_get_auto_start(self->msg));
269 PyDoc_STRVAR(Message_set_auto_start__doc__,
270 "message.set_auto_start(bool) -> None\n"
271 "Set whether this message will cause an owner for the destination name\n"
272 "to be auto-started.\n");
273 static PyObject *
274 Message_set_auto_start(Message *self, PyObject *args)
276 int value;
277 if (!PyArg_ParseTuple(args, "i", &value)) return NULL;
278 if (!self->msg) return DBusPy_RaiseUnusableMessage();
279 dbus_message_set_auto_start(self->msg, value ? TRUE : FALSE);
280 Py_INCREF(Py_None);
281 return Py_None;
284 PyDoc_STRVAR(Message_get_no_reply__doc__,
285 "message.get_no_reply() -> bool\n"
286 "Return true if this message need not be replied to.\n");
287 static PyObject *
288 Message_get_no_reply(Message *self, PyObject *unused UNUSED)
290 if (!self->msg) return DBusPy_RaiseUnusableMessage();
291 return PyBool_FromLong(dbus_message_get_no_reply(self->msg));
294 PyDoc_STRVAR(Message_set_no_reply__doc__,
295 "message.set_no_reply(bool) -> None\n"
296 "Set whether no reply to this message is required.\n");
297 static PyObject *
298 Message_set_no_reply(Message *self, PyObject *args)
300 int value;
301 if (!PyArg_ParseTuple(args, "i", &value)) return NULL;
302 if (!self->msg) return DBusPy_RaiseUnusableMessage();
303 dbus_message_set_no_reply(self->msg, value ? TRUE : FALSE);
304 Py_RETURN_NONE;
307 PyDoc_STRVAR(Message_get_reply_serial__doc__,
308 "message.get_reply_serial() -> long\n"
309 "Returns the serial that the message is a reply to or 0 if none.\n");
310 static PyObject *
311 Message_get_reply_serial(Message *self, PyObject *unused UNUSED)
313 if (!self->msg) return DBusPy_RaiseUnusableMessage();
314 return PyLong_FromUnsignedLong(dbus_message_get_reply_serial(self->msg));
317 PyDoc_STRVAR(Message_set_reply_serial__doc__,
318 "message.set_reply_serial(bool) -> None\n"
319 "Set the serial that this message is a reply to.\n");
320 static PyObject *
321 Message_set_reply_serial(Message *self, PyObject *args)
323 dbus_uint32_t value;
325 if (!PyArg_ParseTuple(args, "k", &value)) return NULL;
326 if (!self->msg) return DBusPy_RaiseUnusableMessage();
327 if (!dbus_message_set_reply_serial(self->msg, value)) {
328 return PyErr_NoMemory();
330 Py_INCREF(Py_None);
331 return Py_None;
334 PyDoc_STRVAR(Message_get_type__doc__,
335 "message.get_type() -> int\n\n"
336 "Returns the type of the message.\n");
337 static PyObject *
338 Message_get_type(Message *self, PyObject *unused UNUSED)
340 if (!self->msg) return DBusPy_RaiseUnusableMessage();
341 return PyInt_FromLong(dbus_message_get_type(self->msg));
344 PyDoc_STRVAR(Message_get_serial__doc__,
345 "message.get_serial() -> long\n"
346 "Returns the serial of a message or 0 if none has been specified.\n"
347 "\n"
348 "The message's serial number is provided by the application sending the\n"
349 "message and is used to identify replies to this message. All messages\n"
350 "received on a connection will have a serial, but messages you haven't\n"
351 "sent yet may return 0.\n");
352 static PyObject *
353 Message_get_serial(Message *self, PyObject *unused UNUSED)
355 if (!self->msg) return DBusPy_RaiseUnusableMessage();
356 return PyLong_FromUnsignedLong(dbus_message_get_serial(self->msg));
359 PyDoc_STRVAR(Message_is_method_call__doc__,
360 "is_method_call(interface: str, member: str) -> bool");
361 static PyObject *
362 Message_is_method_call(Message *self, PyObject *args)
364 const char *interface, *method;
366 if (!PyArg_ParseTuple(args, "ss:is_method_call", &interface, &method)) {
367 return NULL;
369 if (!self->msg) return DBusPy_RaiseUnusableMessage();
370 return PyBool_FromLong(dbus_message_is_method_call(self->msg, interface,
371 method));
374 PyDoc_STRVAR(Message_is_error__doc__,
375 "is_error(error: str) -> bool");
376 static PyObject *
377 Message_is_error(Message *self, PyObject *args)
379 const char *error_name;
381 if (!PyArg_ParseTuple(args, "s:is_error", &error_name)) {
382 return NULL;
384 if (!self->msg) return DBusPy_RaiseUnusableMessage();
385 return PyBool_FromLong(dbus_message_is_error(self->msg, error_name));
388 PyDoc_STRVAR(Message_is_signal__doc__,
389 "is_signal(interface: str, member: str) -> bool");
390 static PyObject *
391 Message_is_signal(Message *self, PyObject *args)
393 const char *interface, *signal_name;
395 if (!PyArg_ParseTuple(args, "ss:is_signal", &interface, &signal_name)) {
396 return NULL;
398 if (!self->msg) return DBusPy_RaiseUnusableMessage();
399 return PyBool_FromLong(dbus_message_is_signal(self->msg, interface,
400 signal_name));
403 PyDoc_STRVAR(Message_get_member__doc__,
404 "get_member() -> str or None");
405 static PyObject *
406 Message_get_member(Message *self, PyObject *unused UNUSED)
408 const char *c_str;
410 if (!self->msg) return DBusPy_RaiseUnusableMessage();
411 c_str = dbus_message_get_member(self->msg);
412 if (!c_str) {
413 Py_RETURN_NONE;
415 return PyString_FromString(c_str);
418 PyDoc_STRVAR(Message_has_member__doc__,
419 "has_member(name: str or None) -> bool");
420 static PyObject *
421 Message_has_member(Message *self, PyObject *args)
423 const char *name;
425 if (!PyArg_ParseTuple(args, "z:has_member", &name)) {
426 return NULL;
428 if (!self->msg) return DBusPy_RaiseUnusableMessage();
429 return PyBool_FromLong(dbus_message_has_member(self->msg, name));
432 PyDoc_STRVAR(Message_set_member__doc__,
433 "set_member(unique_name: str or None)");
434 static PyObject *
435 Message_set_member(Message *self, PyObject *args)
437 const char *name;
439 if (!PyArg_ParseTuple(args, "z:set_member", &name)) {
440 return NULL;
442 if (!self->msg) return DBusPy_RaiseUnusableMessage();
443 if (!dbus_py_validate_member_name(name)) return NULL;
444 if (!dbus_message_set_member(self->msg, name)) return PyErr_NoMemory();
445 Py_RETURN_NONE;
448 PyDoc_STRVAR(Message_get_path__doc__,
449 "get_path() -> ObjectPath or None\n\n"
450 "Return the message's destination object path (if it's a method call) or\n"
451 "source object path (if it's a method reply or a signal) or None (if it\n"
452 "has no path).\n");
453 static PyObject *
454 Message_get_path(Message *self, PyObject *unused UNUSED)
456 const char *c_str;
458 if (!self->msg) return DBusPy_RaiseUnusableMessage();
459 c_str = dbus_message_get_path(self->msg);
460 if (!c_str) {
461 Py_RETURN_NONE;
463 return PyObject_CallFunction((PyObject *)&DBusPyObjectPath_Type, "(s)", c_str);
466 PyDoc_STRVAR(Message_get_path_decomposed__doc__,
467 "get_path_decomposed() -> list of str, or None\n\n"
468 "Return a list of path components (e.g. /foo/bar -> ['foo','bar'], / -> [])\n"
469 "or None if the message has no associated path.\n");
470 static PyObject *
471 Message_get_path_decomposed(Message *self, PyObject *unused UNUSED)
473 char **paths, **ptr;
474 PyObject *ret = PyList_New(0);
476 if (!ret) return NULL;
477 if (!self->msg) {
478 Py_DECREF(ret);
479 return DBusPy_RaiseUnusableMessage();
481 if (!dbus_message_get_path_decomposed(self->msg, &paths)) {
482 Py_DECREF(ret);
483 return PyErr_NoMemory();
485 if (!paths) {
486 Py_DECREF(ret);
487 Py_RETURN_NONE;
489 for (ptr = paths; *ptr; ptr++) {
490 PyObject *str = PyString_FromString(*ptr);
492 if (!str) {
493 Py_DECREF(ret);
494 ret = NULL;
495 break;
497 if (PyList_Append(ret, str) < 0) {
498 Py_DECREF(ret);
499 ret = NULL;
500 break;
502 Py_DECREF(str);
503 str = NULL;
505 dbus_free_string_array(paths);
506 return ret;
509 PyDoc_STRVAR(Message_has_path__doc__,
510 "has_path(name: str or None) -> bool");
511 static PyObject *
512 Message_has_path(Message *self, PyObject *args)
514 const char *name;
516 if (!PyArg_ParseTuple(args, "z:has_path", &name)) {
517 return NULL;
519 if (!self->msg) return DBusPy_RaiseUnusableMessage();
520 return PyBool_FromLong(dbus_message_has_path(self->msg, name));
523 PyDoc_STRVAR(Message_set_path__doc__,
524 "set_path(name: str or None)");
525 static PyObject *
526 Message_set_path(Message *self, PyObject *args)
528 const char *name;
530 if (!PyArg_ParseTuple(args, "z:set_path", &name)) return NULL;
531 if (!self->msg) return DBusPy_RaiseUnusableMessage();
532 if (!dbus_message_has_path(self->msg, name)) return PyErr_NoMemory();
533 Py_RETURN_NONE;
536 PyDoc_STRVAR(Message_get_signature__doc__,
537 "get_signature() -> Signature or None");
538 static PyObject *
539 Message_get_signature(Message *self, PyObject *unused UNUSED)
541 const char *c_str;
543 if (!self->msg) return DBusPy_RaiseUnusableMessage();
544 c_str = dbus_message_get_signature(self->msg);
545 if (!c_str) {
546 return PyObject_CallFunction((PyObject *)&DBusPySignature_Type, "(s)", "");
548 return PyObject_CallFunction((PyObject *)&DBusPySignature_Type, "(s)", c_str);
551 PyDoc_STRVAR(Message_has_signature__doc__,
552 "has_signature(signature: str) -> bool");
553 static PyObject *
554 Message_has_signature(Message *self, PyObject *args)
556 const char *name;
558 if (!PyArg_ParseTuple(args, "s:has_signature", &name)) {
559 return NULL;
561 if (!self->msg) return DBusPy_RaiseUnusableMessage();
562 return PyBool_FromLong(dbus_message_has_signature(self->msg, name));
565 PyDoc_STRVAR(Message_get_sender__doc__,
566 "get_sender() -> str or None\n\n"
567 "Return the message's sender unique name, or None if none.\n");
568 static PyObject *
569 Message_get_sender(Message *self, PyObject *unused UNUSED)
571 const char *c_str;
573 if (!self->msg) return DBusPy_RaiseUnusableMessage();
574 c_str = dbus_message_get_sender(self->msg);
575 if (!c_str) {
576 Py_RETURN_NONE;
578 return PyString_FromString(c_str);
581 PyDoc_STRVAR(Message_has_sender__doc__,
582 "has_sender(unique_name: str) -> bool");
583 static PyObject *
584 Message_has_sender(Message *self, PyObject *args)
586 const char *name;
588 if (!PyArg_ParseTuple(args, "s:has_sender", &name)) {
589 return NULL;
591 if (!self->msg) return DBusPy_RaiseUnusableMessage();
592 return PyBool_FromLong(dbus_message_has_sender(self->msg, name));
595 PyDoc_STRVAR(Message_set_sender__doc__,
596 "set_sender(unique_name: str or None)");
597 static PyObject *
598 Message_set_sender(Message *self, PyObject *args)
600 const char *name;
602 if (!PyArg_ParseTuple(args, "z:set_sender", &name)) {
603 return NULL;
605 if (!self->msg) return DBusPy_RaiseUnusableMessage();
606 if (!dbus_py_validate_bus_name(name, 1, 0)) return NULL;
607 if (!dbus_message_set_sender(self->msg, name)) return PyErr_NoMemory();
608 Py_RETURN_NONE;
611 PyDoc_STRVAR(Message_get_destination__doc__,
612 "get_destination() -> str or None\n\n"
613 "Return the message's destination bus name, or None if none.\n");
614 static PyObject *
615 Message_get_destination(Message *self, PyObject *unused UNUSED)
617 const char *c_str;
619 if (!self->msg) return DBusPy_RaiseUnusableMessage();
620 c_str = dbus_message_get_destination(self->msg);
621 if (!c_str) {
622 Py_RETURN_NONE;
624 return PyString_FromString(c_str);
627 PyDoc_STRVAR(Message_has_destination__doc__,
628 "has_destination(bus_name: str) -> bool");
629 static PyObject *
630 Message_has_destination(Message *self, PyObject *args)
632 const char *name;
634 if (!PyArg_ParseTuple(args, "s:has_destination", &name)) {
635 return NULL;
637 if (!self->msg) return DBusPy_RaiseUnusableMessage();
638 return PyBool_FromLong(dbus_message_has_destination(self->msg, name));
641 PyDoc_STRVAR(Message_set_destination__doc__,
642 "set_destination(bus_name: str or None)");
643 static PyObject *
644 Message_set_destination(Message *self, PyObject *args)
646 const char *name;
648 if (!PyArg_ParseTuple(args, "z:set_destination", &name)) {
649 return NULL;
651 if (!self->msg) return DBusPy_RaiseUnusableMessage();
652 if (!dbus_py_validate_bus_name(name, 1, 1)) return NULL;
653 if (!dbus_message_set_destination(self->msg, name)) return PyErr_NoMemory();
654 Py_RETURN_NONE;
657 PyDoc_STRVAR(Message_get_interface__doc__,
658 "get_interface() -> str or None");
659 static PyObject *
660 Message_get_interface(Message *self, PyObject *unused UNUSED)
662 const char *c_str;
664 if (!self->msg) return DBusPy_RaiseUnusableMessage();
665 c_str = dbus_message_get_interface(self->msg);
666 if (!c_str) {
667 Py_RETURN_NONE;
669 return PyString_FromString(c_str);
672 PyDoc_STRVAR(Message_has_interface__doc__,
673 "has_interface(interface: str or None) -> bool");
674 static PyObject *
675 Message_has_interface(Message *self, PyObject *args)
677 const char *name;
679 if (!PyArg_ParseTuple(args, "z:has_interface", &name)) {
680 return NULL;
682 if (!self->msg) return DBusPy_RaiseUnusableMessage();
683 return PyBool_FromLong(dbus_message_has_interface(self->msg, name));
686 PyDoc_STRVAR(Message_set_interface__doc__,
687 "set_interface(name: str or None)");
688 static PyObject *
689 Message_set_interface(Message *self, PyObject *args)
691 const char *name;
693 if (!PyArg_ParseTuple(args, "z:set_interface", &name)) {
694 return NULL;
696 if (!self->msg) return DBusPy_RaiseUnusableMessage();
697 if (!dbus_py_validate_interface_name(name)) return NULL;
698 if (!dbus_message_set_interface(self->msg, name)) return PyErr_NoMemory();
699 Py_RETURN_NONE;
702 PyDoc_STRVAR(Message_get_error_name__doc__,
703 "get_error_name() -> str or None");
704 static PyObject *
705 Message_get_error_name(Message *self, PyObject *unused UNUSED)
707 const char *c_str;
709 if (!self->msg) return DBusPy_RaiseUnusableMessage();
710 c_str = dbus_message_get_error_name(self->msg);
711 if (!c_str) {
712 Py_RETURN_NONE;
714 return PyString_FromString(c_str);
717 PyDoc_STRVAR(Message_set_error_name__doc__,
718 "set_error_name(name: str or None)");
719 static PyObject *
720 Message_set_error_name(Message *self, PyObject *args)
722 const char *name;
724 if (!PyArg_ParseTuple(args, "z:set_error_name", &name)) {
725 return NULL;
727 if (!self->msg) return DBusPy_RaiseUnusableMessage();
728 if (!dbus_py_validate_error_name(name)) return NULL;
729 if (!dbus_message_set_error_name(self->msg, name)) return PyErr_NoMemory();
730 Py_RETURN_NONE;
733 static PyMethodDef Message_tp_methods[] = {
734 {"copy", (PyCFunction)Message_copy,
735 METH_NOARGS, Message_copy__doc__},
736 {"is_method_call", (PyCFunction)Message_is_method_call,
737 METH_VARARGS, Message_is_method_call__doc__},
738 {"is_signal", (PyCFunction)Message_is_signal,
739 METH_VARARGS, Message_is_signal__doc__},
740 {"is_error", (PyCFunction)Message_is_error,
741 METH_VARARGS, Message_is_error__doc__},
743 {"get_args_list", (PyCFunction)dbus_py_Message_get_args_list,
744 METH_VARARGS|METH_KEYWORDS, dbus_py_Message_get_args_list__doc__},
745 {"guess_signature", (PyCFunction)dbus_py_Message_guess_signature,
746 METH_VARARGS|METH_STATIC, dbus_py_Message_guess_signature__doc__},
747 {"append", (PyCFunction)dbus_py_Message_append,
748 METH_VARARGS|METH_KEYWORDS, dbus_py_Message_append__doc__},
750 {"get_auto_start", (PyCFunction)Message_get_auto_start,
751 METH_NOARGS, Message_get_auto_start__doc__},
752 {"set_auto_start", (PyCFunction)Message_set_auto_start,
753 METH_VARARGS, Message_set_auto_start__doc__},
754 {"get_destination", (PyCFunction)Message_get_destination,
755 METH_NOARGS, Message_get_destination__doc__},
756 {"set_destination", (PyCFunction)Message_set_destination,
757 METH_VARARGS, Message_set_destination__doc__},
758 {"has_destination", (PyCFunction)Message_has_destination,
759 METH_VARARGS, Message_has_destination__doc__},
760 {"get_error_name", (PyCFunction)Message_get_error_name,
761 METH_NOARGS, Message_get_error_name__doc__},
762 {"set_error_name", (PyCFunction)Message_set_error_name,
763 METH_VARARGS, Message_set_error_name__doc__},
764 {"get_interface", (PyCFunction)Message_get_interface,
765 METH_NOARGS, Message_get_interface__doc__},
766 {"set_interface", (PyCFunction)Message_set_interface,
767 METH_VARARGS, Message_set_interface__doc__},
768 {"has_interface", (PyCFunction)Message_has_interface,
769 METH_VARARGS, Message_has_interface__doc__},
770 {"get_member", (PyCFunction)Message_get_member,
771 METH_NOARGS, Message_get_member__doc__},
772 {"set_member", (PyCFunction)Message_set_member,
773 METH_VARARGS, Message_set_member__doc__},
774 {"has_member", (PyCFunction)Message_has_member,
775 METH_VARARGS, Message_has_member__doc__},
776 {"get_path", (PyCFunction)Message_get_path,
777 METH_NOARGS, Message_get_path__doc__},
778 {"get_path_decomposed", (PyCFunction)Message_get_path_decomposed,
779 METH_NOARGS, Message_get_path_decomposed__doc__},
780 {"set_path", (PyCFunction)Message_set_path,
781 METH_VARARGS, Message_set_path__doc__},
782 {"has_path", (PyCFunction)Message_has_path,
783 METH_VARARGS, Message_has_path__doc__},
784 {"get_no_reply", (PyCFunction)Message_get_no_reply,
785 METH_NOARGS, Message_get_no_reply__doc__},
786 {"set_no_reply", (PyCFunction)Message_set_no_reply,
787 METH_VARARGS, Message_set_no_reply__doc__},
788 {"get_reply_serial", (PyCFunction)Message_get_reply_serial,
789 METH_NOARGS, Message_get_reply_serial__doc__},
790 {"set_reply_serial", (PyCFunction)Message_set_reply_serial,
791 METH_VARARGS, Message_set_reply_serial__doc__},
792 {"get_sender", (PyCFunction)Message_get_sender,
793 METH_NOARGS, Message_get_sender__doc__},
794 {"set_sender", (PyCFunction)Message_set_sender,
795 METH_VARARGS, Message_set_sender__doc__},
796 {"has_sender", (PyCFunction)Message_has_sender,
797 METH_VARARGS, Message_has_sender__doc__},
798 {"get_serial", (PyCFunction)Message_get_serial,
799 METH_NOARGS, Message_get_serial__doc__},
800 {"get_signature", (PyCFunction)Message_get_signature,
801 METH_NOARGS, Message_get_signature__doc__},
802 {"has_signature", (PyCFunction)Message_has_signature,
803 METH_VARARGS, Message_has_signature__doc__},
804 {"get_type", (PyCFunction)Message_get_type,
805 METH_NOARGS, Message_get_type__doc__},
806 {NULL, NULL, 0, NULL}
809 static PyTypeObject MessageType = {
810 PyObject_HEAD_INIT(NULL)
811 0, /*ob_size*/
812 "dbus.lowlevel.Message", /*tp_name*/
813 sizeof(Message), /*tp_basicsize*/
814 0, /*tp_itemsize*/
815 (destructor)Message_tp_dealloc, /*tp_dealloc*/
816 0, /*tp_print*/
817 0, /*tp_getattr*/
818 0, /*tp_setattr*/
819 0, /*tp_compare*/
820 0, /*tp_repr*/
821 0, /*tp_as_number*/
822 0, /*tp_as_sequence*/
823 0, /*tp_as_mapping*/
824 0, /*tp_hash */
825 0, /*tp_call*/
826 0, /*tp_str*/
827 0, /*tp_getattro*/
828 0, /*tp_setattro*/
829 0, /*tp_as_buffer*/
830 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
831 Message_tp_doc, /* tp_doc */
832 0, /* tp_traverse */
833 0, /* tp_clear */
834 0, /* tp_richcompare */
835 0, /* tp_weaklistoffset */
836 0, /* tp_iter */
837 0, /* tp_iternext */
838 Message_tp_methods, /* tp_methods */
839 0, /* tp_members */
840 0, /* tp_getset */
841 0, /* tp_base */
842 0, /* tp_dict */
843 0, /* tp_descr_get */
844 0, /* tp_descr_set */
845 0, /* tp_dictoffset */
846 0, /* tp_init */
847 0, /* tp_alloc */
848 Message_tp_new, /* tp_new */
851 static PyTypeObject MethodCallMessageType = {
852 PyObject_HEAD_INIT(NULL)
853 0, /*ob_size*/
854 "dbus.lowlevel.MethodCallMessage", /*tp_name*/
855 0, /*tp_basicsize*/
856 0, /*tp_itemsize*/
857 0, /*tp_dealloc*/
858 0, /*tp_print*/
859 0, /*tp_getattr*/
860 0, /*tp_setattr*/
861 0, /*tp_compare*/
862 0, /*tp_repr*/
863 0, /*tp_as_number*/
864 0, /*tp_as_sequence*/
865 0, /*tp_as_mapping*/
866 0, /*tp_hash */
867 0, /*tp_call*/
868 0, /*tp_str*/
869 0, /*tp_getattro*/
870 0, /*tp_setattro*/
871 0, /*tp_as_buffer*/
872 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
873 MethodCallMessage_tp_doc, /* tp_doc */
874 0, /* tp_traverse */
875 0, /* tp_clear */
876 0, /* tp_richcompare */
877 0, /* tp_weaklistoffset */
878 0, /* tp_iter */
879 0, /* tp_iternext */
880 0, /* tp_methods */
881 0, /* tp_members */
882 0, /* tp_getset */
883 DEFERRED_ADDRESS(&MessageType), /* tp_base */
884 0, /* tp_dict */
885 0, /* tp_descr_get */
886 0, /* tp_descr_set */
887 0, /* tp_dictoffset */
888 (initproc)MethodCallMessage_tp_init, /* tp_init */
889 0, /* tp_alloc */
890 0, /* tp_new */
893 static PyTypeObject MethodReturnMessageType = {
894 PyObject_HEAD_INIT(NULL)
895 0, /*ob_size*/
896 "dbus.lowlevel.MethodReturnMessage", /*tp_name*/
897 0, /*tp_basicsize*/
898 0, /*tp_itemsize*/
899 0, /*tp_dealloc*/
900 0, /*tp_print*/
901 0, /*tp_getattr*/
902 0, /*tp_setattr*/
903 0, /*tp_compare*/
904 0, /*tp_repr*/
905 0, /*tp_as_number*/
906 0, /*tp_as_sequence*/
907 0, /*tp_as_mapping*/
908 0, /*tp_hash */
909 0, /*tp_call*/
910 0, /*tp_str*/
911 0, /*tp_getattro*/
912 0, /*tp_setattro*/
913 0, /*tp_as_buffer*/
914 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
915 MethodReturnMessage_tp_doc, /* tp_doc */
916 0, /* tp_traverse */
917 0, /* tp_clear */
918 0, /* tp_richcompare */
919 0, /* tp_weaklistoffset */
920 0, /* tp_iter */
921 0, /* tp_iternext */
922 0, /* tp_methods */
923 0, /* tp_members */
924 0, /* tp_getset */
925 DEFERRED_ADDRESS(&MessageType), /* tp_base */
926 0, /* tp_dict */
927 0, /* tp_descr_get */
928 0, /* tp_descr_set */
929 0, /* tp_dictoffset */
930 (initproc)MethodReturnMessage_tp_init, /* tp_init */
931 0, /* tp_alloc */
932 0, /* tp_new */
935 static PyTypeObject SignalMessageType = {
936 PyObject_HEAD_INIT(NULL)
937 0, /*ob_size*/
938 "dbus.lowlevel.SignalMessage", /*tp_name*/
939 0, /*tp_basicsize*/
940 0, /*tp_itemsize*/
941 0, /*tp_dealloc*/
942 0, /*tp_print*/
943 0, /*tp_getattr*/
944 0, /*tp_setattr*/
945 0, /*tp_compare*/
946 0, /*tp_repr*/
947 0, /*tp_as_number*/
948 0, /*tp_as_sequence*/
949 0, /*tp_as_mapping*/
950 0, /*tp_hash */
951 0, /*tp_call*/
952 0, /*tp_str*/
953 0, /*tp_getattro*/
954 0, /*tp_setattro*/
955 0, /*tp_as_buffer*/
956 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
957 SignalMessage_tp_doc, /* tp_doc */
958 0, /* tp_traverse */
959 0, /* tp_clear */
960 0, /* tp_richcompare */
961 0, /* tp_weaklistoffset */
962 0, /* tp_iter */
963 0, /* tp_iternext */
964 0, /* tp_methods */
965 0, /* tp_members */
966 0, /* tp_getset */
967 DEFERRED_ADDRESS(&MessageType), /* tp_base */
968 0, /* tp_dict */
969 0, /* tp_descr_get */
970 0, /* tp_descr_set */
971 0, /* tp_dictoffset */
972 (initproc)SignalMessage_tp_init, /* tp_init */
973 0, /* tp_alloc */
974 0, /* tp_new */
977 static PyTypeObject ErrorMessageType = {
978 PyObject_HEAD_INIT(NULL)
979 0, /*ob_size*/
980 "dbus.lowlevel.ErrorMessage", /*tp_name*/
981 0, /*tp_basicsize*/
982 0, /*tp_itemsize*/
983 0, /*tp_dealloc*/
984 0, /*tp_print*/
985 0, /*tp_getattr*/
986 0, /*tp_setattr*/
987 0, /*tp_compare*/
988 0, /*tp_repr*/
989 0, /*tp_as_number*/
990 0, /*tp_as_sequence*/
991 0, /*tp_as_mapping*/
992 0, /*tp_hash */
993 0, /*tp_call*/
994 0, /*tp_str*/
995 0, /*tp_getattro*/
996 0, /*tp_setattro*/
997 0, /*tp_as_buffer*/
998 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
999 ErrorMessage_tp_doc, /* tp_doc */
1000 0, /* tp_traverse */
1001 0, /* tp_clear */
1002 0, /* tp_richcompare */
1003 0, /* tp_weaklistoffset */
1004 0, /* tp_iter */
1005 0, /* tp_iternext */
1006 0, /* tp_methods */
1007 0, /* tp_members */
1008 0, /* tp_getset */
1009 DEFERRED_ADDRESS(&MessageType), /* tp_base */
1010 0, /* tp_dict */
1011 0, /* tp_descr_get */
1012 0, /* tp_descr_set */
1013 0, /* tp_dictoffset */
1014 (initproc)ErrorMessage_tp_init, /* tp_init */
1015 0, /* tp_alloc */
1016 0, /* tp_new */
1019 dbus_bool_t
1020 dbus_py_init_message_types(void)
1022 if (PyType_Ready(&MessageType) < 0) return 0;
1024 MethodCallMessageType.tp_base = &MessageType;
1025 if (PyType_Ready(&MethodCallMessageType) < 0) return 0;
1027 MethodReturnMessageType.tp_base = &MessageType;
1028 if (PyType_Ready(&MethodReturnMessageType) < 0) return 0;
1030 SignalMessageType.tp_base = &MessageType;
1031 if (PyType_Ready(&SignalMessageType) < 0) return 0;
1033 ErrorMessageType.tp_base = &MessageType;
1034 if (PyType_Ready(&ErrorMessageType) < 0) return 0;
1036 return 1;
1039 dbus_bool_t
1040 dbus_py_insert_message_types(PyObject *this_module)
1042 if (PyModule_AddObject(this_module, "Message",
1043 (PyObject *)&MessageType) < 0) return 0;
1045 if (PyModule_AddObject(this_module, "MethodCallMessage",
1046 (PyObject *)&MethodCallMessageType) < 0) return 0;
1048 if (PyModule_AddObject(this_module, "MethodReturnMessage",
1049 (PyObject *)&MethodReturnMessageType) < 0) return 0;
1051 if (PyModule_AddObject(this_module, "ErrorMessage",
1052 (PyObject *)&ErrorMessageType) < 0) return 0;
1054 if (PyModule_AddObject(this_module, "SignalMessage",
1055 (PyObject *)&SignalMessageType) < 0) return 0;
1057 return 1;
1060 /* vim:set ft=c cino< sw=4 sts=4 et: */