From: Amit Bawer <abawer(a)redhat.com>
Use Py_BuildValue("{...}",...) for creating dictionary information
for resource.
Using helper to determine the build value format:
- Py2: Handle lockspace and resource name as PyString with "s" format.
- Py3: Handle lockspace and resource name as PyBytes with "y" format.
Current status:
- Py2: will build and pass tests.
- Py3: build will fail for missing respective API calls.
---
python/sanlock.c | 37 +++++++++++--------------------------
1 file changed, 11 insertions(+), 26 deletions(-)
diff --git a/python/sanlock.c b/python/sanlock.c
index 5870081..6477afe 100644
--- a/python/sanlock.c
+++ b/python/sanlock.c
@@ -536,11 +536,11 @@ py_read_resource(PyObject *self __unused, PyObject *args, PyObject
*keywds)
{
int rv, rs_len, sector = SECTOR_SIZE_512;
long align = ALIGNMENT_1M;
const char *path;
struct sanlk_resource *rs;
- PyObject *rs_info = NULL, *rs_entry = NULL;
+ PyObject *rs_info = NULL;
static char *kwlist[] = {"path", "offset", "align",
"sector", NULL};
/* allocate the needed memory for the resource and one disk */
rs_len = sizeof(struct sanlk_resource) + sizeof(struct sanlk_disk);
@@ -580,35 +580,20 @@ py_read_resource(PyObject *self __unused, PyObject *args, PyObject
*keywds)
__set_exception(rv, "Sanlock resource read failure");
goto exit_fail;
}
/* prepare the dictionary holding the information */
- if ((rs_info = PyDict_New()) == NULL)
- goto exit_fail;
-
- /* fill the dictionary information: lockspace */
- if ((rs_entry = PyString_FromString(rs->lockspace_name)) == NULL)
- goto exit_fail;
- rv = PyDict_SetItemString(rs_info, "lockspace", rs_entry);
- Py_DECREF(rs_entry);
- if (rv != 0)
- goto exit_fail;
-
- /* fill the dictionary information: resource */
- if ((rs_entry = PyString_FromString(rs->name)) == NULL)
- goto exit_fail;
- rv = PyDict_SetItemString(rs_info, "resource", rs_entry);
- Py_DECREF(rs_entry);
- if (rv != 0)
- goto exit_fail;
-
- /* fill the dictionary information: version */
- if ((rs_entry = PyLong_FromUnsignedLong(rs->lver)) == NULL)
- goto exit_fail;
- rv = PyDict_SetItemString(rs_info, "version", rs_entry);
- Py_DECREF(rs_entry);
- if (rv != 0)
+ rs_info = Py_BuildValue(
+#if PY_MAJOR_VERSION == 2
+ "{s:s,s:s,s:K}",
+#else
+ "{s:y,s:y,s:K}",
+#endif
+ "lockspace", rs->lockspace_name,
+ "resource", rs->name,
+ "version", rs->lver);
+ if (rs_info == NULL)
goto exit_fail;
/* success */
free(rs);
return rs_info;
--
2.17.2