Skip to content

Commit

Permalink
Clean up macro defenitions
Browse files Browse the repository at this point in the history
Re-use FAIL_ACTION macros.

Also remove all goto fails except `set_dbus_error_from_python_exception`
  • Loading branch information
igo95862 committed Jun 25, 2022
1 parent 7187bbf commit f57749f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 100 deletions.
127 changes: 44 additions & 83 deletions src/sdbus/sd_bus_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,7 @@
return NULL; \
}

#define CALL_PYTHON_AND_CHECK(py_function) \
({ \
PyObject* new_object = py_function; \
if (new_object == NULL) { \
return NULL; \
} \
new_object; \
})

#define CALL_PYTHON_GOTO_FAIL(py_function) \
({ \
PyObject* new_object = py_function; \
if (new_object == NULL) { \
goto fail; \
} \
new_object; \
})
// Call Python macros

#define CALL_PYTHON_FAIL_ACTION(py_function, action) \
({ \
Expand All @@ -64,6 +48,37 @@
new_object; \
})

#define CALL_PYTHON_AND_CHECK(py_function) CALL_PYTHON_FAIL_ACTION(py_function, return NULL)
#define CALL_PYTHON_CHECK_RETURN_NEG1(py_function) CALL_PYTHON_FAIL_ACTION(py_function, return -1)
#define CALL_PYTHON_GOTO_FAIL(py_function) CALL_PYTHON_FAIL_ACTION(py_function, goto fail)

#define CALL_PYTHON_INT_CHECK(py_function) \
({ \
int return_int = py_function; \
if (return_int < 0) { \
return NULL; \
} \
return_int; \
})

#define CALL_PYTHON_BOOL_CHECK(py_function) \
({ \
int return_int = py_function; \
if (!return_int) { \
return NULL; \
} \
return_int; \
})

#define CALL_PYTHON_EXPECT_NONE(py_function) \
({ \
PyObject* none_obj = py_function; \
if (none_obj == NULL) { \
return NULL; \
} \
Py_DECREF(none_obj); \
})

#define PYTHON_ERR_OCCURED \
if (PyErr_Occurred()) { \
return NULL; \
Expand Down Expand Up @@ -96,60 +111,42 @@
return_int; \
})

#define SD_BUS_PY_UNICODE_AS_BYTES(py_unicode) \
#define SD_BUS_PY_UNICODE_AS_BYTES_ERROR_ACTION(py_unicode, action) \
({ \
PyObject* utf_8_bytes = PyUnicode_AsUTF8String(py_unicode); \
if (utf_8_bytes == NULL) { \
return NULL; \
action; \
} \
utf_8_bytes; \
})

#define SD_BUS_PY_UNICODE_AS_BYTES_GOTO_FAIL(py_unicode) \
({ \
PyObject* utf_8_bytes = PyUnicode_AsUTF8String(py_unicode); \
if (utf_8_bytes == NULL) { \
goto fail; \
} \
utf_8_bytes; \
})
#define SD_BUS_PY_UNICODE_AS_BYTES(py_unicode) SD_BUS_PY_UNICODE_AS_BYTES_ERROR_ACTION(py_unicode, return NULL)
#define SD_BUS_PY_UNICODE_AS_BYTES_GOTO_FAIL(py_unicode) SD_BUS_PY_UNICODE_AS_BYTES_ERROR_ACTION(py_unicode, goto fail)

#define SD_BUS_PY_BYTES_AS_CHAR_PTR(py_bytes) \
#define SD_BUS_PY_BYTES_AS_CHAR_PTR_ERROR_ACTION(py_bytes, action) \
({ \
const char* new_char_ptr = PyBytes_AsString(py_bytes); \
if (new_char_ptr == NULL) { \
return NULL; \
action; \
} \
new_char_ptr; \
})

#define SD_BUS_PY_BYTES_AS_CHAR_PTR_GOTO_FAIL(py_bytes) \
({ \
const char* new_char_ptr = PyBytes_AsString(py_bytes); \
if (new_char_ptr == NULL) { \
goto fail; \
} \
new_char_ptr; \
})
#define SD_BUS_PY_BYTES_AS_CHAR_PTR(py_bytes) SD_BUS_PY_BYTES_AS_CHAR_PTR_ERROR_ACTION(py_bytes, return NULL)
#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)

#ifndef Py_LIMITED_API
#define SD_BUS_PY_UNICODE_AS_CHAR_PTR(py_object) \
#define SD_BUS_PY_UNICODE_AS_CHAR_PTR_ERROR_ACTION(py_object, action) \
({ \
const char* new_char_ptr = PyUnicode_AsUTF8(py_object); \
if (new_char_ptr == NULL) { \
return NULL; \
action; \
} \
new_char_ptr; \
})

#define SD_BUS_PY_UNICODE_AS_CHAR_PTR_GOTO_FAIL(py_object) \
({ \
const char* new_char_ptr = PyUnicode_AsUTF8(py_object); \
if (new_char_ptr == NULL) { \
goto fail; \
} \
new_char_ptr; \
})
#define SD_BUS_PY_UNICODE_AS_CHAR_PTR(py_object) SD_BUS_PY_UNICODE_AS_CHAR_PTR_ERROR_ACTION(py_object, return NULL)
#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)

#define SD_BUS_PY_UNICODE_AS_CHAR_PTR_OPTIONAL(py_object) \
({ \
Expand Down Expand Up @@ -186,42 +183,6 @@
next_object; \
})

#define CALL_PYTHON_INT_CHECK(py_function) \
({ \
int return_int = py_function; \
if (return_int < 0) { \
return NULL; \
} \
return_int; \
})

#define CALL_PYTHON_BOOL_CHECK(py_function) \
({ \
int return_int = py_function; \
if (!return_int) { \
return NULL; \
} \
return_int; \
})

#define CALL_PYTHON_EXPECT_NONE(py_function) \
({ \
PyObject* none_obj = py_function; \
if (none_obj == NULL) { \
return NULL; \
} \
Py_DECREF(none_obj); \
})

#define CALL_PYTHON_CHECK_RETURN_NEG1(py_function) \
({ \
PyObject* py_object = py_function; \
if (py_object == NULL) { \
return -1; \
} \
py_object; \
})

#ifndef Py_LIMITED_API
#define SD_BUS_DEALLOC_TAIL \
PyTypeObject* self_type = Py_TYPE(self); \
Expand Down
26 changes: 9 additions & 17 deletions src/sdbus/sd_bus_internals_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,16 +417,14 @@ static int _SdBusInterface_property_get_callback(sd_bus* Py_UNUSED(bus),
PyObject* get_call = NULL;
PyObject* new_message CLEANUP_PY_OBJECT = NULL;

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

new_message = CALL_PYTHON_GOTO_FAIL(SD_BUS_PY_CLASS_DUNDER_NEW(SdBusMessage_class));
new_message = METHOD_CALLBACK_ERROR_CHECK(SD_BUS_PY_CLASS_DUNDER_NEW(SdBusMessage_class));
_SdBusMessage_set_messsage((SdBusMessageObject*)new_message, reply);

Py_XDECREF(CALL_PYTHON_GOTO_FAIL(PyObject_CallFunctionObjArgs(get_call, new_message, NULL)));
Py_XDECREF(METHOD_CALLBACK_ERROR_CHECK(PyObject_CallFunctionObjArgs(get_call, new_message, NULL)));
return 0;
fail:
return set_dbus_error_from_python_exception(ret_error);
}

static int _SdBusInterface_property_set_callback(sd_bus* Py_UNUSED(bus),
Expand All @@ -437,19 +435,13 @@ static int _SdBusInterface_property_set_callback(sd_bus* Py_UNUSED(bus),
void* userdata,
sd_bus_error* ret_error) {
SdBusInterfaceObject* self = userdata;
PyObject* property_name_bytes CLEANUP_PY_OBJECT = NULL;
PyObject* new_message CLEANUP_PY_OBJECT = NULL;
property_name_bytes = PyBytes_FromString(property);
if (NULL == property_name_bytes) {
goto fail;
}
PyObject* set_call = CALL_PYTHON_GOTO_FAIL(PyDict_GetItem(self->property_set_dict, property_name_bytes));
PyObject* property_name_bytes CLEANUP_PY_OBJECT = METHOD_CALLBACK_ERROR_CHECK(PyBytes_FromString(property));

PyObject* set_call = METHOD_CALLBACK_ERROR_CHECK(PyDict_GetItem(self->property_set_dict, property_name_bytes));

new_message = CALL_PYTHON_GOTO_FAIL(SD_BUS_PY_CLASS_DUNDER_NEW(SdBusMessage_class));
PyObject* new_message CLEANUP_PY_OBJECT = METHOD_CALLBACK_ERROR_CHECK(SD_BUS_PY_CLASS_DUNDER_NEW(SdBusMessage_class));
_SdBusMessage_set_messsage((SdBusMessageObject*)new_message, value);

Py_XDECREF(CALL_PYTHON_GOTO_FAIL(PyObject_CallFunctionObjArgs(set_call, new_message, NULL)));
Py_XDECREF(METHOD_CALLBACK_ERROR_CHECK(PyObject_CallFunctionObjArgs(set_call, new_message, NULL)));
return 0;
fail:
return set_dbus_error_from_python_exception(ret_error);
}

0 comments on commit f57749f

Please sign in to comment.