From 028dfef5fc182d64f9cbb7a3e2dd684ebf0b5ea0 Mon Sep 17 00:00:00 2001 From: Olivier Martin Date: Fri, 9 Feb 2024 09:56:47 +0100 Subject: [PATCH] python: Update deprecated calls --- common/gattlib_common.c | 28 +++++++++++++++++++++------- dbus/CMakeLists.txt | 2 +- dbus/gattlib_internal.h | 5 ++++- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/common/gattlib_common.c b/common/gattlib_common.c index b0a0fa4..f0e40e6 100644 --- a/common/gattlib_common.c +++ b/common/gattlib_common.c @@ -1,13 +1,9 @@ /* * SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later * - * Copyright (c) 2021-2022, Olivier Martin + * Copyright (c) 2021-2024, Olivier Martin */ -#if defined(WITH_PYTHON) - #include -#endif - #include #include "gattlib_internal.h" @@ -62,6 +58,7 @@ void gattlib_call_notification_handler(struct gattlib_handler *handler, const uu else if (handler->type == PYTHON) { char uuid_str[MAX_LEN_UUID_STR + 1]; PyGILState_STATE d_gstate; + PyObject *result; gattlib_uuid_to_string(uuid, uuid_str, sizeof(uuid_str)); @@ -74,9 +71,17 @@ void gattlib_call_notification_handler(struct gattlib_handler *handler, const uu argument_string = "(sIIO)"; } PyObject *arglist = Py_BuildValue(argument_string, uuid_str, data, data_length, handler->user_data); - PyEval_CallObject((PyObject *)handler->notification_handler, arglist); +#if PYTHON_VERSION >= PYTHON_VERSIONS(3, 9) + result = PyObject_Call((PyObject *)handler->notification_handler, arglist, NULL); +#else + result = PyEval_CallObject((PyObject *)handler->notification_handler, arglist); +#endif Py_DECREF(arglist); + if (result == NULL) { + GATTLIB_LOG(GATTLIB_ERROR, "Python notification handler has raised an exception."); + } + PyGILState_Release(d_gstate); } #endif @@ -91,13 +96,22 @@ void gattlib_call_disconnection_handler(struct gattlib_handler *handler) { } #if defined(WITH_PYTHON) else if (handler->type == PYTHON) { + PyObject *result; PyGILState_STATE d_gstate; d_gstate = PyGILState_Ensure(); PyObject *arglist = Py_BuildValue("(O)", handler->user_data); - PyEval_CallObject((PyObject *)handler->disconnection_handler, arglist); +#if PYTHON_VERSION >= PYTHON_VERSIONS(3, 9) + result = PyObject_Call((PyObject *)handler->disconnection_handler, arglist, NULL); +#else + result = PyEval_CallObject((PyObject *)handler->disconnection_handler, arglist); +#endif Py_DECREF(arglist); + if (result == NULL) { + GATTLIB_LOG(GATTLIB_ERROR, "Python handler has raised an exception."); + } + PyGILState_Release(d_gstate); } #endif diff --git a/dbus/CMakeLists.txt b/dbus/CMakeLists.txt index 41c5754..2f43b2d 100644 --- a/dbus/CMakeLists.txt +++ b/dbus/CMakeLists.txt @@ -101,7 +101,7 @@ if(GATTLIB_PYTHON_INTERFACE) include_directories(${Python_INCLUDE_DIRS}) list(APPEND gattlib_LIBS ${Python_LIBRARIES}) - add_definitions(-DWITH_PYTHON) + add_definitions(-DWITH_PYTHON -DPYTHON_VERSION_MAJOR=${Python_VERSION_MAJOR} -DPYTHON_VERSION_MINOR=${Python_VERSION_MINOR}) endif() endif() diff --git a/dbus/gattlib_internal.h b/dbus/gattlib_internal.h index 162ba5b..3bb78e1 100644 --- a/dbus/gattlib_internal.h +++ b/dbus/gattlib_internal.h @@ -1,7 +1,7 @@ /* * SPDX-License-Identifier: BSD-3-Clause * - * Copyright (c) 2016-2022, Olivier Martin + * Copyright (c) 2016-2024, Olivier Martin */ #ifndef __GATTLIB_INTERNAL_H__ @@ -21,6 +21,9 @@ #if defined(WITH_PYTHON) #include + + #define PYTHON_VERSIONS(major, minor) (((major) << 8) | (minor)) + #define PYTHON_VERSION PYTHON_VERSIONS(PYTHON_VERSION_MAJOR, PYTHON_VERSION_MINOR) #endif #include "bluez5/lib/uuid.h"