Skip to content

Commit 028dfef

Browse files
committed
python: Update deprecated calls
1 parent a41061c commit 028dfef

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

common/gattlib_common.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
/*
22
* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
33
*
4-
* Copyright (c) 2021-2022, Olivier Martin <[email protected]>
4+
* Copyright (c) 2021-2024, Olivier Martin <[email protected]>
55
*/
66

7-
#if defined(WITH_PYTHON)
8-
#include <Python.h>
9-
#endif
10-
117
#include <stdio.h>
128

139
#include "gattlib_internal.h"
@@ -62,6 +58,7 @@ void gattlib_call_notification_handler(struct gattlib_handler *handler, const uu
6258
else if (handler->type == PYTHON) {
6359
char uuid_str[MAX_LEN_UUID_STR + 1];
6460
PyGILState_STATE d_gstate;
61+
PyObject *result;
6562

6663
gattlib_uuid_to_string(uuid, uuid_str, sizeof(uuid_str));
6764

@@ -74,9 +71,17 @@ void gattlib_call_notification_handler(struct gattlib_handler *handler, const uu
7471
argument_string = "(sIIO)";
7572
}
7673
PyObject *arglist = Py_BuildValue(argument_string, uuid_str, data, data_length, handler->user_data);
77-
PyEval_CallObject((PyObject *)handler->notification_handler, arglist);
74+
#if PYTHON_VERSION >= PYTHON_VERSIONS(3, 9)
75+
result = PyObject_Call((PyObject *)handler->notification_handler, arglist, NULL);
76+
#else
77+
result = PyEval_CallObject((PyObject *)handler->notification_handler, arglist);
78+
#endif
7879
Py_DECREF(arglist);
7980

81+
if (result == NULL) {
82+
GATTLIB_LOG(GATTLIB_ERROR, "Python notification handler has raised an exception.");
83+
}
84+
8085
PyGILState_Release(d_gstate);
8186
}
8287
#endif
@@ -91,13 +96,22 @@ void gattlib_call_disconnection_handler(struct gattlib_handler *handler) {
9196
}
9297
#if defined(WITH_PYTHON)
9398
else if (handler->type == PYTHON) {
99+
PyObject *result;
94100
PyGILState_STATE d_gstate;
95101
d_gstate = PyGILState_Ensure();
96102

97103
PyObject *arglist = Py_BuildValue("(O)", handler->user_data);
98-
PyEval_CallObject((PyObject *)handler->disconnection_handler, arglist);
104+
#if PYTHON_VERSION >= PYTHON_VERSIONS(3, 9)
105+
result = PyObject_Call((PyObject *)handler->disconnection_handler, arglist, NULL);
106+
#else
107+
result = PyEval_CallObject((PyObject *)handler->disconnection_handler, arglist);
108+
#endif
99109
Py_DECREF(arglist);
100110

111+
if (result == NULL) {
112+
GATTLIB_LOG(GATTLIB_ERROR, "Python handler has raised an exception.");
113+
}
114+
101115
PyGILState_Release(d_gstate);
102116
}
103117
#endif

dbus/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ if(GATTLIB_PYTHON_INTERFACE)
101101
include_directories(${Python_INCLUDE_DIRS})
102102
list(APPEND gattlib_LIBS ${Python_LIBRARIES})
103103

104-
add_definitions(-DWITH_PYTHON)
104+
add_definitions(-DWITH_PYTHON -DPYTHON_VERSION_MAJOR=${Python_VERSION_MAJOR} -DPYTHON_VERSION_MINOR=${Python_VERSION_MINOR})
105105
endif()
106106
endif()
107107

dbus/gattlib_internal.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* SPDX-License-Identifier: BSD-3-Clause
33
*
4-
* Copyright (c) 2016-2022, Olivier Martin <[email protected]>
4+
* Copyright (c) 2016-2024, Olivier Martin <[email protected]>
55
*/
66

77
#ifndef __GATTLIB_INTERNAL_H__
@@ -21,6 +21,9 @@
2121

2222
#if defined(WITH_PYTHON)
2323
#include <Python.h>
24+
25+
#define PYTHON_VERSIONS(major, minor) (((major) << 8) | (minor))
26+
#define PYTHON_VERSION PYTHON_VERSIONS(PYTHON_VERSION_MAJOR, PYTHON_VERSION_MINOR)
2427
#endif
2528

2629
#include "bluez5/lib/uuid.h"

0 commit comments

Comments
 (0)