Convert unicode objects to utf-8 encoded G_STRINGs

This commit is contained in:
Thomas Vander Stichele 2009-06-01 19:08:47 +02:00
parent eb3701dfe5
commit 88f3323bfe
3 changed files with 127 additions and 82 deletions

View file

@ -76,15 +76,13 @@ pygst_value_as_pyobject(const GValue *value, gboolean copy_boxed)
(gstintrange_class,
Py_BuildValue ("ii",
gst_value_get_int_range_min (value),
gst_value_get_int_range_max (value)),
NULL);
gst_value_get_int_range_max (value)), NULL);
} else if (GST_VALUE_HOLDS_DOUBLE_RANGE (value)) {
ret = PyObject_Call
(gstdoublerange_class,
Py_BuildValue ("dd",
gst_value_get_double_range_min (value),
gst_value_get_double_range_max (value)),
NULL);
gst_value_get_double_range_max (value)), NULL);
} else if (GST_VALUE_HOLDS_LIST (value)) {
int i, len;
len = gst_value_list_get_size (value);
@ -108,8 +106,7 @@ pygst_value_as_pyobject(const GValue *value, gboolean copy_boxed)
(gstfraction_class,
Py_BuildValue ("ii",
gst_value_get_fraction_numerator (value),
gst_value_get_fraction_denominator (value)),
NULL);
gst_value_get_fraction_denominator (value)), NULL);
} else if (GST_VALUE_HOLDS_FRACTION_RANGE (value)) {
const GValue *min, *max;
min = gst_value_get_fraction_range_min (value);
@ -118,16 +115,27 @@ pygst_value_as_pyobject(const GValue *value, gboolean copy_boxed)
(gstfractionrange_class,
Py_BuildValue ("OO",
pygst_value_as_pyobject (min, copy_boxed),
pygst_value_as_pyobject (max, copy_boxed)),
NULL);
pygst_value_as_pyobject (max, copy_boxed)), NULL);
} else if (GST_VALUE_HOLDS_BUFFER (value)) {
return pygstminiobject_new (gst_value_get_mini_object (value));
} else {
gchar buf[256];
g_snprintf (buf, 256, "unknown type: %s", g_type_name (G_VALUE_TYPE (value)));
g_snprintf (buf, 256, "unknown type: %s",
g_type_name (G_VALUE_TYPE (value)));
PyErr_SetString (PyExc_TypeError, buf);
}
}
if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
PyObject *u = NULL;
/* FIXME: docs are not clear on whether this sets a python
exception when it fails */
u = PyUnicode_FromEncodedObject (ret, "utf-8", NULL);
Py_DECREF (ret);
ret = u;
}
return ret;
}
@ -172,6 +180,10 @@ pygst_value_init_for_pyobject (GValue *value, PyObject *obj)
} else if (PyList_Check (obj)) {
PyErr_Clear ();
t = GST_TYPE_LIST;
} else if (PyUnicode_Check (obj)) {
/* unicode strings should be converted to UTF-8 */
PyErr_Clear ();
t = G_TYPE_STRING;
} else {
/* pyg_type_from_object already set the error */
return FALSE;
@ -198,6 +210,15 @@ pygst_value_from_pyobject (GValue *value, PyObject *obj)
{
GType f = g_type_fundamental (G_VALUE_TYPE (value));
/* Unicode objects should be converted to utf-8 strings */
if (PyObject_TypeCheck (obj, &PyUnicode_Type)) {
PyObject *v;
v = PyUnicode_AsUTF8String (obj);
Py_DECREF (obj);
obj = v;
}
/* work around a bug in pygtk whereby pyg_value_from_pyobject claims success
for unknown fundamental types without actually doing anything */
if (f < G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST)
@ -357,11 +378,13 @@ pygst_value_init(void)
NULL_CHECK (gstfourcc_class);
gstintrange_class = (PyObject *) PyDict_GetItemString (dict, "IntRange");
NULL_CHECK (gstintrange_class);
gstdoublerange_class = (PyObject*)PyDict_GetItemString (dict, "DoubleRange");
gstdoublerange_class =
(PyObject *) PyDict_GetItemString (dict, "DoubleRange");
NULL_CHECK (gstdoublerange_class);
gstfraction_class = (PyObject *) PyDict_GetItemString (dict, "Fraction");
NULL_CHECK (gstfraction_class);
gstfractionrange_class = (PyObject*)PyDict_GetItemString (dict, "FractionRange");
gstfractionrange_class =
(PyObject *) PyDict_GetItemString (dict, "FractionRange");
NULL_CHECK (gstfractionrange_class);
return TRUE;

View file

@ -44,11 +44,11 @@ class StructureTest(TestCase):
def testString(self):
assert self.struct.has_key('foo')
assert isinstance(self.struct['foo'], str)
assert isinstance(self.struct['foo'], unicode)
assert self.struct['foo'] == 'bar', self.struct['foo']
self.struct['foo'] = 'baz'
assert self.struct.has_key('foo')
assert isinstance(self.struct['foo'], str)
assert isinstance(self.struct['foo'], unicode)
assert self.struct['foo'] == 'baz', self.struct['foo']
def testBoolean(self):

View file

@ -43,3 +43,25 @@ class TestTagList(TestCase):
keys = taglist.keys()
keys.sort()
self.assertEqual(keys, ['key1', 'key2'])
def testUnicode(self):
taglist = gst.TagList()
# normal ASCII text
taglist[gst.TAG_ARTIST] = 'Artist'
self.failUnless(isinstance(taglist[gst.TAG_ARTIST], unicode))
self.assertEquals(taglist[gst.TAG_ARTIST], u'Artist')
self.assertEquals(taglist[gst.TAG_ARTIST], 'Artist')
# normal ASCII text as unicode
taglist[gst.TAG_ARTIST] = u'Artist'
self.failUnless(isinstance(taglist[gst.TAG_ARTIST], unicode))
self.assertEquals(taglist[gst.TAG_ARTIST], u'Artist')
self.assertEquals(taglist[gst.TAG_ARTIST], 'Artist')
# real unicode
taglist[gst.TAG_ARTIST] = u'S\xc3\xadgur R\xc3\xb3s'
self.failUnless(isinstance(taglist[gst.TAG_ARTIST], unicode))
self.assertEquals(taglist[gst.TAG_ARTIST], u'S\xc3\xadgur R\xc3\xb3s')