1
1
/*
2
2
* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
3
3
*
4
- * Copyright (c) 2021-2022 , Olivier Martin <[email protected] >
4
+ * Copyright (c) 2021-2024 , Olivier Martin <[email protected] >
5
5
*/
6
6
7
- #if defined(WITH_PYTHON )
8
- #include <Python.h>
9
- #endif
10
-
11
7
#include <stdio.h>
12
8
13
9
#include "gattlib_internal.h"
@@ -62,6 +58,7 @@ void gattlib_call_notification_handler(struct gattlib_handler *handler, const uu
62
58
else if (handler -> type == PYTHON ) {
63
59
char uuid_str [MAX_LEN_UUID_STR + 1 ];
64
60
PyGILState_STATE d_gstate ;
61
+ PyObject * result ;
65
62
66
63
gattlib_uuid_to_string (uuid , uuid_str , sizeof (uuid_str ));
67
64
@@ -74,9 +71,17 @@ void gattlib_call_notification_handler(struct gattlib_handler *handler, const uu
74
71
argument_string = "(sIIO)" ;
75
72
}
76
73
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
78
79
Py_DECREF (arglist );
79
80
81
+ if (result == NULL ) {
82
+ GATTLIB_LOG (GATTLIB_ERROR , "Python notification handler has raised an exception." );
83
+ }
84
+
80
85
PyGILState_Release (d_gstate );
81
86
}
82
87
#endif
@@ -91,13 +96,22 @@ void gattlib_call_disconnection_handler(struct gattlib_handler *handler) {
91
96
}
92
97
#if defined(WITH_PYTHON )
93
98
else if (handler -> type == PYTHON ) {
99
+ PyObject * result ;
94
100
PyGILState_STATE d_gstate ;
95
101
d_gstate = PyGILState_Ensure ();
96
102
97
103
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
99
109
Py_DECREF (arglist );
100
110
111
+ if (result == NULL ) {
112
+ GATTLIB_LOG (GATTLIB_ERROR , "Python handler has raised an exception." );
113
+ }
114
+
101
115
PyGILState_Release (d_gstate );
102
116
}
103
117
#endif
0 commit comments