diff options
author | Gian Mario Tagliaretti <gianmt@gnome.org> | 2009-03-28 22:05:10 +0000 |
---|---|---|
committer | Gian Mario Tagliaretti <gianmt@src.gnome.org> | 2009-03-28 22:05:10 +0000 |
commit | 96bf175f8217e20ca449d9a5aeb8ea90b5844383 (patch) | |
tree | 6b735b73a874162c234ec1cb654a0804240c1470 /gio/pygio-utils.c | |
parent | 3ed45914ee441d76822c26bf2f34d79fd3c2d7df (diff) | |
download | pygobject-96bf175f8217e20ca449d9a5aeb8ea90b5844383.tar.gz pygobject-96bf175f8217e20ca449d9a5aeb8ea90b5844383.tar.xz pygobject-96bf175f8217e20ca449d9a5aeb8ea90b5844383.zip |
add a couple of convinence functions to convert from/to a python list and
2009-03-28 Gian Mario Tagliaretti <gianmt@gnome.org>
* gio/pygio-utils.[hc]: (strv_to_pylist) (pylist_to_strv) add a couple
of convinence functions to convert from/to a python list and an array
of strings.
* gio/Makefile.am
* gio/gdrive.override:
* gio/gio.override: Strip GDrive overrides and
wrap g_drive_enumerate_identifiers
* tests/test_gio.py:
* gio/gvolume.override: wrap g_volume_enumerate_identifiers
* gio/gio.defs: add missing g_drive_get_identifier and
g_drive_enumerate_identifiers
svn path=/trunk/; revision=1035
Diffstat (limited to 'gio/pygio-utils.c')
-rw-r--r-- | gio/pygio-utils.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/gio/pygio-utils.c b/gio/pygio-utils.c index 313b147..4c3d0bf 100644 --- a/gio/pygio-utils.c +++ b/gio/pygio-utils.c @@ -128,3 +128,81 @@ pygio_pylist_to_uri_glist(PyObject *pyfile_list) return file_list; } + +/** + * strv_to_pylist: + * @strv: array of strings + * + * Returns: A python list of strings + */ +PyObject * +strv_to_pylist (char **strv) +{ + gsize len, i; + PyObject *list; + + len = strv ? g_strv_length (strv) : 0; + list = PyList_New (len); + + for (i = 0; i < len; i++) + PyList_SetItem (list, i, PyString_FromString (strv[i])); + + return list; +} + +/** + * pylist_to_strv: + * @strvp: a pointer to an array where return strings. + * + * Returns: TRUE if the list of strings could be converted, FALSE otherwise. + */ +gboolean +pylist_to_strv (PyObject *list, + char ***strvp) +{ + int i, len; + char **ret; + + *strvp = NULL; + + if (list == Py_None) + return TRUE; + + if (!PySequence_Check (list)) + { + PyErr_Format (PyExc_TypeError, "argument must be a list or tuple of strings"); + return FALSE; + } + + if ((len = PySequence_Size (list)) < 0) + return FALSE; + + ret = g_new (char*, len + 1); + for (i = 0; i <= len; ++i) + ret[i] = NULL; + + for (i = 0; i < len; ++i) + { + PyObject *item = PySequence_GetItem (list, i); + + if (!item) + { + g_strfreev (ret); + return FALSE; + } + + if (!PyString_Check (item)) + { + Py_DECREF (item); + g_strfreev (ret); + PyErr_Format (PyExc_TypeError, "argument must be a list of strings"); + return FALSE; + } + + ret[i] = g_strdup (PyString_AsString (item)); + Py_DECREF (item); + } + + *strvp = ret; + return TRUE; +} |