Skip to content

Commit f57749f

Browse files
committed
Clean up macro defenitions
Re-use FAIL_ACTION macros. Also remove all goto fails except `set_dbus_error_from_python_exception`
1 parent 7187bbf commit f57749f

File tree

2 files changed

+53
-100
lines changed

2 files changed

+53
-100
lines changed

src/sdbus/sd_bus_internals.h

Lines changed: 44 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,7 @@
3737
return NULL; \
3838
}
3939

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
5741

5842
#define CALL_PYTHON_FAIL_ACTION(py_function, action) \
5943
({ \
@@ -64,6 +48,37 @@
6448
new_object; \
6549
})
6650

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+
6782
#define PYTHON_ERR_OCCURED \
6883
if (PyErr_Occurred()) { \
6984
return NULL; \
@@ -96,60 +111,42 @@
96111
return_int; \
97112
})
98113

99-
#define SD_BUS_PY_UNICODE_AS_BYTES(py_unicode) \
114+
#define SD_BUS_PY_UNICODE_AS_BYTES_ERROR_ACTION(py_unicode, action) \
100115
({ \
101116
PyObject* utf_8_bytes = PyUnicode_AsUTF8String(py_unicode); \
102117
if (utf_8_bytes == NULL) { \
103-
return NULL; \
118+
action; \
104119
} \
105120
utf_8_bytes; \
106121
})
107122

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)
116125

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) \
118127
({ \
119128
const char* new_char_ptr = PyBytes_AsString(py_bytes); \
120129
if (new_char_ptr == NULL) { \
121-
return NULL; \
130+
action; \
122131
} \
123132
new_char_ptr; \
124133
})
125134

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)
134137

135138
#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) \
137140
({ \
138141
const char* new_char_ptr = PyUnicode_AsUTF8(py_object); \
139142
if (new_char_ptr == NULL) { \
140-
return NULL; \
143+
action; \
141144
} \
142145
new_char_ptr; \
143146
})
144147

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)
153150

154151
#define SD_BUS_PY_UNICODE_AS_CHAR_PTR_OPTIONAL(py_object) \
155152
({ \
@@ -186,42 +183,6 @@
186183
next_object; \
187184
})
188185

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-
225186
#ifndef Py_LIMITED_API
226187
#define SD_BUS_DEALLOC_TAIL \
227188
PyTypeObject* self_type = Py_TYPE(self); \

src/sdbus/sd_bus_internals_interface.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -417,16 +417,14 @@ static int _SdBusInterface_property_get_callback(sd_bus* Py_UNUSED(bus),
417417
PyObject* get_call = NULL;
418418
PyObject* new_message CLEANUP_PY_OBJECT = NULL;
419419

420-
property_name_bytes = CALL_PYTHON_GOTO_FAIL(PyBytes_FromString(property));
421-
get_call = CALL_PYTHON_GOTO_FAIL(PyDict_GetItem(self->property_get_dict, property_name_bytes));
420+
property_name_bytes = METHOD_CALLBACK_ERROR_CHECK(PyBytes_FromString(property));
421+
get_call = METHOD_CALLBACK_ERROR_CHECK(PyDict_GetItem(self->property_get_dict, property_name_bytes));
422422

423-
new_message = CALL_PYTHON_GOTO_FAIL(SD_BUS_PY_CLASS_DUNDER_NEW(SdBusMessage_class));
423+
new_message = METHOD_CALLBACK_ERROR_CHECK(SD_BUS_PY_CLASS_DUNDER_NEW(SdBusMessage_class));
424424
_SdBusMessage_set_messsage((SdBusMessageObject*)new_message, reply);
425425

426-
Py_XDECREF(CALL_PYTHON_GOTO_FAIL(PyObject_CallFunctionObjArgs(get_call, new_message, NULL)));
426+
Py_XDECREF(METHOD_CALLBACK_ERROR_CHECK(PyObject_CallFunctionObjArgs(get_call, new_message, NULL)));
427427
return 0;
428-
fail:
429-
return set_dbus_error_from_python_exception(ret_error);
430428
}
431429

432430
static int _SdBusInterface_property_set_callback(sd_bus* Py_UNUSED(bus),
@@ -437,19 +435,13 @@ static int _SdBusInterface_property_set_callback(sd_bus* Py_UNUSED(bus),
437435
void* userdata,
438436
sd_bus_error* ret_error) {
439437
SdBusInterfaceObject* self = userdata;
440-
PyObject* property_name_bytes CLEANUP_PY_OBJECT = NULL;
441-
PyObject* new_message CLEANUP_PY_OBJECT = NULL;
442-
property_name_bytes = PyBytes_FromString(property);
443-
if (NULL == property_name_bytes) {
444-
goto fail;
445-
}
446-
PyObject* set_call = CALL_PYTHON_GOTO_FAIL(PyDict_GetItem(self->property_set_dict, property_name_bytes));
438+
PyObject* property_name_bytes CLEANUP_PY_OBJECT = METHOD_CALLBACK_ERROR_CHECK(PyBytes_FromString(property));
439+
440+
PyObject* set_call = METHOD_CALLBACK_ERROR_CHECK(PyDict_GetItem(self->property_set_dict, property_name_bytes));
447441

448-
new_message = CALL_PYTHON_GOTO_FAIL(SD_BUS_PY_CLASS_DUNDER_NEW(SdBusMessage_class));
442+
PyObject* new_message CLEANUP_PY_OBJECT = METHOD_CALLBACK_ERROR_CHECK(SD_BUS_PY_CLASS_DUNDER_NEW(SdBusMessage_class));
449443
_SdBusMessage_set_messsage((SdBusMessageObject*)new_message, value);
450444

451-
Py_XDECREF(CALL_PYTHON_GOTO_FAIL(PyObject_CallFunctionObjArgs(set_call, new_message, NULL)));
445+
Py_XDECREF(METHOD_CALLBACK_ERROR_CHECK(PyObject_CallFunctionObjArgs(set_call, new_message, NULL)));
452446
return 0;
453-
fail:
454-
return set_dbus_error_from_python_exception(ret_error);
455447
}

0 commit comments

Comments
 (0)