diff options
author | John (J5) Palmieri <johnp@redhat.com> | 2010-12-16 15:41:10 -0500 |
---|---|---|
committer | John (J5) Palmieri <johnp@redhat.com> | 2010-12-17 12:01:18 -0500 |
commit | 046cc5915286e042d1040271a90676b77632409e (patch) | |
tree | b53be45206b6e8e736d259e3d6a9d1f97492c27a | |
parent | b03cc9e0d66d8caea3cd6a63db198c43de9267e9 (diff) | |
download | pygobject-046cc5915286e042d1040271a90676b77632409e.tar.gz |
handle unicode objects in properties
* There are still some cavets in Python 2:
- properties are returned as String objects with the unicode code points
- you must add # coding=utf-8 to the top of your python file or python
will error out if it sees embeded unicode charaters (such as when
supporting python 3 and python 2 from the same source)
https://bugzilla.gnome.org/show_bug.cgi?id=620579
-rw-r--r-- | gobject/pygtype.c | 35 | ||||
-rw-r--r-- | tests/test_properties.py | 16 |
2 files changed, 42 insertions, 9 deletions
diff --git a/gobject/pygtype.c b/gobject/pygtype.c index a2ba55a8..827ba7f7 100644 --- a/gobject/pygtype.c +++ b/gobject/pygtype.c @@ -765,13 +765,20 @@ pyg_value_from_pyobject(GValue *value, PyObject *obj) } break; case G_TYPE_CHAR: - if ((tmp = PyObject_Str(obj))) - g_value_set_char(value, PYGLIB_PyUnicode_AsString(tmp)[0]); - else { +#if PY_VERSION_HEX < 0x03000000 + if (PyString_Check(obj)) { + g_value_set_char(value, PyString_AsString(obj)[0]); + } else +#endif + if (PyUnicode_Check(obj)) { + tmp = PyUnicode_AsUTF8String(obj); + g_value_set_char(value, PYGLIB_PyBytes_AsString(tmp)[0]); + Py_DECREF(tmp); + } else { PyErr_Clear(); return -1; } - Py_DECREF(tmp); + break; case G_TYPE_UCHAR: if (PYGLIB_PyLong_Check(obj)) { @@ -781,8 +788,13 @@ pyg_value_from_pyobject(GValue *value, PyObject *obj) g_value_set_uchar(value, (guchar)PYGLIB_PyLong_AsLong (obj)); else return -1; - } else if ((tmp = PyObject_Str(obj))) { - g_value_set_uchar(value, PYGLIB_PyUnicode_AsString(tmp)[0]); +#if PY_VERSION_HEX < 0x03000000 + } else if (PyString_Check(obj)) { + g_value_set_uchar(value, PyString_AsString(obj)[0]); +#endif + } else if (PyUnicode_Check(obj)) { + tmp = PyUnicode_AsUTF8String(obj); + g_value_set_uchar(value, PYGLIB_PyBytes_AsString(tmp)[0]); Py_DECREF(tmp); } else { PyErr_Clear(); @@ -877,10 +889,15 @@ pyg_value_from_pyobject(GValue *value, PyObject *obj) g_value_set_double(value, PyFloat_AsDouble(obj)); break; case G_TYPE_STRING: - if (obj == Py_None) + if (obj == Py_None) { g_value_set_string(value, NULL); - else if ((tmp = PyObject_Str(obj))) { - g_value_set_string(value, PYGLIB_PyUnicode_AsString(tmp)); +#if PY_VERSION_HEX < 0x03000000 + } else if (PyString_Check(obj)) { + g_value_set_string(value, PyString_AsString(obj)); +#endif + } else if (PyUnicode_Check(obj)) { + tmp = PyUnicode_AsUTF8String(obj); + g_value_set_string(value, PYGLIB_PyBytes_AsString(tmp)); Py_DECREF(tmp); } else { PyErr_Clear(); diff --git a/tests/test_properties.py b/tests/test_properties.py index 19e1136d..d156199b 100644 --- a/tests/test_properties.py +++ b/tests/test_properties.py @@ -1,3 +1,4 @@ +# coding=utf-8 import sys import struct @@ -13,6 +14,13 @@ from gobject.constants import \ G_MININT, G_MAXINT, G_MAXUINT, G_MINLONG, G_MAXLONG, \ G_MAXULONG +if sys.version_info < (3, 0): + TEST_UTF8 = "\xe2\x99\xa5" + UNICODE_UTF8 = unicode(TEST_UTF8, 'UTF-8') +else: + TEST_UTF8 = "♥" + UNICODE_UTF8 = TEST_UTF8 + from compathelper import _long class PropertyObject(GObject): @@ -71,6 +79,14 @@ class TestProperties(unittest.TestCase): self.assertEqual(obj.props.construct, "456") obj.props.construct = '789' self.assertEqual(obj.props.construct, "789") + + def testUTF8(self): + obj = new(PropertyObject, construct_only=UNICODE_UTF8) + self.assertEqual(obj.props.construct_only, TEST_UTF8) + obj.set_property('construct', UNICODE_UTF8) + self.assertEqual(obj.props.construct, TEST_UTF8) + obj.props.normal = UNICODE_UTF8 + self.assertEqual(obj.props.normal, TEST_UTF8) def testConstructOnly(self): obj = new(PropertyObject, construct_only="123") |