[PyQt] Re: Bug in QString comparison

Phil Thompson phil at riverbankcomputing.com
Tue Sep 16 21:54:09 BST 2008


On Wed, 10 Sep 2008 01:51:54 +0200, Giovanni Bajo <rasky at develer.com>
wrote:
> Hello Phil,
> 
> this looks weird to me (using sip 4.7.6, PyQt 4.4.2, Qt 4.4.0):
> 
> =======================================
> $ python
> Python 2.5.2 (r252:60911, May  7 2008, 15:21:12) 
> [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from PyQt4.Qt import *
>>>> "a" == QString("a")
> True
>>>> u"a" == QString(u"a")
> True
>>>> a = u"\u30b5"
>>>> b = QString(a)
>>>> a
> u'\u30b5'
>>>> b
> PyQt4.QtCore.QString(u'\u30b5')
>>>> a == b
> False
> ========================================
> 
> Why does the equality operator returns False?

a is converting b to a string because QString supports the buffer protocol.
That conversion is done using the default codec which is normally ascii. If
QString didn't support the buffer protocol then I think Python would then
go on to try b == a, which would work.

b == a works as expected because b is converting a to a QString first.

> Moreover: I can't appear to be able to print the QString:
> 
> ========================================
>>>> print a
>>>>> print b
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u30b5' in
> position 0: ordinal not in range(128)
> ========================================

It's the same cause - the default codec is ascii.

Phil



More information about the PyQt mailing list