|
37 | 37 | return NULL; \ |
38 | 38 | } |
39 | 39 |
|
40 | | -#define CALL_PYTHON_AND_CHECK(py_function) \ |
41 | | - ({ \ |
42 | | - PyObject* new_object = py_function; \ |
43 | | - if (new_object == NULL) { \ |
44 | | - return NULL; \ |
45 | | - } \ |
46 | | - new_object; \ |
47 | | - }) |
48 | | - |
49 | | -#define CALL_PYTHON_GOTO_FAIL(py_function) \ |
50 | | - ({ \ |
51 | | - PyObject* new_object = py_function; \ |
52 | | - if (new_object == NULL) { \ |
53 | | - goto fail; \ |
54 | | - } \ |
55 | | - new_object; \ |
56 | | - }) |
| 40 | +// Call Python macros |
57 | 41 |
|
58 | 42 | #define CALL_PYTHON_FAIL_ACTION(py_function, action) \ |
59 | 43 | ({ \ |
|
64 | 48 | new_object; \ |
65 | 49 | }) |
66 | 50 |
|
| 51 | +#define CALL_PYTHON_AND_CHECK(py_function) CALL_PYTHON_FAIL_ACTION(py_function, return NULL) |
| 52 | +#define CALL_PYTHON_CHECK_RETURN_NEG1(py_function) CALL_PYTHON_FAIL_ACTION(py_function, return -1) |
| 53 | +#define CALL_PYTHON_GOTO_FAIL(py_function) CALL_PYTHON_FAIL_ACTION(py_function, goto fail) |
| 54 | + |
| 55 | +#define CALL_PYTHON_INT_CHECK(py_function) \ |
| 56 | + ({ \ |
| 57 | + int return_int = py_function; \ |
| 58 | + if (return_int < 0) { \ |
| 59 | + return NULL; \ |
| 60 | + } \ |
| 61 | + return_int; \ |
| 62 | + }) |
| 63 | + |
| 64 | +#define CALL_PYTHON_BOOL_CHECK(py_function) \ |
| 65 | + ({ \ |
| 66 | + int return_int = py_function; \ |
| 67 | + if (!return_int) { \ |
| 68 | + return NULL; \ |
| 69 | + } \ |
| 70 | + return_int; \ |
| 71 | + }) |
| 72 | + |
| 73 | +#define CALL_PYTHON_EXPECT_NONE(py_function) \ |
| 74 | + ({ \ |
| 75 | + PyObject* none_obj = py_function; \ |
| 76 | + if (none_obj == NULL) { \ |
| 77 | + return NULL; \ |
| 78 | + } \ |
| 79 | + Py_DECREF(none_obj); \ |
| 80 | + }) |
| 81 | + |
67 | 82 | #define PYTHON_ERR_OCCURED \ |
68 | 83 | if (PyErr_Occurred()) { \ |
69 | 84 | return NULL; \ |
|
96 | 111 | return_int; \ |
97 | 112 | }) |
98 | 113 |
|
99 | | -#define SD_BUS_PY_UNICODE_AS_BYTES(py_unicode) \ |
| 114 | +#define SD_BUS_PY_UNICODE_AS_BYTES_ERROR_ACTION(py_unicode, action) \ |
100 | 115 | ({ \ |
101 | 116 | PyObject* utf_8_bytes = PyUnicode_AsUTF8String(py_unicode); \ |
102 | 117 | if (utf_8_bytes == NULL) { \ |
103 | | - return NULL; \ |
| 118 | + action; \ |
104 | 119 | } \ |
105 | 120 | utf_8_bytes; \ |
106 | 121 | }) |
107 | 122 |
|
108 | | -#define SD_BUS_PY_UNICODE_AS_BYTES_GOTO_FAIL(py_unicode) \ |
109 | | - ({ \ |
110 | | - PyObject* utf_8_bytes = PyUnicode_AsUTF8String(py_unicode); \ |
111 | | - if (utf_8_bytes == NULL) { \ |
112 | | - goto fail; \ |
113 | | - } \ |
114 | | - utf_8_bytes; \ |
115 | | - }) |
| 123 | +#define SD_BUS_PY_UNICODE_AS_BYTES(py_unicode) SD_BUS_PY_UNICODE_AS_BYTES_ERROR_ACTION(py_unicode, return NULL) |
| 124 | +#define SD_BUS_PY_UNICODE_AS_BYTES_GOTO_FAIL(py_unicode) SD_BUS_PY_UNICODE_AS_BYTES_ERROR_ACTION(py_unicode, goto fail) |
116 | 125 |
|
117 | | -#define SD_BUS_PY_BYTES_AS_CHAR_PTR(py_bytes) \ |
| 126 | +#define SD_BUS_PY_BYTES_AS_CHAR_PTR_ERROR_ACTION(py_bytes, action) \ |
118 | 127 | ({ \ |
119 | 128 | const char* new_char_ptr = PyBytes_AsString(py_bytes); \ |
120 | 129 | if (new_char_ptr == NULL) { \ |
121 | | - return NULL; \ |
| 130 | + action; \ |
122 | 131 | } \ |
123 | 132 | new_char_ptr; \ |
124 | 133 | }) |
125 | 134 |
|
126 | | -#define SD_BUS_PY_BYTES_AS_CHAR_PTR_GOTO_FAIL(py_bytes) \ |
127 | | - ({ \ |
128 | | - const char* new_char_ptr = PyBytes_AsString(py_bytes); \ |
129 | | - if (new_char_ptr == NULL) { \ |
130 | | - goto fail; \ |
131 | | - } \ |
132 | | - new_char_ptr; \ |
133 | | - }) |
| 135 | +#define SD_BUS_PY_BYTES_AS_CHAR_PTR(py_bytes) SD_BUS_PY_BYTES_AS_CHAR_PTR_ERROR_ACTION(py_bytes, return NULL) |
| 136 | +#define SD_BUS_PY_BYTES_AS_CHAR_PTR_GOTO_FAIL(py_bytes) SD_BUS_PY_BYTES_AS_CHAR_PTR_ERROR_ACTION(py_bytes, goto fail) |
134 | 137 |
|
135 | 138 | #ifndef Py_LIMITED_API |
136 | | -#define SD_BUS_PY_UNICODE_AS_CHAR_PTR(py_object) \ |
| 139 | +#define SD_BUS_PY_UNICODE_AS_CHAR_PTR_ERROR_ACTION(py_object, action) \ |
137 | 140 | ({ \ |
138 | 141 | const char* new_char_ptr = PyUnicode_AsUTF8(py_object); \ |
139 | 142 | if (new_char_ptr == NULL) { \ |
140 | | - return NULL; \ |
| 143 | + action; \ |
141 | 144 | } \ |
142 | 145 | new_char_ptr; \ |
143 | 146 | }) |
144 | 147 |
|
145 | | -#define SD_BUS_PY_UNICODE_AS_CHAR_PTR_GOTO_FAIL(py_object) \ |
146 | | - ({ \ |
147 | | - const char* new_char_ptr = PyUnicode_AsUTF8(py_object); \ |
148 | | - if (new_char_ptr == NULL) { \ |
149 | | - goto fail; \ |
150 | | - } \ |
151 | | - new_char_ptr; \ |
152 | | - }) |
| 148 | +#define SD_BUS_PY_UNICODE_AS_CHAR_PTR(py_object) SD_BUS_PY_UNICODE_AS_CHAR_PTR_ERROR_ACTION(py_object, return NULL) |
| 149 | +#define SD_BUS_PY_UNICODE_AS_CHAR_PTR_GOTO_FAIL(py_object) SD_BUS_PY_UNICODE_AS_CHAR_PTR_ERROR_ACTION(py_object, goto fail) |
153 | 150 |
|
154 | 151 | #define SD_BUS_PY_UNICODE_AS_CHAR_PTR_OPTIONAL(py_object) \ |
155 | 152 | ({ \ |
|
186 | 183 | next_object; \ |
187 | 184 | }) |
188 | 185 |
|
189 | | -#define CALL_PYTHON_INT_CHECK(py_function) \ |
190 | | - ({ \ |
191 | | - int return_int = py_function; \ |
192 | | - if (return_int < 0) { \ |
193 | | - return NULL; \ |
194 | | - } \ |
195 | | - return_int; \ |
196 | | - }) |
197 | | - |
198 | | -#define CALL_PYTHON_BOOL_CHECK(py_function) \ |
199 | | - ({ \ |
200 | | - int return_int = py_function; \ |
201 | | - if (!return_int) { \ |
202 | | - return NULL; \ |
203 | | - } \ |
204 | | - return_int; \ |
205 | | - }) |
206 | | - |
207 | | -#define CALL_PYTHON_EXPECT_NONE(py_function) \ |
208 | | - ({ \ |
209 | | - PyObject* none_obj = py_function; \ |
210 | | - if (none_obj == NULL) { \ |
211 | | - return NULL; \ |
212 | | - } \ |
213 | | - Py_DECREF(none_obj); \ |
214 | | - }) |
215 | | - |
216 | | -#define CALL_PYTHON_CHECK_RETURN_NEG1(py_function) \ |
217 | | - ({ \ |
218 | | - PyObject* py_object = py_function; \ |
219 | | - if (py_object == NULL) { \ |
220 | | - return -1; \ |
221 | | - } \ |
222 | | - py_object; \ |
223 | | - }) |
224 | | - |
225 | 186 | #ifndef Py_LIMITED_API |
226 | 187 | #define SD_BUS_DEALLOC_TAIL \ |
227 | 188 | PyTypeObject* self_type = Py_TYPE(self); \ |
|
0 commit comments