From 9b8d558acc52b49cef4ccbc7d7361e4244d572b6 Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Wed, 9 Nov 2016 14:37:00 -0800 Subject: Use Py_BuildValue instead of manually building dicts Change-Id: I6077b1f82a910e0d939cac7eb0d05bcbcdabdeb7 --- src/c/pyeclib_c/pyeclib_c.c | 50 +++++++-------------------------------------- 1 file changed, 7 insertions(+), 43 deletions(-) diff --git a/src/c/pyeclib_c/pyeclib_c.c b/src/c/pyeclib_c/pyeclib_c.c index 8e1a23c..b3de0aa 100644 --- a/src/c/pyeclib_c/pyeclib_c.c +++ b/src/c/pyeclib_c/pyeclib_c.c @@ -355,18 +355,6 @@ pyeclib_c_get_segment_info(PyObject *self, PyObject *args) pyeclib_t *pyeclib_handle = NULL; PyObject *ret_dict = NULL; /* python dictionary to return */ - // Prepare variables for return dict to cleanup on exit - PyObject *segment_size_key = NULL; - PyObject *segment_size_value = NULL; - PyObject *last_segment_size_key = NULL; - PyObject *last_segment_size_value = NULL; - PyObject *fragment_size_key = NULL; - PyObject *fragment_size_value = NULL; - PyObject *last_fragment_size_key = NULL; - PyObject *last_fragment_size_value = NULL; - PyObject *num_segments_key = NULL; - PyObject *num_segments_value = NULL; - int data_len; /* data length from user in bytes */ int segment_size, last_segment_size; /* segment sizes in bytes */ int num_segments; /* total number of segments */ @@ -462,42 +450,18 @@ pyeclib_c_get_segment_info(PyObject *self, PyObject *args) fragment_size += sizeof(fragment_header_t); /* Create and return the python dictionary of segment info */ - ret_dict = PyDict_New(); + ret_dict = Py_BuildValue( + "{s:i, s:i, s:i, s:i, s:i}", + "segment_size", segment_size, + "last_segment_size", last_segment_size, + "fragment_size", fragment_size, + "last_fragment_size", last_fragment_size, + "num_segments", num_segments); if (NULL == ret_dict) { goto error; - } else { - if((segment_size_key = PyString_FromString("segment_size\0")) == NULL || - (segment_size_value = PyInt_FromLong(segment_size)) == NULL || - PyDict_SetItem(ret_dict, segment_size_key, segment_size_value)) goto error; - - if((last_segment_size_key = PyString_FromString("last_segment_size\0")) == NULL || - (last_segment_size_value = PyInt_FromLong(last_segment_size)) == NULL || - PyDict_SetItem(ret_dict, last_segment_size_key, last_segment_size_value)) goto error; - - if((fragment_size_key = PyString_FromString("fragment_size\0")) == NULL || - (fragment_size_value = PyInt_FromLong(fragment_size)) == NULL || - PyDict_SetItem(ret_dict, fragment_size_key, fragment_size_value)) goto error; - - if((last_fragment_size_key = PyString_FromString("last_fragment_size\0")) == NULL || - (last_fragment_size_value = PyInt_FromLong(last_fragment_size)) == NULL || - PyDict_SetItem(ret_dict, last_fragment_size_key, last_fragment_size_value)) goto error; - - if((num_segments_key = PyString_FromString("num_segments\0")) == NULL || - (num_segments_value = PyInt_FromLong(num_segments)) == NULL || - PyDict_SetItem(ret_dict, num_segments_key, num_segments_value)) goto error; } exit: - Py_XDECREF(segment_size_key); - Py_XDECREF(segment_size_value); - Py_XDECREF(last_segment_size_key); - Py_XDECREF(last_segment_size_value); - Py_XDECREF(fragment_size_key); - Py_XDECREF(fragment_size_value); - Py_XDECREF(last_fragment_size_key); - Py_XDECREF(last_fragment_size_value); - Py_XDECREF(num_segments_key); - Py_XDECREF(num_segments_value); return ret_dict; error: -- cgit v1.2.1