user
user

Reputation: 61

Getting Error with GTK

While trying this gtk code:

import gtk
tv = gtk.TextView()
b = tv.get_buffer()
t = "Let's check this out.\x00"
b.set_text(t)

I am getting this error:

GtkWarning: gtk_text_buffer_emit_insert: assertion `g_utf8_validate (text, len, NULL)' failed

The text is generated automatically here i have shown for example. I wanted to put that string into the text buffer but i can't able to. Help me out with this.

Upvotes: 4

Views: 975

Answers (3)

andlabs
andlabs

Reputation: 11578

I am simply going to assume that what I asked in a comment above is what you are trying to do.

While it is true that C strings end with a \x00 byte, when you use strings with other programming languages, you don't have to do this. The library that binds your C library to your programming language of choice does that for you. In this case, pygtk does the C string muckery for you. So you don't have to have an explicit \x00 at the end of your strings there. Get rid of it and the code should work again.

Upvotes: 1

ntd
ntd

Reputation: 7434

The official g_utf8_validate documentation says that function returns false for any NUL byte. This contrast with his name because NUL bytes are valid UTF-8 codepoints.

In my opinion this is a plain bug: what's the purpose of all those gssize max_len arguments if the string should be nul-terminated anyway? In any case this is the actual situation and it will not change in the near future. You should deal with it, e.g. strip every NUL byte from your strings or convert it to another codepoint.

Upvotes: 1

unwind
unwind

Reputation: 399703

I believe this is due to the strange embedded 0-byte at the end of the string t (the \x00 part).

I don't think you can expect/require GTK+ to support binary data in the GtkTextBuffer, it simply requires text to be in UTF-8. Remove the spurious 0-byte.

I think there's a conflict where Python reports the length of the string as n, but the the C code in GTK+ that tries to verify the encoding hits that embedded '\0', and treats that as end-of-string.

Notice that fast_validate_len() (part of g_utf8_validate()'s implementation) will treat its input as 0-terminated, even though it also has an explicit length.

Upvotes: 1

Related Questions