mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-11-26 11:41:09 +00:00
Convert unicode objects to utf-8 encoded G_STRINGs
This commit is contained in:
parent
eb3701dfe5
commit
88f3323bfe
3 changed files with 127 additions and 82 deletions
|
@ -76,15 +76,13 @@ pygst_value_as_pyobject(const GValue *value, gboolean copy_boxed)
|
||||||
(gstintrange_class,
|
(gstintrange_class,
|
||||||
Py_BuildValue ("ii",
|
Py_BuildValue ("ii",
|
||||||
gst_value_get_int_range_min (value),
|
gst_value_get_int_range_min (value),
|
||||||
gst_value_get_int_range_max (value)),
|
gst_value_get_int_range_max (value)), NULL);
|
||||||
NULL);
|
|
||||||
} else if (GST_VALUE_HOLDS_DOUBLE_RANGE (value)) {
|
} else if (GST_VALUE_HOLDS_DOUBLE_RANGE (value)) {
|
||||||
ret = PyObject_Call
|
ret = PyObject_Call
|
||||||
(gstdoublerange_class,
|
(gstdoublerange_class,
|
||||||
Py_BuildValue ("dd",
|
Py_BuildValue ("dd",
|
||||||
gst_value_get_double_range_min (value),
|
gst_value_get_double_range_min (value),
|
||||||
gst_value_get_double_range_max (value)),
|
gst_value_get_double_range_max (value)), NULL);
|
||||||
NULL);
|
|
||||||
} else if (GST_VALUE_HOLDS_LIST (value)) {
|
} else if (GST_VALUE_HOLDS_LIST (value)) {
|
||||||
int i, len;
|
int i, len;
|
||||||
len = gst_value_list_get_size (value);
|
len = gst_value_list_get_size (value);
|
||||||
|
@ -108,8 +106,7 @@ pygst_value_as_pyobject(const GValue *value, gboolean copy_boxed)
|
||||||
(gstfraction_class,
|
(gstfraction_class,
|
||||||
Py_BuildValue ("ii",
|
Py_BuildValue ("ii",
|
||||||
gst_value_get_fraction_numerator (value),
|
gst_value_get_fraction_numerator (value),
|
||||||
gst_value_get_fraction_denominator (value)),
|
gst_value_get_fraction_denominator (value)), NULL);
|
||||||
NULL);
|
|
||||||
} else if (GST_VALUE_HOLDS_FRACTION_RANGE (value)) {
|
} else if (GST_VALUE_HOLDS_FRACTION_RANGE (value)) {
|
||||||
const GValue *min, *max;
|
const GValue *min, *max;
|
||||||
min = gst_value_get_fraction_range_min (value);
|
min = gst_value_get_fraction_range_min (value);
|
||||||
|
@ -118,16 +115,27 @@ pygst_value_as_pyobject(const GValue *value, gboolean copy_boxed)
|
||||||
(gstfractionrange_class,
|
(gstfractionrange_class,
|
||||||
Py_BuildValue ("OO",
|
Py_BuildValue ("OO",
|
||||||
pygst_value_as_pyobject (min, copy_boxed),
|
pygst_value_as_pyobject (min, copy_boxed),
|
||||||
pygst_value_as_pyobject (max, copy_boxed)),
|
pygst_value_as_pyobject (max, copy_boxed)), NULL);
|
||||||
NULL);
|
|
||||||
} else if (GST_VALUE_HOLDS_BUFFER (value)) {
|
} else if (GST_VALUE_HOLDS_BUFFER (value)) {
|
||||||
return pygstminiobject_new (gst_value_get_mini_object (value));
|
return pygstminiobject_new (gst_value_get_mini_object (value));
|
||||||
} else {
|
} else {
|
||||||
gchar buf[256];
|
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);
|
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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,6 +180,10 @@ pygst_value_init_for_pyobject (GValue *value, PyObject *obj)
|
||||||
} else if (PyList_Check (obj)) {
|
} else if (PyList_Check (obj)) {
|
||||||
PyErr_Clear ();
|
PyErr_Clear ();
|
||||||
t = GST_TYPE_LIST;
|
t = GST_TYPE_LIST;
|
||||||
|
} else if (PyUnicode_Check (obj)) {
|
||||||
|
/* unicode strings should be converted to UTF-8 */
|
||||||
|
PyErr_Clear ();
|
||||||
|
t = G_TYPE_STRING;
|
||||||
} else {
|
} else {
|
||||||
/* pyg_type_from_object already set the error */
|
/* pyg_type_from_object already set the error */
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
@ -198,6 +210,15 @@ pygst_value_from_pyobject (GValue *value, PyObject *obj)
|
||||||
{
|
{
|
||||||
GType f = g_type_fundamental (G_VALUE_TYPE (value));
|
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
|
/* work around a bug in pygtk whereby pyg_value_from_pyobject claims success
|
||||||
for unknown fundamental types without actually doing anything */
|
for unknown fundamental types without actually doing anything */
|
||||||
if (f < G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST)
|
if (f < G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST)
|
||||||
|
@ -357,11 +378,13 @@ pygst_value_init(void)
|
||||||
NULL_CHECK (gstfourcc_class);
|
NULL_CHECK (gstfourcc_class);
|
||||||
gstintrange_class = (PyObject *) PyDict_GetItemString (dict, "IntRange");
|
gstintrange_class = (PyObject *) PyDict_GetItemString (dict, "IntRange");
|
||||||
NULL_CHECK (gstintrange_class);
|
NULL_CHECK (gstintrange_class);
|
||||||
gstdoublerange_class = (PyObject*)PyDict_GetItemString (dict, "DoubleRange");
|
gstdoublerange_class =
|
||||||
|
(PyObject *) PyDict_GetItemString (dict, "DoubleRange");
|
||||||
NULL_CHECK (gstdoublerange_class);
|
NULL_CHECK (gstdoublerange_class);
|
||||||
gstfraction_class = (PyObject *) PyDict_GetItemString (dict, "Fraction");
|
gstfraction_class = (PyObject *) PyDict_GetItemString (dict, "Fraction");
|
||||||
NULL_CHECK (gstfraction_class);
|
NULL_CHECK (gstfraction_class);
|
||||||
gstfractionrange_class = (PyObject*)PyDict_GetItemString (dict, "FractionRange");
|
gstfractionrange_class =
|
||||||
|
(PyObject *) PyDict_GetItemString (dict, "FractionRange");
|
||||||
NULL_CHECK (gstfractionrange_class);
|
NULL_CHECK (gstfractionrange_class);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
|
||||||
|
|
|
@ -44,11 +44,11 @@ class StructureTest(TestCase):
|
||||||
|
|
||||||
def testString(self):
|
def testString(self):
|
||||||
assert self.struct.has_key('foo')
|
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']
|
assert self.struct['foo'] == 'bar', self.struct['foo']
|
||||||
self.struct['foo'] = 'baz'
|
self.struct['foo'] = 'baz'
|
||||||
assert self.struct.has_key('foo')
|
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']
|
assert self.struct['foo'] == 'baz', self.struct['foo']
|
||||||
|
|
||||||
def testBoolean(self):
|
def testBoolean(self):
|
||||||
|
|
|
@ -43,3 +43,25 @@ class TestTagList(TestCase):
|
||||||
keys = taglist.keys()
|
keys = taglist.keys()
|
||||||
keys.sort()
|
keys.sort()
|
||||||
self.assertEqual(keys, ['key1', 'key2'])
|
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')
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue