diff options
author | Tim Burke <tim.burke@gmail.com> | 2016-11-09 14:37:00 -0800 |
---|---|---|
committer | Clay Gerrard <clay.gerrard@gmail.com> | 2016-11-29 20:03:49 +0000 |
commit | 9b8d558acc52b49cef4ccbc7d7361e4244d572b6 (patch) | |
tree | 0505ccedb52e48adb4693193e156c27a20243485 /src | |
parent | cc6f4bba26ee2a1e2dc18e17131ff5b5fbe7d6d9 (diff) | |
download | pyeclib-9b8d558acc52b49cef4ccbc7d7361e4244d572b6.tar.gz |
Use Py_BuildValue instead of manually building dicts
Change-Id: I6077b1f82a910e0d939cac7eb0d05bcbcdabdeb7
Diffstat (limited to 'src')
-rw-r--r-- | src/c/pyeclib_c/pyeclib_c.c | 50 |
1 files 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: |