To parse `PyObject *args` into one TDBDATA.

This already reduces boilerplate code, but will help more in the next
patch, where there will be several fuctions (chainlock_*) which takes
key as the only argument.

Cc: [email protected]
Signed-off-by: Kirill Smelkov <[email protected]>
---
 lib/tdb/pytdb.c |   30 ++++++++++++------------------
 1 files changed, 12 insertions(+), 18 deletions(-)

diff --git a/lib/tdb/pytdb.c b/lib/tdb/pytdb.c
index f5cb307..42df639 100644
--- a/lib/tdb/pytdb.c
+++ b/lib/tdb/pytdb.c
@@ -69,6 +69,14 @@ static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
     }
 }
 
+/* parse `PyObject *args` into TDB_DATA and store result in *dptr */
+#define        PyTdb_ARGS_AS_TDBDATA(args, dptr) do {  \
+       PyObject *py_obj;       \
+       if (!PyArg_ParseTuple((args), "O", &py_obj))    \
+               return NULL;    \
+       *(dptr) = PyString_AsTDB_DATA(py_obj);  \
+} while (0)
+
 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
        if (ret != 0) { \
                PyErr_SetTDBError(tdb); \
@@ -202,11 +210,7 @@ static PyObject *obj_close(PyTdbObject *self)
 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
 {
        TDB_DATA key;
-       PyObject *py_key;
-       if (!PyArg_ParseTuple(args, "O", &py_key))
-               return NULL;
-
-       key = PyString_AsTDB_DATA(py_key);
+       PyTdb_ARGS_AS_TDBDATA(args, &key);
 
        return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key));
 }
@@ -235,24 +239,17 @@ static PyObject *obj_firstkey(PyTdbObject *self)
 static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
 {
        TDB_DATA key;
-       PyObject *py_key;
-       if (!PyArg_ParseTuple(args, "O", &py_key))
-               return NULL;
+       PyTdb_ARGS_AS_TDBDATA(args, &key);
 
-       key = PyString_AsTDB_DATA(py_key);
-       
        return PyString_FromTDB_DATA(tdb_nextkey(self->ctx, key));
 }
 
 static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
 {
        TDB_DATA key;
-       PyObject *py_key;
        int ret;
-       if (!PyArg_ParseTuple(args, "O", &py_key))
-               return NULL;
+       PyTdb_ARGS_AS_TDBDATA(args, &key);
 
-       key = PyString_AsTDB_DATA(py_key);
        ret = tdb_delete(self->ctx, key);
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
        Py_RETURN_NONE;
@@ -262,11 +259,8 @@ static PyObject *obj_has_key(PyTdbObject *self, PyObject 
*args)
 {
        TDB_DATA key;
        int ret;
-       PyObject *py_key;
-       if (!PyArg_ParseTuple(args, "O", &py_key))
-               return NULL;
+       PyTdb_ARGS_AS_TDBDATA(args, &key);
 
-       key = PyString_AsTDB_DATA(py_key);
        ret = tdb_exists(self->ctx, key);
        if (ret != TDB_ERR_NOEXIST) {
                PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-- 
1.7.3.1.50.g1e633



-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to