This is an automated email from the git hooks/post-receive script.
nsoffer pushed a commit to branch master in repository sanlock.
commit 80c7effc57e492fed86c50040a6ad032346283ef Author: Nir Soffer nsoffer@redhat.com AuthorDate: Fri Jun 14 23:00:18 2019 +0300
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 nsoffer@redhat.com --- python/sanlock.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-)
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; }
sanlock-devel@lists.fedorahosted.org