]> jfr.im git - irc/evilnet/x3.git/commitdiff
mod-python: refactor dict_t construction logic
authorhstuart <redacted>
Wed, 7 Oct 2009 09:20:17 +0000 (09:20 +0000)
committerhstuart <redacted>
Wed, 7 Oct 2009 09:20:17 +0000 (09:20 +0000)
ChangeLog
src/mod-python.c

index 2a5b9a5d242a2b2926fbd7ea7258766af1d3dd3f..9f0ed80f2918738c8fe53de3f6d48d7cc349f400 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,10 @@
 /***********************************************************************
 X3 ChangeLog
 
 /***********************************************************************
 X3 ChangeLog
 
+1009-10-07  Henrik Stuart <evilnet@hstuart.dk>
+
+       * src/mod-python.c: refactor dict_t construction logic.
+
 2009-10-07  Henrik Stuart <evilnet@hstuart.dk>
 
        * src/mod-python.c: improve error logic for emb_get_channel.
 2009-10-07  Henrik Stuart <evilnet@hstuart.dk>
 
        * src/mod-python.c: improve error logic for emb_get_channel.
index 97b74df03176ae6a903890d0fb6ffbe8deb45363..80c9401185506fcadb5a6f474cf3bef446c631fd 100644 (file)
@@ -112,25 +112,19 @@ static int _dict_iter_fill_tuple(char const* key, UNUSED_ARG(void* data), void*
     return 0;
 }
 
     return 0;
 }
 
-/* get a tuple with all users in it */
 static PyObject*
 static PyObject*
-emb_get_users(UNUSED_ARG(PyObject *self), PyObject *args) {
+pyobj_from_dict_t(dict_t d) {
     PyObject* retval;
     PyObject* retval;
-    size_t num_clients, n = 0;
+    size_t n = 0;
     struct _tuple_dict_extra extra;
 
     struct _tuple_dict_extra extra;
 
-    if (!PyArg_ParseTuple(args, ""))
-        return NULL;
-
-    num_clients = dict_size(clients);
-    retval = PyTuple_New(num_clients);
-    if (retval == NULL)
+    if ((retval = PyTuple_New(dict_size(d))) == NULL)
         return NULL;
 
     extra.extra = &n;
     extra.data = retval;
 
         return NULL;
 
     extra.extra = &n;
     extra.data = retval;
 
-    if (dict_foreach(clients, _dict_iter_fill_tuple, (void*)&extra) != NULL) {
+    if (dict_foreach(d, _dict_iter_fill_tuple, (void*)&extra) != NULL) {
         pyobj_release_tuple(retval, n);
         return NULL;
     }
         pyobj_release_tuple(retval, n);
         return NULL;
     }
@@ -138,78 +132,38 @@ emb_get_users(UNUSED_ARG(PyObject *self), PyObject *args) {
     return retval;
 }
 
     return retval;
 }
 
-/* get a tuple with all channels in it */
+/* get a tuple with all users in it */
 static PyObject*
 static PyObject*
-emb_get_channels(UNUSED_ARG(PyObject* self), PyObject* args) {
-    PyObject* retval;
-    size_t num_channels, n = 0;
-    struct _tuple_dict_extra extra;
-
+emb_get_users(UNUSED_ARG(PyObject *self), PyObject *args) {
     if (!PyArg_ParseTuple(args, ""))
         return NULL;
 
     if (!PyArg_ParseTuple(args, ""))
         return NULL;
 
-    num_channels = dict_size(channels);
-    retval = PyTuple_New(num_channels);
-    if (retval == NULL)
-        return NULL;
-
-    extra.extra = &n;
-    extra.data = retval;
+    return pyobj_from_dict_t(clients);
+}
 
 
-    if (dict_foreach(channels, _dict_iter_fill_tuple, (void*)&extra) != NULL) {
-        pyobj_release_tuple(retval, n);
+/* get a tuple with all channels in it */
+static PyObject*
+emb_get_channels(UNUSED_ARG(PyObject* self), PyObject* args) {
+    if (!PyArg_ParseTuple(args, ""))
         return NULL;
         return NULL;
-    }
 
 
-    return retval;
+    return pyobj_from_dict_t(channels);
 }
 
 static PyObject*
 emb_get_servers(UNUSED_ARG(PyObject* self), PyObject* args) {
 }
 
 static PyObject*
 emb_get_servers(UNUSED_ARG(PyObject* self), PyObject* args) {
-    PyObject* retval;
-    size_t n = 0;
-    struct _tuple_dict_extra extra;
-
     if (!PyArg_ParseTuple(args, ""))
         return NULL;
 
     if (!PyArg_ParseTuple(args, ""))
         return NULL;
 
-    retval = PyTuple_New(dict_size(servers));
-    if (retval == NULL)
-        return NULL;
-
-    extra.extra = &n;
-    extra.data = retval;
-
-    if (dict_foreach(servers, _dict_iter_fill_tuple, (void*)&extra) != NULL) {
-        pyobj_release_tuple(retval, n);
-        return NULL;
-    }
-
-    return retval;
+    return pyobj_from_dict_t(servers);
 }
 
 static PyObject*
 emb_get_accounts(UNUSED_ARG(PyObject* self), PyObject* args) {
 }
 
 static PyObject*
 emb_get_accounts(UNUSED_ARG(PyObject* self), PyObject* args) {
-    PyObject* retval;
-    size_t n = 0;
-    struct _tuple_dict_extra extra;
-
     if (!PyArg_ParseTuple(args, ""))
         return NULL;
 
     if (!PyArg_ParseTuple(args, ""))
         return NULL;
 
-    retval = PyTuple_New(dict_size(nickserv_handle_dict));
-    if (retval == NULL)
-        return NULL;
-
-    extra.extra = &n;
-    extra.data = retval;
-
-    if (dict_foreach(nickserv_handle_dict, _dict_iter_fill_tuple, (void*)&extra) != NULL) {
-        pyobj_release_tuple(retval, n);
-        return NULL;
-    }
-
-    return retval;
+    return pyobj_from_dict_t(nickserv_handle_dict);
 }
 
 static PyObject*
 }
 
 static PyObject*