diff options
author | Christoph Reiter <reiter.christoph@gmail.com> | 2015-02-18 19:30:30 +0100 |
---|---|---|
committer | Christoph Reiter <creiter@src.gnome.org> | 2015-07-17 17:41:09 +0200 |
commit | 79d23e9accd331d954007dcae6b6b7fa2837cfa7 (patch) | |
tree | 265ed504a983afe4b6a268eb649429c3cb0c287e | |
parent | 8aa3d5935b4541be6e76e8792e58bb301fa4f7d1 (diff) | |
download | pygobject-79d23e9accd331d954007dcae6b6b7fa2837cfa7.tar.gz |
Allow passing unicode lists to GStrv properties on Python 2
https://bugzilla.gnome.org/show_bug.cgi?id=744745
-rw-r--r-- | gi/pygi-value.c | 41 | ||||
-rw-r--r-- | tests/test_properties.py | 8 |
2 files changed, 39 insertions, 10 deletions
diff --git a/gi/pygi-value.c b/gi/pygi-value.c index 7fdf7675..79ee596f 100644 --- a/gi/pygi-value.c +++ b/gi/pygi-value.c @@ -933,17 +933,40 @@ pyg_strv_to_gvalue(GValue *value, PyObject *obj) Py_ssize_t argc, i; gchar **argv; - if (!(PyTuple_Check(obj) || PyList_Check(obj))) + if (!(PyTuple_Check (obj) || PyList_Check (obj))) return -1; - argc = PySequence_Length(obj); - for (i = 0; i < argc; ++i) - if (!PYGLIB_PyUnicode_Check(PySequence_Fast_GET_ITEM(obj, i))) - return -1; - argv = g_new(gchar *, argc + 1); - for (i = 0; i < argc; ++i) - argv[i] = g_strdup(PYGLIB_PyUnicode_AsString(PySequence_Fast_GET_ITEM(obj, i))); + argc = PySequence_Length (obj); + argv = g_new (gchar *, argc + 1); + for (i = 0; i < argc; ++i) { + PyObject* item = PySequence_Fast_GET_ITEM (obj, i); + /* same as _pygi_marshal_from_py_utf8 */ + if (PyUnicode_Check (item)) { + PyObject *pystr_obj = PyUnicode_AsUTF8String (item); + if (!pystr_obj) { + goto error; + } + argv[i] = g_strdup (PYGLIB_PyBytes_AsString (pystr_obj)); + Py_DECREF (pystr_obj); + } +#if PY_VERSION_HEX < 0x03000000 + else if (PyString_Check (item)) { + argv[i] = g_strdup (PyString_AsString (item)); + } +#endif + else { + goto error; + } + } + argv[i] = NULL; - g_value_take_boxed(value, argv); + g_value_take_boxed (value, argv); return 0; + +error: + for (i = i - 1; i >= 0; i--) { + g_free (argv[i]); + } + g_free (argv); + return -1; } diff --git a/tests/test_properties.py b/tests/test_properties.py index ce184fed..d8d5a10c 100644 --- a/tests/test_properties.py +++ b/tests/test_properties.py @@ -40,7 +40,7 @@ else: TEST_UTF8 = "♥" UNICODE_UTF8 = TEST_UTF8 -from compathelper import _long +from compathelper import _long, _unicode from helper import capture_glib_warnings, capture_output @@ -1109,6 +1109,12 @@ class CPropertiesTestBase(object): obj = GIMarshallingTests.PropertiesObject(some_strv=['hello', 'world']) self.assertEqual(self.get_prop(obj, 'some-strv'), ['hello', 'world']) + # unicode on py2 + obj = GIMarshallingTests.PropertiesObject(some_strv=[_unicode('foo')]) + self.assertEqual(self.get_prop(obj, 'some-strv'), [_unicode('foo')]) + self.assertRaises(TypeError, self.set_prop, self.obj, 'some-strv', + [_unicode('foo'), 1]) + def test_boxed_struct(self): self.assertEqual(self.get_prop(self.obj, 'some-boxed-struct'), None) |