[PyKDE] QString to Python string conversion trouble
Andreas Gerstlauer
gerstl at ics.uci.edu
Wed Oct 24 09:27:50 BST 2001
I wrote:
> On the other hand, in lieu of 2.2. where __str__ should probably only
> return a string, and if you really want str() not to raise an exception,
> how about the following: __str__ returns a string object in which only
> chars > 128 are escaped (other <= 128 are passed without escaping)?
However, I've noticed another problem with that approach: in the current
version, a plain unicode(QString(u"...")) (using the default decoding)
will not return back the correct, original unicode string. Instead, I
also get the escape encoded string, like in the str() case that started
this thread (since the QString.__str__() method choses to use that
encoding):
>>> s = u"Test\u0400Test"
>>> s
u'Test\u0400Test'
>>> unicode(qt.QString(s))
u'Test\\u0400Test'
In order to get the original unicode string back one has to explicitly
specify the esacpe decoding:
>>> unicode(qt.QString(s), 'Unicode Escape')
u'Test\u0400Test'
Just wanted to mention that. I don't know if that is intended/acceptable.
In order to get around that in the approach where __str__ returns a string
object one would have to use the default encoding in __str__ but that brings
us back to where all started from (before pre4.0).
On the other hand, if __str__ returns a unicode object that problem goes
away (see previous mail).
In general, out of curiosity (since you mentioned that two other people
convinced you to do it like that), what was the rationale for choosing the
escape encoding (as opposed to say an UTF-8 encoding)?
Andreas
More information about the PyQt
mailing list