| File: | build/temp.linux-x86_64-3.8/../../dbus_bindings/mainloop.c |
| Warning: | line 160, column 28 PyObject ownership leak with reference count of 1 |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* Implementation of main-loop integration for dbus-python. | ||||
| 2 | * | ||||
| 3 | * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/> | ||||
| 4 | * Copyright (C) 2008 Huang Peng <phuang@redhat.com> | ||||
| 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 | /* Native mainloop wrapper ========================================= */ | ||||
| 32 | |||||
| 33 | PyDoc_STRVAR(NativeMainLoop_tp_doc,static const char NativeMainLoop_tp_doc[] = "Object representing D-Bus main loop integration done in native code.\n" "Cannot be instantiated directly.\n" | ||||
| 34 | "Object representing D-Bus main loop integration done in native code.\n"static const char NativeMainLoop_tp_doc[] = "Object representing D-Bus main loop integration done in native code.\n" "Cannot be instantiated directly.\n" | ||||
| 35 | "Cannot be instantiated directly.\n"static const char NativeMainLoop_tp_doc[] = "Object representing D-Bus main loop integration done in native code.\n" "Cannot be instantiated directly.\n" | ||||
| 36 | )static const char NativeMainLoop_tp_doc[] = "Object representing D-Bus main loop integration done in native code.\n" "Cannot be instantiated directly.\n"; | ||||
| 37 | |||||
| 38 | static PyTypeObject NativeMainLoop_Type; | ||||
| 39 | |||||
| 40 | DEFINE_CHECK(NativeMainLoop)static inline int NativeMainLoop_Check (PyObject *o) { return (((((PyObject*)(o))->ob_type) == (&NativeMainLoop_Type ) || PyType_IsSubtype((((PyObject*)(o))->ob_type), (&NativeMainLoop_Type )))); } static inline int NativeMainLoop_CheckExact (PyObject *o) { return ((((PyObject*)(o))->ob_type) == &NativeMainLoop_Type ); } | ||||
| 41 | |||||
| 42 | typedef struct { | ||||
| 43 | PyObject_HEADPyObject ob_base; | ||||
| 44 | /* Called with the GIL held, should set a Python exception on error */ | ||||
| 45 | dbus_bool_t (*set_up_connection_cb)(DBusConnection *, void *); | ||||
| 46 | dbus_bool_t (*set_up_server_cb)(DBusServer *, void *); | ||||
| 47 | /* Called in a destructor. Must not touch the exception state (use | ||||
| 48 | * PyErr_Fetch and PyErr_Restore if necessary). */ | ||||
| 49 | void (*free_cb)(void *); | ||||
| 50 | void *data; | ||||
| 51 | } NativeMainLoop; | ||||
| 52 | |||||
| 53 | static void NativeMainLoop_tp_dealloc(NativeMainLoop *self) | ||||
| 54 | { | ||||
| 55 | if (self->data && self->free_cb) { | ||||
| 56 | (self->free_cb)(self->data); | ||||
| 57 | } | ||||
| 58 | PyObject_DelPyObject_Free((PyObject *)self); | ||||
| 59 | } | ||||
| 60 | |||||
| 61 | static PyTypeObject NativeMainLoop_Type = { | ||||
| 62 | PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0){ { 1, 0 }, 0 }, | ||||
| 63 | "dbus.mainloop.NativeMainLoop", | ||||
| 64 | sizeof(NativeMainLoop), | ||||
| 65 | 0, | ||||
| 66 | (destructor)NativeMainLoop_tp_dealloc, /* tp_dealloc */ | ||||
| 67 | 0, /* tp_print */ | ||||
| 68 | 0, /* tp_getattr */ | ||||
| 69 | 0, /* tp_setattr */ | ||||
| 70 | 0, /* tp_compare */ | ||||
| 71 | 0, /* tp_repr */ | ||||
| 72 | 0, /* tp_as_number */ | ||||
| 73 | 0, /* tp_as_sequence */ | ||||
| 74 | 0, /* tp_as_mapping */ | ||||
| 75 | 0, /* tp_hash */ | ||||
| 76 | 0, /* tp_call */ | ||||
| 77 | 0, /* tp_str */ | ||||
| 78 | 0, /* tp_getattro */ | ||||
| 79 | 0, /* tp_setattro */ | ||||
| 80 | 0, /* tp_as_buffer */ | ||||
| 81 | Py_TPFLAGS_DEFAULT( 0 | (1UL << 18) | 0), /* tp_flags */ | ||||
| 82 | NativeMainLoop_tp_doc, /* tp_doc */ | ||||
| 83 | 0, /* tp_traverse */ | ||||
| 84 | 0, /* tp_clear */ | ||||
| 85 | 0, /* tp_richcompare */ | ||||
| 86 | 0, /* tp_weaklistoffset */ | ||||
| 87 | 0, /* tp_iter */ | ||||
| 88 | 0, /* tp_iternext */ | ||||
| 89 | 0, /* tp_methods */ | ||||
| 90 | 0, /* tp_members */ | ||||
| 91 | 0, /* tp_getset */ | ||||
| 92 | 0, /* tp_base */ | ||||
| 93 | 0, /* tp_dict */ | ||||
| 94 | 0, /* tp_descr_get */ | ||||
| 95 | 0, /* tp_descr_set */ | ||||
| 96 | 0, /* tp_dictoffset */ | ||||
| 97 | 0, /* tp_init */ | ||||
| 98 | 0, /* tp_alloc */ | ||||
| 99 | /* deliberately not callable! */ | ||||
| 100 | 0, /* tp_new */ | ||||
| 101 | }; | ||||
| 102 | |||||
| 103 | /* Internal C API for Connection, Bus, Server ======================= */ | ||||
| 104 | |||||
| 105 | dbus_bool_t | ||||
| 106 | dbus_py_check_mainloop_sanity(PyObject *mainloop) | ||||
| 107 | { | ||||
| 108 | if (NativeMainLoop_Check(mainloop)) { | ||||
| 109 | return TRUE1; | ||||
| 110 | } | ||||
| 111 | PyErr_SetString(PyExc_TypeError, | ||||
| 112 | "A dbus.mainloop.NativeMainLoop instance is required"); | ||||
| 113 | return FALSE0; | ||||
| 114 | } | ||||
| 115 | |||||
| 116 | dbus_bool_t | ||||
| 117 | dbus_py_set_up_connection(PyObject *conn, PyObject *mainloop) | ||||
| 118 | { | ||||
| 119 | if (NativeMainLoop_Check(mainloop)) { | ||||
| 120 | /* Native mainloops are allowed to do arbitrary strange things */ | ||||
| 121 | NativeMainLoop *nml = (NativeMainLoop *)mainloop; | ||||
| 122 | DBusConnection *dbc = DBusPyConnection_BorrowDBusConnection(conn); | ||||
| 123 | |||||
| 124 | if (!dbc) { | ||||
| 125 | return FALSE0; | ||||
| 126 | } | ||||
| 127 | return (nml->set_up_connection_cb)(dbc, nml->data); | ||||
| 128 | } | ||||
| 129 | PyErr_SetString(PyExc_TypeError, | ||||
| 130 | "A dbus.mainloop.NativeMainLoop instance is required"); | ||||
| 131 | return FALSE0; | ||||
| 132 | } | ||||
| 133 | |||||
| 134 | dbus_bool_t | ||||
| 135 | dbus_py_set_up_server(PyObject *server, PyObject *mainloop) | ||||
| 136 | { | ||||
| 137 | if (NativeMainLoop_Check(mainloop)) { | ||||
| 138 | /* Native mainloops are allowed to do arbitrary strange things */ | ||||
| 139 | NativeMainLoop *nml = (NativeMainLoop *)mainloop; | ||||
| 140 | DBusServer *dbs = DBusPyServer_BorrowDBusServer(server); | ||||
| 141 | |||||
| 142 | if (!dbs) { | ||||
| 143 | return FALSE0; | ||||
| 144 | } | ||||
| 145 | return (nml->set_up_server_cb)(dbs, nml->data); | ||||
| 146 | } | ||||
| 147 | PyErr_SetString(PyExc_TypeError, | ||||
| 148 | "A dbus.mainloop.NativeMainLoop instance is required"); | ||||
| 149 | return FALSE0; | ||||
| 150 | } | ||||
| 151 | |||||
| 152 | /* C API ============================================================ */ | ||||
| 153 | |||||
| 154 | PyObject * | ||||
| 155 | DBusPyNativeMainLoop_New4(dbus_bool_t (*conn_cb)(DBusConnection *, void *), | ||||
| 156 | dbus_bool_t (*server_cb)(DBusServer *, void *), | ||||
| 157 | void (*free_cb)(void *), | ||||
| 158 | void *data) | ||||
| 159 | { | ||||
| 160 | NativeMainLoop *self = PyObject_New(NativeMainLoop, &NativeMainLoop_Type)( (NativeMainLoop *) _PyObject_New(&NativeMainLoop_Type) ); | ||||
| |||||
| 161 | if (self) { | ||||
| 162 | self->data = data; | ||||
| 163 | self->free_cb = free_cb; | ||||
| 164 | self->set_up_connection_cb = conn_cb; | ||||
| 165 | self->set_up_server_cb = server_cb; | ||||
| 166 | } | ||||
| 167 | return (PyObject *)self; | ||||
| 168 | } | ||||
| 169 | |||||
| 170 | /* Null mainloop implementation ===================================== */ | ||||
| 171 | |||||
| 172 | static dbus_bool_t | ||||
| 173 | noop_main_loop_cb(void *conn_or_server UNUSED__attribute__((__unused__)), void *data UNUSED__attribute__((__unused__))) | ||||
| 174 | { | ||||
| 175 | return TRUE1; | ||||
| 176 | } | ||||
| 177 | |||||
| 178 | #define noop_conn_cb((dbus_bool_t (*)(DBusConnection *, void *))(noop_main_loop_cb )) ((dbus_bool_t (*)(DBusConnection *, void *))(noop_main_loop_cb)) | ||||
| 179 | #define noop_server_cb((dbus_bool_t (*)(DBusServer *, void *))(noop_main_loop_cb)) ((dbus_bool_t (*)(DBusServer *, void *))(noop_main_loop_cb)) | ||||
| 180 | |||||
| 181 | /* Initialization =================================================== */ | ||||
| 182 | |||||
| 183 | dbus_bool_t | ||||
| 184 | dbus_py_init_mainloop(void) | ||||
| 185 | { | ||||
| 186 | if (PyType_Ready (&NativeMainLoop_Type) < 0) return 0; | ||||
| 187 | |||||
| 188 | return 1; | ||||
| 189 | } | ||||
| 190 | |||||
| 191 | dbus_bool_t | ||||
| 192 | dbus_py_insert_mainloop_types(PyObject *this_module) | ||||
| 193 | { | ||||
| 194 | PyObject *null_main_loop = DBusPyNativeMainLoop_New4(noop_conn_cb((dbus_bool_t (*)(DBusConnection *, void *))(noop_main_loop_cb )), | ||||
| |||||
| 195 | noop_server_cb((dbus_bool_t (*)(DBusServer *, void *))(noop_main_loop_cb)), | ||||
| 196 | NULL((void*)0), | ||||
| 197 | NULL((void*)0)); | ||||
| 198 | if (!null_main_loop
| ||||
| 199 | |||||
| 200 | /* PyModule_AddObject steals a ref */ | ||||
| 201 | Py_INCREF (&NativeMainLoop_Type)_Py_INCREF(((PyObject*)(&NativeMainLoop_Type))); | ||||
| 202 | if (PyModule_AddObject (this_module, "NativeMainLoop", | ||||
| 203 | (PyObject *)&NativeMainLoop_Type) < 0) return 0; | ||||
| 204 | if (PyModule_AddObject (this_module, "NULL_MAIN_LOOP", | ||||
| 205 | null_main_loop) < 0) return 0; | ||||
| 206 | return 1; | ||||
| 207 | } | ||||
| 208 | |||||
| 209 | /* vim:set ft=c cino< sw=4 sts=4 et: */ |
| 1 | #ifndef _PyObject_New |
| 2 | struct _object; |
| 3 | typedef struct _object PyObject; |
| 4 | PyObject* clang_analyzer_PyObject_New_Reference(); |
| 5 | PyObject* _PyObject_New(PyTypeObject *type) { |
| 6 | return clang_analyzer_PyObject_New_Reference(); |
| 7 | } |
| 8 | #else |
| 9 | #warning "API _PyObject_New is defined as a macro." |
| 10 | #endif |