| File: | build/temp.linux-x86_64-3.8/../../dbus_bindings/message-append.c |
| Warning: | line 414, column 16 PyObject ownership leak with reference count of 1 |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* D-Bus Message serialization. This contains all the logic to map from | |||
| 2 | * Python objects to D-Bus types. | |||
| 3 | * | |||
| 4 | * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/> | |||
| 5 | * | |||
| 6 | * SPDX-License-Identifier: MIT | |||
| 7 | * | |||
| 8 | * Permission is hereby granted, free of charge, to any person | |||
| 9 | * obtaining a copy of this software and associated documentation | |||
| 10 | * files (the "Software"), to deal in the Software without | |||
| 11 | * restriction, including without limitation the rights to use, copy, | |||
| 12 | * modify, merge, publish, distribute, sublicense, and/or sell copies | |||
| 13 | * of the Software, and to permit persons to whom the Software is | |||
| 14 | * furnished to do so, subject to the following conditions: | |||
| 15 | * | |||
| 16 | * The above copyright notice and this permission notice shall be | |||
| 17 | * included in all copies or substantial portions of the Software. | |||
| 18 | * | |||
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |||
| 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |||
| 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |||
| 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |||
| 23 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |||
| 24 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |||
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |||
| 26 | * DEALINGS IN THE SOFTWARE. | |||
| 27 | */ | |||
| 28 | ||||
| 29 | #include "dbus_bindings-internal.h" | |||
| 30 | ||||
| 31 | #include <assert.h> | |||
| 32 | ||||
| 33 | #define DBG_IS_TOO_VERBOSE | |||
| 34 | #include "compat-internal.h" | |||
| 35 | #include "types-internal.h" | |||
| 36 | #include "message-internal.h" | |||
| 37 | ||||
| 38 | /* Return the number of variants wrapping the given object. Return 0 | |||
| 39 | * if the object is not a D-Bus type. | |||
| 40 | */ | |||
| 41 | static long | |||
| 42 | get_variant_level(PyObject *obj) | |||
| 43 | { | |||
| 44 | if (DBusPyString_Check(obj)) { | |||
| 45 | return ((DBusPyString *)obj)->variant_level; | |||
| 46 | } | |||
| 47 | else if (DBusPyFloatBase_Check(obj)) { | |||
| 48 | return ((DBusPyFloatBase *)obj)->variant_level; | |||
| 49 | } | |||
| 50 | else if (DBusPyArray_Check(obj)) { | |||
| 51 | return ((DBusPyArray *)obj)->variant_level; | |||
| 52 | } | |||
| 53 | else if (DBusPyDict_Check(obj)) { | |||
| 54 | return ((DBusPyDict *)obj)->variant_level; | |||
| 55 | } | |||
| 56 | else if (DBusPyLongBase_Check(obj) || | |||
| 57 | DBusPyBytesBase_Check(obj) || | |||
| 58 | DBusPyStrBase_Check(obj) || | |||
| 59 | DBusPyStruct_Check(obj)) { | |||
| 60 | return dbus_py_variant_level_get(obj); | |||
| 61 | } | |||
| 62 | else { | |||
| 63 | return 0; | |||
| 64 | } | |||
| 65 | } | |||
| 66 | ||||
| 67 | char dbus_py_Message_append__doc__[] = ( | |||
| 68 | "message.append(*args, **kwargs)\n" | |||
| 69 | "\n" | |||
| 70 | "Set the message's arguments from the positional parameter, according to\n" | |||
| 71 | "the signature given by the ``signature`` keyword parameter.\n" | |||
| 72 | "\n" | |||
| 73 | "The following type conversions are supported:\n\n" | |||
| 74 | "=============================== ===========================\n" | |||
| 75 | "D-Bus (in signature) Python\n" | |||
| 76 | "=============================== ===========================\n" | |||
| 77 | "boolean (b) any object (via bool())\n" | |||
| 78 | "byte (y) string of length 1\n" | |||
| 79 | " any integer\n" | |||
| 80 | "any integer type any integer\n" | |||
| 81 | "double (d) any float\n" | |||
| 82 | "object path anything with a __dbus_object_path__ attribute\n" | |||
| 83 | "string, signature, object path str (must be UTF-8) or unicode\n" | |||
| 84 | "dict (a{...}) any mapping\n" | |||
| 85 | "array (a...) any iterable over appropriate objects\n" | |||
| 86 | "struct ((...)) any iterable over appropriate objects\n" | |||
| 87 | "variant any object above (guess type as below)\n" | |||
| 88 | "=============================== ===========================\n" | |||
| 89 | "\n" | |||
| 90 | "Here 'any integer' means anything on which int() or long()\n" | |||
| 91 | "(as appropriate) will work, except for basestring subclasses.\n" | |||
| 92 | "'Any float' means anything on which float() will work, except\n" | |||
| 93 | "for basestring subclasses.\n" | |||
| 94 | "\n" | |||
| 95 | "If there is no signature, guess from the arguments using\n" | |||
| 96 | "the static method `Message.guess_signature`.\n" | |||
| 97 | ); | |||
| 98 | ||||
| 99 | char dbus_py_Message_guess_signature__doc__[] = ( | |||
| 100 | "guess_signature(*args) -> Signature [static method]\n\n" | |||
| 101 | "Guess a D-Bus signature which should be used to encode the given\n" | |||
| 102 | "Python objects.\n" | |||
| 103 | "\n" | |||
| 104 | "The signature is constructed as follows:\n\n" | |||
| 105 | "+-------------------------------+---------------------------+\n" | |||
| 106 | "|Python |D-Bus |\n" | |||
| 107 | "+===============================+===========================+\n" | |||
| 108 | "|D-Bus type, variant_level > 0 |variant (v) |\n" | |||
| 109 | "+-------------------------------+---------------------------+\n" | |||
| 110 | "|D-Bus type, variant_level == 0 |the corresponding type |\n" | |||
| 111 | "+-------------------------------+---------------------------+\n" | |||
| 112 | "|anything with a |object path |\n" | |||
| 113 | "|__dbus_object_path__ attribute | |\n" | |||
| 114 | "+-------------------------------+---------------------------+\n" | |||
| 115 | "|bool |boolean (y) |\n" | |||
| 116 | "+-------------------------------+---------------------------+\n" | |||
| 117 | "|any other int subclass |int32 (i) |\n" | |||
| 118 | "+-------------------------------+---------------------------+\n" | |||
| 119 | "|any other long subclass |int64 (x) |\n" | |||
| 120 | "+-------------------------------+---------------------------+\n" | |||
| 121 | "|any other float subclass |double (d) |\n" | |||
| 122 | "+-------------------------------+---------------------------+\n" | |||
| 123 | "|any other str subclass |string (s) |\n" | |||
| 124 | "+-------------------------------+---------------------------+\n" | |||
| 125 | "|any other unicode subclass |string (s) |\n" | |||
| 126 | "+-------------------------------+---------------------------+\n" | |||
| 127 | "|any other tuple subclass |struct ((...)) |\n" | |||
| 128 | "+-------------------------------+---------------------------+\n" | |||
| 129 | "|any other list subclass |array (a...), guess |\n" | |||
| 130 | "| |contents' type according to|\n" | |||
| 131 | "| |type of first item |\n" | |||
| 132 | "+-------------------------------+---------------------------+\n" | |||
| 133 | "|any other dict subclass |dict (a{...}), guess key, |\n" | |||
| 134 | "| |value type according to |\n" | |||
| 135 | "| |types for an arbitrary item|\n" | |||
| 136 | "+-------------------------------+---------------------------+\n" | |||
| 137 | "|anything else |raise TypeError |\n" | |||
| 138 | "+-------------------------------+---------------------------+\n" | |||
| 139 | ); | |||
| 140 | ||||
| 141 | /* return a new reference, possibly to None */ | |||
| 142 | static PyObject * | |||
| 143 | get_object_path(PyObject *obj) | |||
| 144 | { | |||
| 145 | PyObject *magic_attr = PyObject_GetAttr(obj, dbus_py__dbus_object_path__const); | |||
| 146 | ||||
| 147 | if (magic_attr) { | |||
| 148 | if (PyUnicode_Check(magic_attr)((((((PyObject*)(magic_attr))->ob_type))->tp_flags & ((1UL << 28))) != 0) || PyBytes_Check(magic_attr)((((((PyObject*)(magic_attr))->ob_type))->tp_flags & ((1UL << 27))) != 0)) { | |||
| 149 | return magic_attr; | |||
| 150 | } | |||
| 151 | else { | |||
| 152 | Py_CLEAR(magic_attr)do { PyObject *_py_tmp = ((PyObject*)(magic_attr)); if (_py_tmp != ((void*)0)) { (magic_attr) = ((void*)0); _Py_DECREF(((PyObject *)(_py_tmp))); } } while (0); | |||
| 153 | PyErr_SetString(PyExc_TypeError, "__dbus_object_path__ must be " | |||
| 154 | "a string"); | |||
| 155 | return NULL((void*)0); | |||
| 156 | } | |||
| 157 | } | |||
| 158 | else { | |||
| 159 | /* Ignore exceptions, except for SystemExit and KeyboardInterrupt */ | |||
| 160 | if (PyErr_ExceptionMatches(PyExc_SystemExit) || | |||
| 161 | PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) | |||
| 162 | return NULL((void*)0); | |||
| 163 | PyErr_Clear(); | |||
| 164 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 165 | } | |||
| 166 | } | |||
| 167 | ||||
| 168 | /* Return a new reference. If the object is a variant and variant_level_ptr | |||
| 169 | * is not NULL, put the variant level in the variable pointed to, and | |||
| 170 | * return the contained type instead of "v". */ | |||
| 171 | static PyObject * | |||
| 172 | _signature_string_from_pyobject(PyObject *obj, long *variant_level_ptr) | |||
| 173 | { | |||
| 174 | PyObject *magic_attr; | |||
| 175 | long variant_level = get_variant_level(obj); | |||
| 176 | ||||
| 177 | if (variant_level < 0) | |||
| 178 | return NULL((void*)0); | |||
| 179 | ||||
| 180 | if (variant_level_ptr) { | |||
| 181 | *variant_level_ptr = variant_level; | |||
| 182 | } | |||
| 183 | else if (variant_level > 0) { | |||
| 184 | return PyUnicode_FromString(DBUS_TYPE_VARIANT_AS_STRING"v"); | |||
| 185 | } | |||
| 186 | ||||
| 187 | if (obj == Py_True((PyObject *) &_Py_TrueStruct) || obj == Py_False((PyObject *) &_Py_FalseStruct)) { | |||
| 188 | return PyUnicode_FromString(DBUS_TYPE_BOOLEAN_AS_STRING"b"); | |||
| 189 | } | |||
| 190 | ||||
| 191 | magic_attr = get_object_path(obj); | |||
| 192 | if (!magic_attr) | |||
| 193 | return NULL((void*)0); | |||
| 194 | if (magic_attr != Py_None(&_Py_NoneStruct)) { | |||
| 195 | Py_CLEAR(magic_attr)do { PyObject *_py_tmp = ((PyObject*)(magic_attr)); if (_py_tmp != ((void*)0)) { (magic_attr) = ((void*)0); _Py_DECREF(((PyObject *)(_py_tmp))); } } while (0); | |||
| 196 | return PyUnicode_FromString(DBUS_TYPE_OBJECT_PATH_AS_STRING"o"); | |||
| 197 | } | |||
| 198 | Py_CLEAR(magic_attr)do { PyObject *_py_tmp = ((PyObject*)(magic_attr)); if (_py_tmp != ((void*)0)) { (magic_attr) = ((void*)0); _Py_DECREF(((PyObject *)(_py_tmp))); } } while (0); | |||
| 199 | ||||
| 200 | /* Ordering is important: some of these are subclasses of each other. */ | |||
| 201 | if (PyLong_Check(obj)((((((PyObject*)(obj))->ob_type))->tp_flags & ((1UL << 24))) != 0)) { | |||
| 202 | if (DBusPyUInt64_Check(obj)) | |||
| 203 | return PyUnicode_FromString(DBUS_TYPE_UINT64_AS_STRING"t"); | |||
| 204 | else if (DBusPyInt64_Check(obj)) | |||
| 205 | return PyUnicode_FromString(DBUS_TYPE_INT64_AS_STRING"x"); | |||
| 206 | else if (DBusPyUInt32_Check(obj)) | |||
| 207 | return PyUnicode_FromString(DBUS_TYPE_UINT32_AS_STRING"u"); | |||
| 208 | else if (DBusPyInt32_Check(obj)) | |||
| 209 | return PyUnicode_FromString(DBUS_TYPE_INT32_AS_STRING"i"); | |||
| 210 | else if (DBusPyUInt16_Check(obj)) | |||
| 211 | return PyUnicode_FromString(DBUS_TYPE_UINT16_AS_STRING"q"); | |||
| 212 | else if (DBusPyInt16_Check(obj)) | |||
| 213 | return PyUnicode_FromString(DBUS_TYPE_INT16_AS_STRING"n"); | |||
| 214 | else if (DBusPyByte_Check(obj)) | |||
| 215 | return PyUnicode_FromString(DBUS_TYPE_BYTE_AS_STRING"y"); | |||
| 216 | else if (DBusPyBoolean_Check(obj)) | |||
| 217 | return PyUnicode_FromString(DBUS_TYPE_BOOLEAN_AS_STRING"b"); | |||
| 218 | else | |||
| 219 | return PyUnicode_FromString(DBUS_TYPE_INT32_AS_STRING"i"); | |||
| 220 | } | |||
| 221 | else if (PyUnicode_Check(obj)((((((PyObject*)(obj))->ob_type))->tp_flags & ((1UL << 28))) != 0)) { | |||
| 222 | /* Object paths and signatures are unicode subtypes in Python 3 | |||
| 223 | * (the first two cases will never be true in Python 2) */ | |||
| 224 | if (DBusPyObjectPath_Check(obj)) | |||
| 225 | return PyUnicode_FromString(DBUS_TYPE_OBJECT_PATH_AS_STRING"o"); | |||
| 226 | else if (DBusPySignature_Check(obj)) | |||
| 227 | return PyUnicode_FromString(DBUS_TYPE_SIGNATURE_AS_STRING"g"); | |||
| 228 | else | |||
| 229 | return PyUnicode_FromString(DBUS_TYPE_STRING_AS_STRING"s"); | |||
| 230 | } | |||
| 231 | #if defined(DBUS_TYPE_UNIX_FD((int) 'h')) | |||
| 232 | else if (DBusPyUnixFd_Check(obj)) | |||
| 233 | return PyUnicode_FromString(DBUS_TYPE_UNIX_FD_AS_STRING"h"); | |||
| 234 | #endif | |||
| 235 | else if (PyFloat_Check(obj)((((PyObject*)(obj))->ob_type) == (&PyFloat_Type) || PyType_IsSubtype ((((PyObject*)(obj))->ob_type), (&PyFloat_Type)))) { | |||
| 236 | #ifdef WITH_DBUS_FLOAT32 | |||
| 237 | if (DBusPyDouble_Check(obj)) | |||
| 238 | return PyUnicode_FromString(DBUS_TYPE_DOUBLE_AS_STRING"d"); | |||
| 239 | else if (DBusPyFloat_Check(obj)) | |||
| 240 | return PyUnicode_FromString(DBUS_TYPE_FLOAT_AS_STRING); | |||
| 241 | else | |||
| 242 | #endif | |||
| 243 | return PyUnicode_FromString(DBUS_TYPE_DOUBLE_AS_STRING"d"); | |||
| 244 | } | |||
| 245 | else if (PyBytes_Check(obj)((((((PyObject*)(obj))->ob_type))->tp_flags & ((1UL << 27))) != 0)) { | |||
| 246 | /* Object paths and signatures are bytes subtypes in Python 2 | |||
| 247 | * (the first two cases will never be true in Python 3) */ | |||
| 248 | if (DBusPyObjectPath_Check(obj)) | |||
| 249 | return PyUnicode_FromString(DBUS_TYPE_OBJECT_PATH_AS_STRING"o"); | |||
| 250 | else if (DBusPySignature_Check(obj)) | |||
| 251 | return PyUnicode_FromString(DBUS_TYPE_SIGNATURE_AS_STRING"g"); | |||
| 252 | else if (DBusPyByteArray_Check(obj)) | |||
| 253 | return PyUnicode_FromString(DBUS_TYPE_ARRAY_AS_STRING"a" | |||
| 254 | DBUS_TYPE_BYTE_AS_STRING"y"); | |||
| 255 | else | |||
| 256 | return PyUnicode_FromString(DBUS_TYPE_STRING_AS_STRING"s"); | |||
| 257 | } | |||
| 258 | else if (PyTuple_Check(obj)((((((PyObject*)(obj))->ob_type))->tp_flags & ((1UL << 26))) != 0)) { | |||
| 259 | Py_ssize_t len = PyTuple_GET_SIZE(obj)(((PyVarObject*)(((PyTupleObject *)(obj))))->ob_size); | |||
| 260 | PyObject *list = PyList_New(len + 2); /* new ref */ | |||
| 261 | PyObject *item; /* temporary new ref */ | |||
| 262 | PyObject *empty_str; /* temporary new ref */ | |||
| 263 | PyObject *ret; | |||
| 264 | Py_ssize_t i; | |||
| 265 | ||||
| 266 | if (!list) return NULL((void*)0); | |||
| 267 | if (len == 0) { | |||
| 268 | PyErr_SetString(PyExc_ValueError, "D-Bus structs cannot be empty"); | |||
| 269 | Py_CLEAR(list)do { PyObject *_py_tmp = ((PyObject*)(list)); if (_py_tmp != ( (void*)0)) { (list) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 270 | return NULL((void*)0); | |||
| 271 | } | |||
| 272 | /* Set the first and last elements of list to be the parentheses */ | |||
| 273 | item = PyUnicode_FromString(DBUS_STRUCT_BEGIN_CHAR_AS_STRING"("); | |||
| 274 | if (PyList_SetItem(list, 0, item) < 0) { | |||
| 275 | Py_CLEAR(list)do { PyObject *_py_tmp = ((PyObject*)(list)); if (_py_tmp != ( (void*)0)) { (list) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 276 | return NULL((void*)0); | |||
| 277 | } | |||
| 278 | item = PyUnicode_FromString(DBUS_STRUCT_END_CHAR_AS_STRING")"); | |||
| 279 | if (PyList_SetItem(list, len + 1, item) < 0) { | |||
| 280 | Py_CLEAR(list)do { PyObject *_py_tmp = ((PyObject*)(list)); if (_py_tmp != ( (void*)0)) { (list) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 281 | return NULL((void*)0); | |||
| 282 | } | |||
| 283 | if (!item || !PyList_GET_ITEM(list, 0)(((PyListObject *)(list))->ob_item[0])) { | |||
| 284 | Py_CLEAR(list)do { PyObject *_py_tmp = ((PyObject*)(list)); if (_py_tmp != ( (void*)0)) { (list) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 285 | return NULL((void*)0); | |||
| 286 | } | |||
| 287 | item = NULL((void*)0); | |||
| 288 | ||||
| 289 | for (i = 0; i < len; i++) { | |||
| 290 | item = PyTuple_GetItem(obj, i); | |||
| 291 | if (!item) { | |||
| 292 | Py_CLEAR(list)do { PyObject *_py_tmp = ((PyObject*)(list)); if (_py_tmp != ( (void*)0)) { (list) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 293 | return NULL((void*)0); | |||
| 294 | } | |||
| 295 | item = _signature_string_from_pyobject(item, NULL((void*)0)); | |||
| 296 | if (!item) { | |||
| 297 | Py_CLEAR(list)do { PyObject *_py_tmp = ((PyObject*)(list)); if (_py_tmp != ( (void*)0)) { (list) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 298 | return NULL((void*)0); | |||
| 299 | } | |||
| 300 | if (PyList_SetItem(list, i + 1, item) < 0) { | |||
| 301 | Py_CLEAR(list)do { PyObject *_py_tmp = ((PyObject*)(list)); if (_py_tmp != ( (void*)0)) { (list) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 302 | return NULL((void*)0); | |||
| 303 | } | |||
| 304 | item = NULL((void*)0); | |||
| 305 | } | |||
| 306 | empty_str = PyUnicode_FromString(""); | |||
| 307 | if (!empty_str) { | |||
| 308 | /* really shouldn't happen */ | |||
| 309 | Py_CLEAR(list)do { PyObject *_py_tmp = ((PyObject*)(list)); if (_py_tmp != ( (void*)0)) { (list) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 310 | return NULL((void*)0); | |||
| 311 | } | |||
| 312 | ret = PyObject_CallMethod_PyObject_CallMethod_SizeT(empty_str, "join", "(O)", list); /* new ref */ | |||
| 313 | /* whether ret is NULL or not, */ | |||
| 314 | Py_CLEAR(empty_str)do { PyObject *_py_tmp = ((PyObject*)(empty_str)); if (_py_tmp != ((void*)0)) { (empty_str) = ((void*)0); _Py_DECREF(((PyObject *)(_py_tmp))); } } while (0); | |||
| 315 | Py_CLEAR(list)do { PyObject *_py_tmp = ((PyObject*)(list)); if (_py_tmp != ( (void*)0)) { (list) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 316 | return ret; | |||
| 317 | } | |||
| 318 | else if (PyList_Check(obj)((((((PyObject*)(obj))->ob_type))->tp_flags & ((1UL << 25))) != 0)) { | |||
| 319 | PyObject *tmp; | |||
| 320 | PyObject *ret = PyUnicode_FromString(DBUS_TYPE_ARRAY_AS_STRING"a"); | |||
| 321 | if (!ret) return NULL((void*)0); | |||
| 322 | if (DBusPyArray_Check(obj) && | |||
| 323 | PyUnicode_Check(((DBusPyArray *)obj)->signature)((((((PyObject*)(((DBusPyArray *)obj)->signature))->ob_type ))->tp_flags & ((1UL << 28))) != 0)) | |||
| 324 | { | |||
| 325 | PyObject *concat = PyUnicode_Concat( | |||
| 326 | ret, ((DBusPyArray *)obj)->signature); | |||
| 327 | Py_CLEAR(ret)do { PyObject *_py_tmp = ((PyObject*)(ret)); if (_py_tmp != ( (void*)0)) { (ret) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 328 | return concat; | |||
| 329 | } | |||
| 330 | if (PyList_GET_SIZE(obj)((((PyVarObject*)(obj))->ob_size)) == 0) { | |||
| 331 | /* No items, so fail. Or should we guess "av"? */ | |||
| 332 | PyErr_SetString(PyExc_ValueError, "Unable to guess signature " | |||
| 333 | "from an empty list"); | |||
| 334 | return NULL((void*)0); | |||
| 335 | } | |||
| 336 | tmp = PyList_GetItem(obj, 0); | |||
| 337 | tmp = _signature_string_from_pyobject(tmp, NULL((void*)0)); | |||
| 338 | if (!tmp) return NULL((void*)0); | |||
| 339 | { | |||
| 340 | PyObject *concat = PyUnicode_Concat(ret, tmp); | |||
| 341 | Py_CLEAR(ret)do { PyObject *_py_tmp = ((PyObject*)(ret)); if (_py_tmp != ( (void*)0)) { (ret) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 342 | Py_CLEAR(tmp)do { PyObject *_py_tmp = ((PyObject*)(tmp)); if (_py_tmp != ( (void*)0)) { (tmp) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 343 | return concat; | |||
| 344 | } | |||
| 345 | } | |||
| 346 | else if (PyDict_Check(obj)((((((PyObject*)(obj))->ob_type))->tp_flags & ((1UL << 29))) != 0)) { | |||
| 347 | PyObject *key, *value, *keysig, *valuesig; | |||
| 348 | Py_ssize_t pos = 0; | |||
| 349 | PyObject *ret = NULL((void*)0); | |||
| 350 | ||||
| 351 | if (DBusPyDict_Check(obj) && | |||
| 352 | PyUnicode_Check(((DBusPyDict *)obj)->signature)((((((PyObject*)(((DBusPyDict *)obj)->signature))->ob_type ))->tp_flags & ((1UL << 28))) != 0)) | |||
| 353 | { | |||
| 354 | return PyUnicode_FromFormat((DBUS_TYPE_ARRAY_AS_STRING"a" | |||
| 355 | DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING"{" | |||
| 356 | "%U" | |||
| 357 | DBUS_DICT_ENTRY_END_CHAR_AS_STRING"}"), | |||
| 358 | ((DBusPyDict *)obj)->signature); | |||
| 359 | } | |||
| 360 | if (!PyDict_Next(obj, &pos, &key, &value)) { | |||
| 361 | /* No items, so fail. Or should we guess "a{vv}"? */ | |||
| 362 | PyErr_SetString(PyExc_ValueError, "Unable to guess signature " | |||
| 363 | "from an empty dict"); | |||
| 364 | return NULL((void*)0); | |||
| 365 | } | |||
| 366 | keysig = _signature_string_from_pyobject(key, NULL((void*)0)); | |||
| 367 | valuesig = _signature_string_from_pyobject(value, NULL((void*)0)); | |||
| 368 | if (keysig && valuesig) { | |||
| 369 | ret = PyUnicode_FromFormat((DBUS_TYPE_ARRAY_AS_STRING"a" | |||
| 370 | DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING"{" | |||
| 371 | "%U%U" | |||
| 372 | DBUS_DICT_ENTRY_END_CHAR_AS_STRING"}"), | |||
| 373 | keysig, valuesig); | |||
| 374 | } | |||
| 375 | Py_CLEAR(keysig)do { PyObject *_py_tmp = ((PyObject*)(keysig)); if (_py_tmp != ((void*)0)) { (keysig) = ((void*)0); _Py_DECREF(((PyObject*) (_py_tmp))); } } while (0); | |||
| 376 | Py_CLEAR(valuesig)do { PyObject *_py_tmp = ((PyObject*)(valuesig)); if (_py_tmp != ((void*)0)) { (valuesig) = ((void*)0); _Py_DECREF(((PyObject *)(_py_tmp))); } } while (0); | |||
| 377 | return ret; | |||
| 378 | } | |||
| 379 | else { | |||
| 380 | PyErr_Format(PyExc_TypeError, "Don't know which D-Bus type " | |||
| 381 | "to use to encode type \"%s\"", | |||
| 382 | Py_TYPE(obj)(((PyObject*)(obj))->ob_type)->tp_name); | |||
| 383 | return NULL((void*)0); | |||
| 384 | } | |||
| 385 | } | |||
| 386 | ||||
| 387 | PyObject * | |||
| 388 | dbus_py_Message_guess_signature(PyObject *unused UNUSED__attribute__((__unused__)), PyObject *args) | |||
| 389 | { | |||
| 390 | PyObject *tmp, *ret = NULL((void*)0); | |||
| 391 | ||||
| 392 | if (!args) { | |||
| 393 | if (!PyErr_Occurred()) { | |||
| 394 | PyErr_BadInternalCall()_PyErr_BadInternalCall("../../dbus_bindings/message-append.c" , 394); | |||
| 395 | } | |||
| 396 | return NULL((void*)0); | |||
| 397 | } | |||
| 398 | ||||
| 399 | #ifdef USING_DBG | |||
| 400 | fprintf(stderrstderr, "DBG/%ld: called Message_guess_signature", (long)getpid()); | |||
| 401 | PyObject_Print(args, stderrstderr, 0); | |||
| 402 | fprintf(stderrstderr, "\n"); | |||
| 403 | #endif | |||
| 404 | ||||
| 405 | if (!PyTuple_Check(args)((((((PyObject*)(args))->ob_type))->tp_flags & ((1UL << 26))) != 0)) { | |||
| 406 | DBG("%s", "Message_guess_signature: args not a tuple")do {} while (0); | |||
| 407 | PyErr_BadInternalCall()_PyErr_BadInternalCall("../../dbus_bindings/message-append.c" , 407); | |||
| 408 | return NULL((void*)0); | |||
| 409 | } | |||
| 410 | ||||
| 411 | /* if there were no args, easy */ | |||
| 412 | if (PyTuple_GET_SIZE(args)(((PyVarObject*)(((PyTupleObject *)(args))))->ob_size) == 0) { | |||
| 413 | DBG("%s", "Message_guess_signature: no args, so return Signature('')")do {} while (0); | |||
| 414 | return PyObject_CallFunction_PyObject_CallFunction_SizeT((PyObject *)&DBusPySignature_Type, "(s)", ""); | |||
| ||||
| 415 | } | |||
| 416 | ||||
| 417 | /* if there were args, the signature we want is, by construction, | |||
| 418 | * exactly the signature we get for the tuple args, except that we don't | |||
| 419 | * want the parentheses. */ | |||
| 420 | tmp = _signature_string_from_pyobject(args, NULL((void*)0)); | |||
| 421 | if (!tmp) { | |||
| 422 | DBG("%s", "Message_guess_signature: failed")do {} while (0); | |||
| 423 | return NULL((void*)0); | |||
| 424 | } | |||
| 425 | if (PyUnicode_Check(tmp)((((((PyObject*)(tmp))->ob_type))->tp_flags & ((1UL << 28))) != 0)) { | |||
| 426 | PyObject *as_bytes = PyUnicode_AsUTF8String(tmp); | |||
| 427 | Py_CLEAR(tmp)do { PyObject *_py_tmp = ((PyObject*)(tmp)); if (_py_tmp != ( (void*)0)) { (tmp) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 428 | if (!as_bytes) | |||
| 429 | return NULL((void*)0); | |||
| 430 | if (PyBytes_GET_SIZE(as_bytes)((((PyVarObject*)(as_bytes))->ob_size)) < 2) { | |||
| 431 | PyErr_SetString(PyExc_RuntimeError, "Internal error: " | |||
| 432 | "_signature_string_from_pyobject returned " | |||
| 433 | "a bad result"); | |||
| 434 | Py_CLEAR(as_bytes)do { PyObject *_py_tmp = ((PyObject*)(as_bytes)); if (_py_tmp != ((void*)0)) { (as_bytes) = ((void*)0); _Py_DECREF(((PyObject *)(_py_tmp))); } } while (0); | |||
| 435 | return NULL((void*)0); | |||
| 436 | } | |||
| 437 | tmp = as_bytes; | |||
| 438 | } | |||
| 439 | if (!PyBytes_Check(tmp)((((((PyObject*)(tmp))->ob_type))->tp_flags & ((1UL << 27))) != 0) || PyBytes_GET_SIZE(tmp)((((PyVarObject*)(tmp))->ob_size)) < 2) { | |||
| 440 | PyErr_SetString(PyExc_RuntimeError, "Internal error: " | |||
| 441 | "_signature_string_from_pyobject returned " | |||
| 442 | "a bad result"); | |||
| 443 | Py_CLEAR(tmp)do { PyObject *_py_tmp = ((PyObject*)(tmp)); if (_py_tmp != ( (void*)0)) { (tmp) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 444 | return NULL((void*)0); | |||
| 445 | } | |||
| 446 | ret = PyObject_CallFunction_PyObject_CallFunction_SizeT((PyObject *)&DBusPySignature_Type, "(s#)", | |||
| 447 | PyBytes_AS_STRING(tmp)((((PyBytesObject *)(tmp))->ob_sval)) + 1, | |||
| 448 | PyBytes_GET_SIZE(tmp)((((PyVarObject*)(tmp))->ob_size)) - 2); | |||
| 449 | Py_CLEAR(tmp)do { PyObject *_py_tmp = ((PyObject*)(tmp)); if (_py_tmp != ( (void*)0)) { (tmp) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 450 | return ret; | |||
| 451 | } | |||
| 452 | ||||
| 453 | static int _message_iter_append_pyobject(DBusMessageIter *appender, | |||
| 454 | DBusSignatureIter *sig_iter, | |||
| 455 | PyObject *obj, | |||
| 456 | dbus_bool_t *more); | |||
| 457 | static int _message_iter_append_variant(DBusMessageIter *appender, | |||
| 458 | PyObject *obj); | |||
| 459 | ||||
| 460 | static int | |||
| 461 | _message_iter_append_string(DBusMessageIter *appender, | |||
| 462 | int sig_type, PyObject *obj, | |||
| 463 | dbus_bool_t allow_object_path_attr) | |||
| 464 | { | |||
| 465 | char *s; | |||
| 466 | PyObject *utf8; | |||
| 467 | ||||
| 468 | if (sig_type == DBUS_TYPE_OBJECT_PATH((int) 'o') && allow_object_path_attr) { | |||
| 469 | PyObject *object_path = get_object_path (obj); | |||
| 470 | ||||
| 471 | if (object_path == Py_None(&_Py_NoneStruct)) { | |||
| 472 | Py_CLEAR(object_path)do { PyObject *_py_tmp = ((PyObject*)(object_path)); if (_py_tmp != ((void*)0)) { (object_path) = ((void*)0); _Py_DECREF(((PyObject *)(_py_tmp))); } } while (0); | |||
| 473 | } | |||
| 474 | else if (!object_path) { | |||
| 475 | return -1; | |||
| 476 | } | |||
| 477 | else { | |||
| 478 | int ret = _message_iter_append_string(appender, sig_type, | |||
| 479 | object_path, FALSE0); | |||
| 480 | Py_CLEAR(object_path)do { PyObject *_py_tmp = ((PyObject*)(object_path)); if (_py_tmp != ((void*)0)) { (object_path) = ((void*)0); _Py_DECREF(((PyObject *)(_py_tmp))); } } while (0); | |||
| 481 | return ret; | |||
| 482 | } | |||
| 483 | } | |||
| 484 | ||||
| 485 | if (PyBytes_Check(obj)((((((PyObject*)(obj))->ob_type))->tp_flags & ((1UL << 27))) != 0)) { | |||
| 486 | utf8 = obj; | |||
| 487 | Py_INCREF(obj)_Py_INCREF(((PyObject*)(obj))); | |||
| 488 | } | |||
| 489 | else if (PyUnicode_Check(obj)((((((PyObject*)(obj))->ob_type))->tp_flags & ((1UL << 28))) != 0)) { | |||
| 490 | utf8 = PyUnicode_AsUTF8String(obj); | |||
| 491 | if (!utf8) return -1; | |||
| 492 | } | |||
| 493 | else { | |||
| 494 | PyErr_SetString(PyExc_TypeError, | |||
| 495 | "Expected a string or unicode object"); | |||
| 496 | return -1; | |||
| 497 | } | |||
| 498 | ||||
| 499 | /* Raise TypeError if the string has embedded NULs */ | |||
| 500 | if (PyBytes_AsStringAndSize(utf8, &s, NULL((void*)0)) < 0) | |||
| 501 | return -1; | |||
| 502 | ||||
| 503 | /* Validate UTF-8, strictly */ | |||
| 504 | if (!dbus_validate_utf8(s, NULL((void*)0))) { | |||
| 505 | PyErr_SetString(PyExc_UnicodeError, "String parameters " | |||
| 506 | "to be sent over D-Bus must be valid UTF-8 " | |||
| 507 | "with no noncharacter code points"); | |||
| 508 | return -1; | |||
| 509 | } | |||
| 510 | ||||
| 511 | DBG("Performing actual append: string (from unicode) %s", s)do {} while (0); | |||
| 512 | if (!dbus_message_iter_append_basic(appender, sig_type, &s)) { | |||
| 513 | Py_CLEAR(utf8)do { PyObject *_py_tmp = ((PyObject*)(utf8)); if (_py_tmp != ( (void*)0)) { (utf8) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 514 | PyErr_NoMemory(); | |||
| 515 | return -1; | |||
| 516 | } | |||
| 517 | ||||
| 518 | Py_CLEAR(utf8)do { PyObject *_py_tmp = ((PyObject*)(utf8)); if (_py_tmp != ( (void*)0)) { (utf8) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 519 | return 0; | |||
| 520 | } | |||
| 521 | ||||
| 522 | static int | |||
| 523 | _message_iter_append_byte(DBusMessageIter *appender, PyObject *obj) | |||
| 524 | { | |||
| 525 | unsigned char y; | |||
| 526 | ||||
| 527 | if (PyBytes_Check(obj)((((((PyObject*)(obj))->ob_type))->tp_flags & ((1UL << 27))) != 0)) { | |||
| 528 | if (PyBytes_GET_SIZE(obj)((((PyVarObject*)(obj))->ob_size)) != 1) { | |||
| 529 | PyErr_Format(PyExc_ValueError, | |||
| 530 | "Expected a length-1 bytes but found %d bytes", | |||
| 531 | (int)PyBytes_GET_SIZE(obj)((((PyVarObject*)(obj))->ob_size))); | |||
| 532 | return -1; | |||
| 533 | } | |||
| 534 | y = *(unsigned char *)PyBytes_AS_STRING(obj)((((PyBytesObject *)(obj))->ob_sval)); | |||
| 535 | } | |||
| 536 | else { | |||
| 537 | /* on Python 2 this accepts either int or long */ | |||
| 538 | long i = PyLong_AsLong(obj); | |||
| 539 | ||||
| 540 | if (i == -1 && PyErr_Occurred()) return -1; | |||
| 541 | if (i < 0 || i > 0xff) { | |||
| 542 | PyErr_Format(PyExc_ValueError, | |||
| 543 | "%d outside range for a byte value", | |||
| 544 | (int)i); | |||
| 545 | return -1; | |||
| 546 | } | |||
| 547 | y = i; | |||
| 548 | } | |||
| 549 | DBG("Performing actual append: byte \\x%02x", (unsigned)y)do {} while (0); | |||
| 550 | if (!dbus_message_iter_append_basic(appender, DBUS_TYPE_BYTE((int) 'y'), &y)) { | |||
| 551 | PyErr_NoMemory(); | |||
| 552 | return -1; | |||
| 553 | } | |||
| 554 | return 0; | |||
| 555 | } | |||
| 556 | ||||
| 557 | static dbus_bool_t | |||
| 558 | dbuspy_message_iter_close_container(DBusMessageIter *iter, | |||
| 559 | DBusMessageIter *sub, | |||
| 560 | dbus_bool_t is_ok) | |||
| 561 | { | |||
| 562 | if (!is_ok) { | |||
| 563 | dbus_message_iter_abandon_container(iter, sub); | |||
| 564 | return TRUE1; | |||
| 565 | } | |||
| 566 | return dbus_message_iter_close_container(iter, sub); | |||
| 567 | } | |||
| 568 | ||||
| 569 | #if defined(DBUS_TYPE_UNIX_FD((int) 'h')) | |||
| 570 | static int | |||
| 571 | _message_iter_append_unixfd(DBusMessageIter *appender, PyObject *obj) | |||
| 572 | { | |||
| 573 | int fd; | |||
| 574 | long original_fd; | |||
| 575 | ||||
| 576 | if (PyLong_Check(obj)((((((PyObject*)(obj))->ob_type))->tp_flags & ((1UL << 24))) != 0)) | |||
| 577 | { | |||
| 578 | /* on Python 2 this accepts either int or long */ | |||
| 579 | original_fd = PyLong_AsLong(obj); | |||
| 580 | if (original_fd == -1 && PyErr_Occurred()) | |||
| 581 | return -1; | |||
| 582 | if (original_fd < INT_MIN(-2147483647 -1) || original_fd > INT_MAX2147483647) { | |||
| 583 | PyErr_Format(PyExc_ValueError, "out of int range: %ld", | |||
| 584 | original_fd); | |||
| 585 | return -1; | |||
| 586 | } | |||
| 587 | fd = (int)original_fd; | |||
| 588 | } | |||
| 589 | else if (PyObject_IsInstance(obj, (PyObject*) &DBusPyUnixFd_Type)) { | |||
| 590 | fd = dbus_py_unix_fd_get_fd(obj); | |||
| 591 | } | |||
| 592 | else { | |||
| 593 | return -1; | |||
| 594 | } | |||
| 595 | ||||
| 596 | DBG("Performing actual append: fd %d", fd)do {} while (0); | |||
| 597 | if (!dbus_message_iter_append_basic(appender, DBUS_TYPE_UNIX_FD((int) 'h'), &fd)) { | |||
| 598 | PyErr_NoMemory(); | |||
| 599 | return -1; | |||
| 600 | } | |||
| 601 | return 0; | |||
| 602 | } | |||
| 603 | #endif | |||
| 604 | ||||
| 605 | static int | |||
| 606 | _message_iter_append_dictentry(DBusMessageIter *appender, | |||
| 607 | DBusSignatureIter *sig_iter, | |||
| 608 | PyObject *dict, PyObject *key) | |||
| 609 | { | |||
| 610 | DBusSignatureIter sub_sig_iter; | |||
| 611 | DBusMessageIter sub; | |||
| 612 | int ret = -1; | |||
| 613 | PyObject *value = PyObject_GetItem(dict, key); | |||
| 614 | dbus_bool_t more; | |||
| 615 | ||||
| 616 | if (!value) return -1; | |||
| 617 | ||||
| 618 | #ifdef USING_DBG | |||
| 619 | fprintf(stderrstderr, "Append dictentry: "); | |||
| 620 | PyObject_Print(key, stderrstderr, 0); | |||
| 621 | fprintf(stderrstderr, " => "); | |||
| 622 | PyObject_Print(value, stderrstderr, 0); | |||
| 623 | fprintf(stderrstderr, "\n"); | |||
| 624 | #endif | |||
| 625 | ||||
| 626 | DBG("Recursing signature iterator %p -> %p", sig_iter, &sub_sig_iter)do {} while (0); | |||
| 627 | dbus_signature_iter_recurse(sig_iter, &sub_sig_iter); | |||
| 628 | #ifdef USING_DBG | |||
| 629 | { | |||
| 630 | char *s; | |||
| 631 | s = dbus_signature_iter_get_signature(sig_iter); | |||
| 632 | DBG("Signature of parent iterator %p is %s", sig_iter, s)do {} while (0); | |||
| 633 | dbus_free(s); | |||
| 634 | s = dbus_signature_iter_get_signature(&sub_sig_iter); | |||
| 635 | DBG("Signature of sub-iterator %p is %s", &sub_sig_iter, s)do {} while (0); | |||
| 636 | dbus_free(s); | |||
| 637 | } | |||
| 638 | #endif | |||
| 639 | ||||
| 640 | DBG("%s", "Opening DICT_ENTRY container")do {} while (0); | |||
| 641 | if (!dbus_message_iter_open_container(appender, DBUS_TYPE_DICT_ENTRY((int) 'e'), | |||
| 642 | NULL((void*)0), &sub)) { | |||
| 643 | PyErr_NoMemory(); | |||
| 644 | goto out; | |||
| 645 | } | |||
| 646 | ret = _message_iter_append_pyobject(&sub, &sub_sig_iter, key, &more); | |||
| 647 | if (ret == 0) { | |||
| 648 | ret = _message_iter_append_pyobject(&sub, &sub_sig_iter, value, &more); | |||
| 649 | } | |||
| 650 | DBG("%s", "Closing DICT_ENTRY container")do {} while (0); | |||
| 651 | if (!dbuspy_message_iter_close_container(appender, &sub, (ret == 0))) { | |||
| 652 | PyErr_NoMemory(); | |||
| 653 | ret = -1; | |||
| 654 | } | |||
| 655 | out: | |||
| 656 | Py_CLEAR(value)do { PyObject *_py_tmp = ((PyObject*)(value)); if (_py_tmp != ((void*)0)) { (value) = ((void*)0); _Py_DECREF(((PyObject*)( _py_tmp))); } } while (0); | |||
| 657 | return ret; | |||
| 658 | } | |||
| 659 | ||||
| 660 | static int | |||
| 661 | _message_iter_append_multi(DBusMessageIter *appender, | |||
| 662 | const DBusSignatureIter *sig_iter, | |||
| 663 | int mode, PyObject *obj) | |||
| 664 | { | |||
| 665 | DBusMessageIter sub_appender; | |||
| 666 | DBusSignatureIter sub_sig_iter; | |||
| 667 | PyObject *contents; | |||
| 668 | int ret; | |||
| 669 | PyObject *iterator = PyObject_GetIter(obj); | |||
| 670 | char *sig = NULL((void*)0); | |||
| 671 | int container = mode; | |||
| 672 | dbus_bool_t is_byte_array = DBusPyByteArray_Check(obj); | |||
| 673 | int inner_type; | |||
| 674 | dbus_bool_t more; | |||
| 675 | ||||
| 676 | assert(mode == DBUS_TYPE_DICT_ENTRY || mode == DBUS_TYPE_ARRAY ||((void) sizeof ((mode == ((int) 'e') || mode == ((int) 'a') || mode == ((int) 'r')) ? 1 : 0), __extension__ ({ if (mode == ( (int) 'e') || mode == ((int) 'a') || mode == ((int) 'r')) ; else __assert_fail ("mode == DBUS_TYPE_DICT_ENTRY || mode == DBUS_TYPE_ARRAY || mode == DBUS_TYPE_STRUCT" , "../../dbus_bindings/message-append.c", 677, __extension__ __PRETTY_FUNCTION__ ); })) | |||
| 677 | mode == DBUS_TYPE_STRUCT)((void) sizeof ((mode == ((int) 'e') || mode == ((int) 'a') || mode == ((int) 'r')) ? 1 : 0), __extension__ ({ if (mode == ( (int) 'e') || mode == ((int) 'a') || mode == ((int) 'r')) ; else __assert_fail ("mode == DBUS_TYPE_DICT_ENTRY || mode == DBUS_TYPE_ARRAY || mode == DBUS_TYPE_STRUCT" , "../../dbus_bindings/message-append.c", 677, __extension__ __PRETTY_FUNCTION__ ); })); | |||
| 678 | ||||
| 679 | #ifdef USING_DBG | |||
| 680 | fprintf(stderrstderr, "Appending multiple: "); | |||
| 681 | PyObject_Print(obj, stderrstderr, 0); | |||
| 682 | fprintf(stderrstderr, "\n"); | |||
| 683 | #endif | |||
| 684 | ||||
| 685 | if (!iterator) return -1; | |||
| 686 | if (mode == DBUS_TYPE_DICT_ENTRY((int) 'e')) container = DBUS_TYPE_ARRAY((int) 'a'); | |||
| 687 | ||||
| 688 | DBG("Recursing signature iterator %p -> %p", sig_iter, &sub_sig_iter)do {} while (0); | |||
| 689 | dbus_signature_iter_recurse(sig_iter, &sub_sig_iter); | |||
| 690 | #ifdef USING_DBG | |||
| 691 | { | |||
| 692 | char *s; | |||
| 693 | s = dbus_signature_iter_get_signature(sig_iter); | |||
| 694 | DBG("Signature of parent iterator %p is %s", sig_iter, s)do {} while (0); | |||
| 695 | dbus_free(s); | |||
| 696 | s = dbus_signature_iter_get_signature(&sub_sig_iter); | |||
| 697 | DBG("Signature of sub-iterator %p is %s", &sub_sig_iter, s)do {} while (0); | |||
| 698 | dbus_free(s); | |||
| 699 | } | |||
| 700 | #endif | |||
| 701 | inner_type = dbus_signature_iter_get_current_type(&sub_sig_iter); | |||
| 702 | ||||
| 703 | if (mode == DBUS_TYPE_ARRAY((int) 'a') || mode == DBUS_TYPE_DICT_ENTRY((int) 'e')) { | |||
| 704 | sig = dbus_signature_iter_get_signature(&sub_sig_iter); | |||
| 705 | if (!sig) { | |||
| 706 | PyErr_NoMemory(); | |||
| 707 | ret = -1; | |||
| 708 | goto out; | |||
| 709 | } | |||
| 710 | } | |||
| 711 | /* else leave sig set to NULL. */ | |||
| 712 | ||||
| 713 | DBG("Opening '%c' container", container)do {} while (0); | |||
| 714 | if (!dbus_message_iter_open_container(appender, container, | |||
| 715 | sig, &sub_appender)) { | |||
| 716 | PyErr_NoMemory(); | |||
| 717 | ret = -1; | |||
| 718 | goto out; | |||
| 719 | } | |||
| 720 | ret = 0; | |||
| 721 | more = TRUE1; | |||
| 722 | while ((contents = PyIter_Next(iterator))) { | |||
| 723 | ||||
| 724 | if (mode == DBUS_TYPE_ARRAY((int) 'a') || mode == DBUS_TYPE_DICT_ENTRY((int) 'e')) { | |||
| 725 | DBG("Recursing signature iterator %p -> %p", sig_iter, &sub_sig_iter)do {} while (0); | |||
| 726 | dbus_signature_iter_recurse(sig_iter, &sub_sig_iter); | |||
| 727 | #ifdef USING_DBG | |||
| 728 | { | |||
| 729 | char *s; | |||
| 730 | s = dbus_signature_iter_get_signature(sig_iter); | |||
| 731 | DBG("Signature of parent iterator %p is %s", sig_iter, s)do {} while (0); | |||
| 732 | dbus_free(s); | |||
| 733 | s = dbus_signature_iter_get_signature(&sub_sig_iter); | |||
| 734 | DBG("Signature of sub-iterator %p is %s", &sub_sig_iter, s)do {} while (0); | |||
| 735 | dbus_free(s); | |||
| 736 | } | |||
| 737 | #endif | |||
| 738 | } | |||
| 739 | else /* struct */ { | |||
| 740 | if (!more) { | |||
| 741 | PyErr_Format(PyExc_TypeError, "Fewer items found in struct's " | |||
| 742 | "D-Bus signature than in Python arguments "); | |||
| 743 | ret = -1; | |||
| 744 | break; | |||
| 745 | } | |||
| 746 | } | |||
| 747 | ||||
| 748 | if (mode == DBUS_TYPE_DICT_ENTRY((int) 'e')) { | |||
| 749 | ret = _message_iter_append_dictentry(&sub_appender, &sub_sig_iter, | |||
| 750 | obj, contents); | |||
| 751 | } | |||
| 752 | else if (mode == DBUS_TYPE_ARRAY((int) 'a') && is_byte_array | |||
| 753 | && inner_type == DBUS_TYPE_VARIANT((int) 'v')) { | |||
| 754 | /* Subscripting a ByteArray gives a str of length 1, but if the | |||
| 755 | * container is a ByteArray and the parameter is an array of | |||
| 756 | * variants, we want to produce an array of variants containing | |||
| 757 | * bytes, not strings. | |||
| 758 | */ | |||
| 759 | PyObject *args = Py_BuildValue_Py_BuildValue_SizeT("(O)", contents); | |||
| 760 | PyObject *byte; | |||
| 761 | ||||
| 762 | if (!args) | |||
| 763 | break; | |||
| 764 | byte = PyObject_Call((PyObject *)&DBusPyByte_Type, args, NULL((void*)0)); | |||
| 765 | Py_CLEAR(args)do { PyObject *_py_tmp = ((PyObject*)(args)); if (_py_tmp != ( (void*)0)) { (args) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 766 | if (!byte) | |||
| 767 | break; | |||
| 768 | ret = _message_iter_append_variant(&sub_appender, byte); | |||
| 769 | Py_CLEAR(byte)do { PyObject *_py_tmp = ((PyObject*)(byte)); if (_py_tmp != ( (void*)0)) { (byte) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0); | |||
| 770 | } | |||
| 771 | else { | |||
| 772 | /* advances sub_sig_iter and sets more on success - for array | |||
| 773 | * this doesn't matter, for struct it's essential */ | |||
| 774 | ret = _message_iter_append_pyobject(&sub_appender, &sub_sig_iter, | |||
| 775 | contents, &more); | |||
| 776 | } | |||
| 777 | ||||
| 778 | Py_CLEAR(contents)do { PyObject *_py_tmp = ((PyObject*)(contents)); if (_py_tmp != ((void*)0)) { (contents) = ((void*)0); _Py_DECREF(((PyObject *)(_py_tmp))); } } while (0); | |||
| 779 | if (ret < 0) { | |||
| 780 | break; | |||
| 781 | } | |||
| 782 | } | |||
| 783 | ||||
| 784 | if (PyErr_Occurred()) { | |||
| 785 | ret = -1; | |||
| 786 | } | |||
| 787 | else if (mode == DBUS_TYPE_STRUCT((int) 'r') && more) { | |||
| 788 | PyErr_Format(PyExc_TypeError, "More items found in struct's D-Bus " | |||
| 789 | "signature than in Python arguments "); | |||
| 790 | ret = -1; | |||
| 791 | } | |||
| 792 | ||||
| 793 | /* This must be run as cleanup, even on failure. */ | |||
| 794 | DBG("Closing '%c' container", container)do {} while (0); | |||
| 795 | if (!dbuspy_message_iter_close_container(appender, &sub_appender, (ret == 0))) { | |||
| 796 | PyErr_NoMemory(); | |||
| 797 | ret = -1; | |||
| 798 | } | |||
| 799 | ||||
| 800 | out: | |||
| 801 | Py_CLEAR(iterator)do { PyObject *_py_tmp = ((PyObject*)(iterator)); if (_py_tmp != ((void*)0)) { (iterator) = ((void*)0); _Py_DECREF(((PyObject *)(_py_tmp))); } } while (0); | |||
| 802 | dbus_free(sig); | |||
| 803 | return ret; | |||
| 804 | } | |||
| 805 | ||||
| 806 | static int | |||
| 807 | _message_iter_append_string_as_byte_array(DBusMessageIter *appender, | |||
| 808 | PyObject *obj) | |||
| 809 | { | |||
| 810 | /* a bit of a faster path for byte arrays that are strings */ | |||
| 811 | Py_ssize_t len = PyBytes_GET_SIZE(obj)((((PyVarObject*)(obj))->ob_size)); | |||
| 812 | const char *s; | |||
| 813 | DBusMessageIter sub; | |||
| 814 | int ret; | |||
| 815 | ||||
| 816 | s = PyBytes_AS_STRING(obj)((((PyBytesObject *)(obj))->ob_sval)); | |||
| 817 | DBG("%s", "Opening ARRAY container")do {} while (0); | |||
| 818 | if (!dbus_message_iter_open_container(appender, DBUS_TYPE_ARRAY((int) 'a'), | |||
| 819 | DBUS_TYPE_BYTE_AS_STRING"y", &sub)) { | |||
| 820 | PyErr_NoMemory(); | |||
| 821 | return -1; | |||
| 822 | } | |||
| 823 | DBG("Appending fixed array of %d bytes", (int)len)do {} while (0); | |||
| 824 | if (dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE((int) 'y'), &s, len)) { | |||
| 825 | ret = 0; | |||
| 826 | } | |||
| 827 | else { | |||
| 828 | PyErr_NoMemory(); | |||
| 829 | ret = -1; | |||
| 830 | } | |||
| 831 | DBG("%s", "Closing ARRAY container")do {} while (0); | |||
| 832 | if (!dbus_message_iter_close_container(appender, &sub)) { | |||
| 833 | PyErr_NoMemory(); | |||
| 834 | return -1; | |||
| 835 | } | |||
| 836 | return ret; | |||
| 837 | } | |||
| 838 | ||||
| 839 | /* Encode some Python object into a D-Bus variant slot. */ | |||
| 840 | static int | |||
| 841 | _message_iter_append_variant(DBusMessageIter *appender, PyObject *obj) | |||
| 842 | { | |||
| 843 | DBusSignatureIter obj_sig_iter; | |||
| 844 | const char *obj_sig_str; | |||
| 845 | PyObject *obj_sig; | |||
| 846 | int ret; | |||
| 847 | long variant_level; | |||
| 848 | dbus_bool_t dummy; | |||
| 849 | DBusMessageIter *variant_iters = NULL((void*)0); | |||
| 850 | ||||
| 851 | /* Separate the object into the contained object, and the number of | |||
| 852 | * variants it's wrapped in. */ | |||
| 853 | obj_sig = _signature_string_from_pyobject(obj, &variant_level); | |||
| 854 | if (!obj_sig) return -1; | |||
| 855 | ||||
| 856 | if (PyUnicode_Check(obj_sig)((((((PyObject*)(obj_sig))->ob_type))->tp_flags & ( (1UL << 28))) != 0)) { | |||
| 857 | PyObject *obj_sig_as_bytes = PyUnicode_AsUTF8String(obj_sig); | |||
| 858 | Py_CLEAR(obj_sig)do { PyObject *_py_tmp = ((PyObject*)(obj_sig)); if (_py_tmp != ((void*)0)) { (obj_sig) = ((void*)0); _Py_DECREF(((PyObject* )(_py_tmp))); } } while (0); | |||
| 859 | if (!obj_sig_as_bytes) | |||
| 860 | return -1; | |||
| 861 | obj_sig = obj_sig_as_bytes; | |||
| 862 | } | |||
| 863 | obj_sig_str = PyBytes_AsString(obj_sig); | |||
| 864 | if (!obj_sig_str) { | |||
| 865 | Py_CLEAR(obj_sig)do { PyObject *_py_tmp = ((PyObject*)(obj_sig)); if (_py_tmp != ((void*)0)) { (obj_sig) = ((void*)0); _Py_DECREF(((PyObject* )(_py_tmp))); } } while (0); | |||
| 866 | return -1; | |||
| 867 | } | |||
| 868 | ||||
| 869 | if (variant_level < 1) { | |||
| 870 | variant_level = 1; | |||
| 871 | } | |||
| 872 | ||||
| 873 | dbus_signature_iter_init(&obj_sig_iter, obj_sig_str); | |||
| 874 | ||||
| 875 | { | |||
| 876 | long i; | |||
| 877 | ||||
| 878 | variant_iters = calloc (variant_level, sizeof (DBusMessageIter)); | |||
| 879 | ||||
| 880 | if (!variant_iters) { | |||
| 881 | PyErr_NoMemory(); | |||
| 882 | ret = -1; | |||
| 883 | goto out; | |||
| 884 | } | |||
| 885 | ||||
| 886 | for (i = 0; i < variant_level; i++) { | |||
| 887 | DBusMessageIter *child = &variant_iters[i]; | |||
| 888 | /* The first is a special case: its parent is the iter passed in | |||
| 889 | * to this function, instead of being the previous one in the | |||
| 890 | * stack | |||
| 891 | */ | |||
| 892 | DBusMessageIter *parent = (i == 0 | |||
| 893 | ? appender | |||
| 894 | : &(variant_iters[i-1])); | |||
| 895 | /* The last is also a special case: it contains the actual | |||
| 896 | * object, rather than another variant | |||
| 897 | */ | |||
| 898 | const char *sig_str = (i == variant_level-1 | |||
| 899 | ? obj_sig_str | |||
| 900 | : DBUS_TYPE_VARIANT_AS_STRING"v"); | |||
| 901 | ||||
| 902 | DBG("Opening VARIANT container %p inside %p containing '%s'",do {} while (0) | |||
| 903 | child, parent, sig_str)do {} while (0); | |||
| 904 | if (!dbus_message_iter_open_container(parent, DBUS_TYPE_VARIANT((int) 'v'), | |||
| 905 | sig_str, child)) { | |||
| 906 | PyErr_NoMemory(); | |||
| 907 | ret = -1; | |||
| 908 | goto out; | |||
| 909 | } | |||
| 910 | } | |||
| 911 | ||||
| 912 | /* Put the object itself into the innermost variant */ | |||
| 913 | ret = _message_iter_append_pyobject(&variant_iters[variant_level-1], | |||
| 914 | &obj_sig_iter, obj, &dummy); | |||
| 915 | ||||
| 916 | /* here we rely on i (and variant_level) being a signed long */ | |||
| 917 | for (i = variant_level - 1; i >= 0; i--) { | |||
| 918 | DBusMessageIter *child = &variant_iters[i]; | |||
| 919 | /* The first is a special case: its parent is the iter passed in | |||
| 920 | * to this function, instead of being the previous one in the | |||
| 921 | * stack | |||
| 922 | */ | |||
| 923 | DBusMessageIter *parent = (i == 0 ? appender | |||
| 924 | : &(variant_iters[i-1])); | |||
| 925 | ||||
| 926 | DBG("Closing VARIANT container %p inside %p", child, parent)do {} while (0); | |||
| 927 | if (!dbus_message_iter_close_container(parent, child)) { | |||
| 928 | PyErr_NoMemory(); | |||
| 929 | ret = -1; | |||
| 930 | goto out; | |||
| 931 | } | |||
| 932 | } | |||
| 933 | } | |||
| 934 | ||||
| 935 | out: | |||
| 936 | if (variant_iters != NULL((void*)0)) | |||
| 937 | free (variant_iters); | |||
| 938 | ||||
| 939 | Py_CLEAR(obj_sig)do { PyObject *_py_tmp = ((PyObject*)(obj_sig)); if (_py_tmp != ((void*)0)) { (obj_sig) = ((void*)0); _Py_DECREF(((PyObject* )(_py_tmp))); } } while (0); | |||
| 940 | return ret; | |||
| 941 | } | |||
| 942 | ||||
| 943 | /* On success, *more is set to whether there's more in the signature. */ | |||
| 944 | static int | |||
| 945 | _message_iter_append_pyobject(DBusMessageIter *appender, | |||
| 946 | DBusSignatureIter *sig_iter, | |||
| 947 | PyObject *obj, | |||
| 948 | dbus_bool_t *more) | |||
| 949 | { | |||
| 950 | int sig_type = dbus_signature_iter_get_current_type(sig_iter); | |||
| 951 | DBusBasicValue u; | |||
| 952 | int ret = -1; | |||
| 953 | ||||
| 954 | #ifdef USING_DBG | |||
| 955 | fprintf(stderrstderr, "Appending object at %p: ", obj); | |||
| 956 | PyObject_Print(obj, stderrstderr, 0); | |||
| 957 | fprintf(stderrstderr, " into appender at %p, dbus wants type %c\n", | |||
| 958 | appender, sig_type); | |||
| 959 | #endif | |||
| 960 | ||||
| 961 | switch (sig_type) { | |||
| 962 | /* The numeric types are relatively simple to deal with, so are | |||
| 963 | * inlined here. */ | |||
| 964 | ||||
| 965 | case DBUS_TYPE_BOOLEAN((int) 'b'): | |||
| 966 | if (PyObject_IsTrue(obj)) { | |||
| 967 | u.bool_val = 1; | |||
| 968 | } | |||
| 969 | else { | |||
| 970 | u.bool_val = 0; | |||
| 971 | } | |||
| 972 | DBG("Performing actual append: bool(%ld)", (long)u.bool_val)do {} while (0); | |||
| 973 | if (!dbus_message_iter_append_basic(appender, sig_type, &u.bool_val)) { | |||
| 974 | PyErr_NoMemory(); | |||
| 975 | ret = -1; | |||
| 976 | break; | |||
| 977 | } | |||
| 978 | ret = 0; | |||
| 979 | break; | |||
| 980 | ||||
| 981 | case DBUS_TYPE_DOUBLE((int) 'd'): | |||
| 982 | u.dbl = PyFloat_AsDouble(obj); | |||
| 983 | if (PyErr_Occurred()) { | |||
| 984 | ret = -1; | |||
| 985 | break; | |||
| 986 | } | |||
| 987 | DBG("Performing actual append: double(%f)", u.dbl)do {} while (0); | |||
| 988 | if (!dbus_message_iter_append_basic(appender, sig_type, &u.dbl)) { | |||
| 989 | PyErr_NoMemory(); | |||
| 990 | ret = -1; | |||
| 991 | break; | |||
| 992 | } | |||
| 993 | ret = 0; | |||
| 994 | break; | |||
| 995 | ||||
| 996 | #ifdef WITH_DBUS_FLOAT32 | |||
| 997 | case DBUS_TYPE_FLOAT: | |||
| 998 | u.dbl = PyFloat_AsDouble(obj); | |||
| 999 | if (PyErr_Occurred()) { | |||
| 1000 | ret = -1; | |||
| 1001 | break; | |||
| 1002 | } | |||
| 1003 | /* FIXME: DBusBasicValue will need to grow a float member if | |||
| 1004 | * float32 becomes supported */ | |||
| 1005 | u.f = (float)u.dbl; | |||
| 1006 | DBG("Performing actual append: float(%f)", u.f)do {} while (0); | |||
| 1007 | if (!dbus_message_iter_append_basic(appender, sig_type, &u.f)) { | |||
| 1008 | PyErr_NoMemory(); | |||
| 1009 | ret = -1; | |||
| 1010 | break; | |||
| 1011 | } | |||
| 1012 | ret = 0; | |||
| 1013 | break; | |||
| 1014 | #endif | |||
| 1015 | ||||
| 1016 | /* The integer types are all basically the same - we delegate to | |||
| 1017 | intNN_range_check() */ | |||
| 1018 | #define PROCESS_INTEGER(size, member) \ | |||
| 1019 | u.member = dbus_py_##size##_range_check(obj);\ | |||
| 1020 | if (u.member == (dbus_##size##_t)(-1) && PyErr_Occurred()) {\ | |||
| 1021 | ret = -1; \ | |||
| 1022 | break; \ | |||
| 1023 | }\ | |||
| 1024 | DBG("Performing actual append: " #size "(%lld)", (long long)u.member)do {} while (0); \ | |||
| 1025 | if (!dbus_message_iter_append_basic(appender, sig_type, &u.member)) {\ | |||
| 1026 | PyErr_NoMemory();\ | |||
| 1027 | ret = -1;\ | |||
| 1028 | break;\ | |||
| 1029 | } \ | |||
| 1030 | ret = 0; | |||
| 1031 | ||||
| 1032 | case DBUS_TYPE_INT16((int) 'n'): | |||
| 1033 | PROCESS_INTEGER(int16, i16) | |||
| 1034 | break; | |||
| 1035 | case DBUS_TYPE_UINT16((int) 'q'): | |||
| 1036 | PROCESS_INTEGER(uint16, u16) | |||
| 1037 | break; | |||
| 1038 | case DBUS_TYPE_INT32((int) 'i'): | |||
| 1039 | PROCESS_INTEGER(int32, i32) | |||
| 1040 | break; | |||
| 1041 | case DBUS_TYPE_UINT32((int) 'u'): | |||
| 1042 | PROCESS_INTEGER(uint32, u32) | |||
| 1043 | break; | |||
| 1044 | #if defined(DBUS_HAVE_INT641) && defined(HAVE_LONG_LONG1) | |||
| 1045 | case DBUS_TYPE_INT64((int) 'x'): | |||
| 1046 | PROCESS_INTEGER(int64, i64) | |||
| 1047 | break; | |||
| 1048 | case DBUS_TYPE_UINT64((int) 't'): | |||
| 1049 | PROCESS_INTEGER(uint64, u64) | |||
| 1050 | break; | |||
| 1051 | #else | |||
| 1052 | case DBUS_TYPE_INT64((int) 'x'): | |||
| 1053 | case DBUS_TYPE_UINT64((int) 't'): | |||
| 1054 | PyErr_SetString(PyExc_NotImplementedError, "64-bit integer " | |||
| 1055 | "types are not supported on this platform"); | |||
| 1056 | ret = -1; | |||
| 1057 | break; | |||
| 1058 | #endif | |||
| 1059 | #undef PROCESS_INTEGER | |||
| 1060 | ||||
| 1061 | /* Now the more complicated cases, which are delegated to helper | |||
| 1062 | * functions (although in practice, the compiler will hopefully | |||
| 1063 | * inline them anyway). */ | |||
| 1064 | ||||
| 1065 | case DBUS_TYPE_STRING((int) 's'): | |||
| 1066 | case DBUS_TYPE_SIGNATURE((int) 'g'): | |||
| 1067 | case DBUS_TYPE_OBJECT_PATH((int) 'o'): | |||
| 1068 | ret = _message_iter_append_string(appender, sig_type, obj, TRUE1); | |||
| 1069 | break; | |||
| 1070 | ||||
| 1071 | case DBUS_TYPE_BYTE((int) 'y'): | |||
| 1072 | ret = _message_iter_append_byte(appender, obj); | |||
| 1073 | break; | |||
| 1074 | ||||
| 1075 | case DBUS_TYPE_ARRAY((int) 'a'): | |||
| 1076 | /* 3 cases - it might actually be a dict, or it might be a byte array | |||
| 1077 | * being copied from a string (for which we have a faster path), | |||
| 1078 | * or it might be a generic array. */ | |||
| 1079 | ||||
| 1080 | sig_type = dbus_signature_iter_get_element_type(sig_iter); | |||
| 1081 | if (sig_type == DBUS_TYPE_DICT_ENTRY((int) 'e')) | |||
| 1082 | ret = _message_iter_append_multi(appender, sig_iter, | |||
| 1083 | DBUS_TYPE_DICT_ENTRY((int) 'e'), obj); | |||
| 1084 | else if (sig_type == DBUS_TYPE_BYTE((int) 'y') && PyBytes_Check(obj)((((((PyObject*)(obj))->ob_type))->tp_flags & ((1UL << 27))) != 0)) | |||
| 1085 | ret = _message_iter_append_string_as_byte_array(appender, obj); | |||
| 1086 | else | |||
| 1087 | ret = _message_iter_append_multi(appender, sig_iter, | |||
| 1088 | DBUS_TYPE_ARRAY((int) 'a'), obj); | |||
| 1089 | DBG("_message_iter_append_multi(): %d", ret)do {} while (0); | |||
| 1090 | break; | |||
| 1091 | ||||
| 1092 | case DBUS_TYPE_STRUCT((int) 'r'): | |||
| 1093 | ret = _message_iter_append_multi(appender, sig_iter, sig_type, obj); | |||
| 1094 | break; | |||
| 1095 | ||||
| 1096 | case DBUS_TYPE_VARIANT((int) 'v'): | |||
| 1097 | ret = _message_iter_append_variant(appender, obj); | |||
| 1098 | break; | |||
| 1099 | ||||
| 1100 | case DBUS_TYPE_INVALID((int) '\0'): | |||
| 1101 | PyErr_SetString(PyExc_TypeError, "Fewer items found in D-Bus " | |||
| 1102 | "signature than in Python arguments"); | |||
| 1103 | ret = -1; | |||
| 1104 | break; | |||
| 1105 | ||||
| 1106 | #if defined(DBUS_TYPE_UNIX_FD((int) 'h')) | |||
| 1107 | case DBUS_TYPE_UNIX_FD((int) 'h'): | |||
| 1108 | ret = _message_iter_append_unixfd(appender, obj); | |||
| 1109 | break; | |||
| 1110 | #endif | |||
| 1111 | ||||
| 1112 | default: | |||
| 1113 | PyErr_Format(PyExc_TypeError, "Unknown type '\\x%x' in D-Bus " | |||
| 1114 | "signature", sig_type); | |||
| 1115 | ret = -1; | |||
| 1116 | break; | |||
| 1117 | } | |||
| 1118 | if (ret < 0) return -1; | |||
| 1119 | ||||
| 1120 | DBG("Advancing signature iter at %p", sig_iter)do {} while (0); | |||
| 1121 | *more = dbus_signature_iter_next(sig_iter); | |||
| 1122 | #ifdef USING_DBG | |||
| 1123 | DBG("- result: %ld, type %02x '%c'", (long)(*more),do {} while (0) | |||
| 1124 | (int)dbus_signature_iter_get_current_type(sig_iter),do {} while (0) | |||
| 1125 | (int)dbus_signature_iter_get_current_type(sig_iter))do {} while (0); | |||
| 1126 | #endif | |||
| 1127 | return 0; | |||
| 1128 | } | |||
| 1129 | ||||
| 1130 | ||||
| 1131 | PyObject * | |||
| 1132 | dbus_py_Message_append(Message *self, PyObject *args, PyObject *kwargs) | |||
| 1133 | { | |||
| 1134 | const char *signature = NULL((void*)0); | |||
| 1135 | PyObject *signature_obj = NULL((void*)0); | |||
| 1136 | DBusSignatureIter sig_iter; | |||
| 1137 | DBusMessageIter appender; | |||
| 1138 | static char *argnames[] = {"signature", NULL((void*)0)}; | |||
| 1139 | dbus_bool_t more; | |||
| 1140 | ||||
| 1141 | if (!self->msg) return DBusPy_RaiseUnusableMessage(); | |||
| ||||
| 1142 | ||||
| 1143 | #ifdef USING_DBG | |||
| 1144 | fprintf(stderrstderr, "DBG/%ld: called Message_append(*", (long)getpid()); | |||
| 1145 | PyObject_Print(args, stderrstderr, 0); | |||
| 1146 | if (kwargs) { | |||
| 1147 | fprintf(stderrstderr, ", **"); | |||
| 1148 | PyObject_Print(kwargs, stderrstderr, 0); | |||
| 1149 | } | |||
| 1150 | fprintf(stderrstderr, ")\n"); | |||
| 1151 | #endif | |||
| 1152 | ||||
| 1153 | /* only use kwargs for this step: deliberately ignore args for now */ | |||
| 1154 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(dbus_py_empty_tuple, kwargs, "|z:append", | |||
| 1155 | argnames, &signature)) return NULL((void*)0); | |||
| 1156 | ||||
| 1157 | if (!signature) { | |||
| 1158 | DBG("%s", "No signature for message, guessing...")do {} while (0); | |||
| 1159 | signature_obj = dbus_py_Message_guess_signature(NULL((void*)0), args); | |||
| 1160 | if (!signature_obj) return NULL((void*)0); | |||
| 1161 | if (PyUnicode_Check(signature_obj)((((((PyObject*)(signature_obj))->ob_type))->tp_flags & ((1UL << 28))) != 0)) { | |||
| 1162 | PyObject *signature_as_bytes; | |||
| 1163 | signature_as_bytes = PyUnicode_AsUTF8String(signature_obj); | |||
| 1164 | Py_CLEAR(signature_obj)do { PyObject *_py_tmp = ((PyObject*)(signature_obj)); if (_py_tmp != ((void*)0)) { (signature_obj) = ((void*)0); _Py_DECREF((( PyObject*)(_py_tmp))); } } while (0); | |||
| 1165 | if (!signature_as_bytes) | |||
| 1166 | return NULL((void*)0); | |||
| 1167 | signature_obj = signature_as_bytes; | |||
| 1168 | } | |||
| 1169 | else { | |||
| 1170 | assert(PyBytes_Check(signature_obj))((void) sizeof ((((((((PyObject*)(signature_obj))->ob_type ))->tp_flags & ((1UL << 27))) != 0)) ? 1 : 0), __extension__ ({ if (((((((PyObject*)(signature_obj))->ob_type))->tp_flags & ((1UL << 27))) != 0)) ; else __assert_fail ("PyBytes_Check(signature_obj)" , "../../dbus_bindings/message-append.c", 1170, __extension__ __PRETTY_FUNCTION__); })); | |||
| 1171 | } | |||
| 1172 | signature = PyBytes_AS_STRING(signature_obj)((((PyBytesObject *)(signature_obj))->ob_sval)); | |||
| 1173 | } | |||
| 1174 | /* from here onwards, you have to do a goto rather than returning NULL | |||
| 1175 | to make sure signature_obj gets freed */ | |||
| 1176 | ||||
| 1177 | /* iterate over args and the signature, together */ | |||
| 1178 | if (!dbus_signature_validate(signature, NULL((void*)0))) { | |||
| 1179 | PyErr_SetString(PyExc_ValueError, "Corrupt type signature"); | |||
| 1180 | goto err; | |||
| 1181 | } | |||
| 1182 | dbus_message_iter_init_append(self->msg, &appender); | |||
| 1183 | ||||
| 1184 | if (signature[0] != '\0') { | |||
| 1185 | int i = 0; | |||
| 1186 | ||||
| 1187 | more = TRUE1; | |||
| 1188 | dbus_signature_iter_init(&sig_iter, signature); | |||
| 1189 | while (more) { | |||
| 1190 | if (i >= PyTuple_GET_SIZE(args)(((PyVarObject*)(((PyTupleObject *)(args))))->ob_size)) { | |||
| 1191 | PyErr_SetString(PyExc_TypeError, "More items found in D-Bus " | |||
| 1192 | "signature than in Python arguments"); | |||
| 1193 | goto hosed; | |||
| 1194 | } | |||
| 1195 | if (_message_iter_append_pyobject(&appender, &sig_iter, | |||
| 1196 | PyTuple_GET_ITEM(args, i)(((PyTupleObject *)(args))->ob_item[i]), | |||
| 1197 | &more) < 0) { | |||
| 1198 | goto hosed; | |||
| 1199 | } | |||
| 1200 | i++; | |||
| 1201 | } | |||
| 1202 | if (i < PyTuple_GET_SIZE(args)(((PyVarObject*)(((PyTupleObject *)(args))))->ob_size)) { | |||
| 1203 | PyErr_SetString(PyExc_TypeError, "Fewer items found in D-Bus " | |||
| 1204 | "signature than in Python arguments"); | |||
| 1205 | goto hosed; | |||
| 1206 | } | |||
| 1207 | } | |||
| 1208 | ||||
| 1209 | /* success! */ | |||
| 1210 | Py_CLEAR(signature_obj)do { PyObject *_py_tmp = ((PyObject*)(signature_obj)); if (_py_tmp != ((void*)0)) { (signature_obj) = ((void*)0); _Py_DECREF((( PyObject*)(_py_tmp))); } } while (0); | |||
| 1211 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 1212 | ||||
| 1213 | hosed: | |||
| 1214 | /* "If appending any of the arguments fails due to lack of memory, | |||
| 1215 | * generally the message is hosed and you have to start over" -libdbus docs | |||
| 1216 | * Enforce this by throwing away the message structure. | |||
| 1217 | */ | |||
| 1218 | dbus_message_unref(self->msg); | |||
| 1219 | self->msg = NULL((void*)0); | |||
| 1220 | err: | |||
| 1221 | Py_CLEAR(signature_obj)do { PyObject *_py_tmp = ((PyObject*)(signature_obj)); if (_py_tmp != ((void*)0)) { (signature_obj) = ((void*)0); _Py_DECREF((( PyObject*)(_py_tmp))); } } while (0); | |||
| 1222 | return NULL((void*)0); | |||
| 1223 | } | |||
| 1224 | ||||
| 1225 | /* vim:set ft=c cino< sw=4 sts=4 et: */ |
| 1 | #ifndef _PyObject_CallFunction_SizeT |
| 2 | struct _object; |
| 3 | typedef struct _object PyObject; |
| 4 | PyObject* clang_analyzer_PyObject_New_Reference(); |
| 5 | PyObject* _PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...) { |
| 6 | return clang_analyzer_PyObject_New_Reference(); |
| 7 | } |
| 8 | #else |
| 9 | #warning "API _PyObject_CallFunction_SizeT is defined as a macro." |
| 10 | #endif |