[pygtk] utf8 validating string

Yann Leboulanger asterix at lagaule.org
Sat Dec 1 06:19:06 WST 2007


John Ehresman wrote:
> Yann Leboulanger wrote:
>> Hi,
>>
>> I have a string that a textview can't display. It contains invalid chars:
>>
>>>>> t = "Let's check this out.\x00"
>>>>> import gtk
>>>>> tv = gtk.TextView()
>>>>> b = tv.get_buffer()
>>>>> b.set_text(t)
>> __main__:1: GtkWarning: gtk_text_buffer_emit_insert: assertion
>> `g_utf8_validate (text, len, NULL)' failed
>>
>> but when I di that I have no problem:
>>>>> t.decode('utf-8')
>> u"Let's check this out.\x00"
> 
> try:
>   u = unicode(t, 'utf-8')
> except Exception:
>   print 'not utf8'
> else:
>   b.set_text(t)
> 
> John
> 


>>> import gtk
>>> tv = gtk.TextView()
>>> b = tv.get_buffer()
>>> t = "Let's check this out.\x00"
>>> u = unicode(t, 'utf-8')
>>> b.set_text(t)
__main__:1: GtkWarning: gtk_text_buffer_emit_insert: assertion
`g_utf8_validate (text, len, NULL)' failed

but b.set_text(u) works ... is it the way to go?


More information about the pygtk mailing list