[PyKDE] QString to Python string conversion trouble

Andreas Gerstlauer gerstl at ics.uci.edu
Wed Oct 24 22:49:03 BST 2001


Boudewijn Rempt <boud at valdyas.org> wrote:
>
>I didn't try to convince Phil that escaped unicode was the way to go - just
>that having exceptions thrown unless you saved the setdefaultencoding function
>and used that to set the default encoding for your app was bad. 
>
Hm, I don't necessary agree (either that or I don't yet understand the 
issues completely - after all, I am not a unicode expert). 
You will only get exceptions if your default encoding is set to ASCII. 
However, if the default is that, you will get exceptions everywhere else, 
too (or at least whenever you use unicode()), not only with QStrings.
The way I see it is that if your application deals with unicode strings
you'll have to make sure that your default encoding is set reasonably
anyway, don't you?

>Returning a Unicode object instead of a string object from a QString sounds fine
>to me. I'm currently far too ill to really try hacking PyQt, though.
>
The whole problem seems to be that as long as the dedicated __unicode__
method infrastructure is not in place, the unicode() function has to
go through __str__ and string objects - and hence needs to do some
encoding of unicode into the intermediate string.

Now, I agree that it is kind of stupid for the user if unicode(QString())
doesn't transparently deliver the unicode string, independent of any
encoding. The fact that this goes through and encoding-decoding step
is an artifact of the above and not really understandable unless you know
the internals. I guess that's why they are introducing __unicode__ in
Python 2.2 ... ;-)

However, until then, the only solution I see for getting around the 
unnecessary intermediate encoding-decoding is to let __str__ return 
a unicode directly?! Otherwise, __str__ has to chose some encoding and
in that case chosing the default encoding would still make the most sense
to me (see above - with all the exception trouble in the default case).

Anyway, all the best wishes for a quick recovery - get well soon!

Andreas





More information about the PyQt mailing list