From 80c7effc57e492fed86c50040a6ad032346283ef Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Jun 17 2019 14:04:28 +0000 Subject: python: Streamline hosts_to_list This function used error prone style, using local loop variable in the scope of the function, making cleanup more risky. Move ls_entry info the loop scope, and use PyList_SetItem(), stealing the reference to ls_entry, so we need to decrease the reference count only on errors. To use PyList_SetItem() we need to allocate the entire list upfront, which is good idea anyway. While refactoring, remove some useless and wrong comments. Signed-off-by: Nir Soffer --- diff --git a/python/sanlock.c b/python/sanlock.c index 4c3e0a0..4f80379 100644 --- a/python/sanlock.c +++ b/python/sanlock.c @@ -271,36 +271,31 @@ set_error(PyObject* exception, const char* format, PyObject* obj) static PyObject * hosts_to_list(struct sanlk_host *hss, int hss_count) { - PyObject *ls_list = NULL, *ls_entry = NULL; - - /* prepare the dictionary holding the information */ - if ((ls_list = PyList_New(0)) == NULL) + PyObject *ls_list = PyList_New(hss_count); + if (ls_list == NULL) goto exit_fail; for (int i = 0; i < hss_count; i++) { - - /* fill the dictionary information */ - ls_entry = Py_BuildValue( - "{s:K,s:K,s:K,s:I,s:I}", - "host_id", hss[i].host_id, - "generation", hss[i].generation, - "timestamp", hss[i].timestamp, - "io_timeout", hss[i].io_timeout, - "flags", hss[i].flags); + PyObject *ls_entry = Py_BuildValue( + "{s:K,s:K,s:K,s:I,s:I}", + "host_id", hss[i].host_id, + "generation", hss[i].generation, + "timestamp", hss[i].timestamp, + "io_timeout", hss[i].io_timeout, + "flags", hss[i].flags); if (ls_entry == NULL) goto exit_fail; - if (PyList_Append(ls_list, ls_entry) != 0) + /* Steals reference to ls_entry. */ + if (PyList_SetItem(ls_list, i, ls_entry) != 0) { + Py_DECREF(ls_entry); goto exit_fail; - - Py_DECREF(ls_entry); + } } return ls_list; - /* failure */ exit_fail: - Py_XDECREF(ls_entry); Py_XDECREF(ls_list); return NULL; }