diff options
Diffstat (limited to 'bindings/python/wrapper_top.c')
| -rw-r--r-- | bindings/python/wrapper_top.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/bindings/python/wrapper_top.c b/bindings/python/wrapper_top.c index a24a2dba..24eb93ac 100644 --- a/bindings/python/wrapper_top.c +++ b/bindings/python/wrapper_top.c @@ -18,8 +18,10 @@ PyMODINIT_FUNC init_lasso(void); G_GNUC_UNUSED static PyObject* get_pystring_from_xml_node(xmlNode *xmlnode); G_GNUC_UNUSED static xmlNode* get_xml_node_from_pystring(PyObject *string); G_GNUC_UNUSED static PyObject* get_dict_from_hashtable_of_objects(GHashTable *value); +G_GNUC_UNUSED static PyObject* get_dict_from_hashtable_of_strings(GHashTable *value); G_GNUC_UNUSED static PyObject* PyGObjectPtr_New(GObject *obj); G_GNUC_UNUSED static void set_hashtable_of_pygobject(GHashTable *a_hash, PyObject *dict); +G_GNUC_UNUSED static void set_hashtable_of_strings(GHashTable *a_hash, PyObject *dict); G_GNUC_UNUSED static void set_list_of_strings(GList **a_list, PyObject *seq); G_GNUC_UNUSED static void set_list_of_xml_nodes(GList **a_list, PyObject *seq); G_GNUC_UNUSED static void set_list_of_pygobject(GList **a_list, PyObject *seq); @@ -73,6 +75,34 @@ get_dict_from_hashtable_of_objects(GHashTable *value) } static PyObject* +get_dict_from_hashtable_of_strings(GHashTable *value) +{ + GList *keys, *begin; + PyObject *dict,*proxy; + char *item_value; + PyObject *item; + + dict = PyDict_New(); + + begin = keys = g_hash_table_get_keys(value); + for (; keys; keys = g_list_next(keys)) { + item_value = g_hash_table_lookup(value, keys->data); + if (item_value) { + item = PyString_FromString(item_value); + PyDict_SetItemString(dict, (char*)keys->data, item); + Py_DECREF(item); + } else { + PyErr_Warn(PyExc_RuntimeWarning, "hashtable contains a null value"); + } + } + g_list_free(begin); + + proxy = PyDictProxy_New(dict); + Py_DECREF(dict); + return proxy; +} + +static PyObject* get_pystring_from_xml_node(xmlNode *xmlnode) { xmlOutputBufferPtr buf; @@ -165,6 +195,44 @@ failure: } } +static void +set_hashtable_of_strings(GHashTable *a_hash, PyObject *dict) +{ + PyObject *key, *value; + Py_ssize_t i; + + if (! a_hash) { + PyErr_SetString(PyExc_TypeError, "hashtable does not exist"); + return; + } + if (dict != Py_None && ! PyDict_Check(dict)) { + PyErr_SetString(PyExc_TypeError, "value should be a frozen dict"); + return; + } + i = 0; + // Increase ref count of common object between old and new + // value of the hashtable + while (PyDict_Next(dict, &i, &key, &value)) { + if (! PyString_Check(key) || ! PyString_Check(value)) + { + PyErr_SetString(PyExc_TypeError, + "value should be a dict, " + "with string keys " + "and string values"); + goto failure; + } + } + g_hash_table_remove_all (a_hash); + i = 0; + while (PyDict_Next(dict, &i, &key, &value)) { + char *ckey = PyString_AsString(key); + char *cvalue = PyString_AsString(value); + g_hash_table_insert (a_hash, g_strdup(ckey), g_strdup(cvalue)); + } +failure: + return; +} + /** Set the GList* pointer, pointed by a_list, to a pointer on a new GList * created by converting the python seq into a GList of char*. */ |
