python: Update deprecated calls

fix-build
Olivier Martin 2024-02-09 09:56:47 +01:00
parent a41061c1d4
commit 028dfef5fc
3 changed files with 26 additions and 9 deletions

View File

@ -1,13 +1,9 @@
/*
* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
*
* Copyright (c) 2021-2022, Olivier Martin <olivier@labapart.org>
* Copyright (c) 2021-2024, Olivier Martin <olivier@labapart.org>
*/
#if defined(WITH_PYTHON)
#include <Python.h>
#endif
#include <stdio.h>
#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

View File

@ -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()

View File

@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 2016-2022, Olivier Martin <olivier@labapart.org>
* Copyright (c) 2016-2024, Olivier Martin <olivier@labapart.org>
*/
#ifndef __GATTLIB_INTERNAL_H__
@ -21,6 +21,9 @@
#if defined(WITH_PYTHON)
#include <Python.h>
#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"