[PyKDE] strange bug in PyQt or python?

Phil Thompson phil at river-bank.demon.co.uk
Sat Jul 7 15:39:57 BST 2001


Gerard Vermeulen wrote:
> 
> Hi,
> 
> The following 30-line program (python-2.1, PyQt/sip-2.5pre1, Qt-2.2.1)
> shows a bug, that goes away if I comment out line 14 and
> uncomment line 15
> 
> --
> #!/usr/bin/env python
> 
> import sys
> from qt import *
> 
> class Item(QListViewItem):
>     def __init__(self, parent, value):
>         QListViewItem.__init__(self, parent)
>         self.value = value
> 
>     def text(self, column):
>         if column == 0:
>             print self.value[0]
>             return "%4d: %5d %8.2f %8.2f" % self.value[0] # this fails
>             #return "%4d: %5d %s %s" % self.value[0] # this works
>         elif column == 1: return "[%5d, %5d)" % self.value[1]
>         else: return ""
> 
> list = [((1, 520, 3.14, 2.72), (13, 17)), ((2, 520, 2.72, 3.14), (37, 43))]
> for item in list:
>     print "%4d: %5d %8.2f %8.2f" % item[0]
> 
> app = QApplication(sys.argv)
> listView = QListView()
> app.setMainWidget(listView)
> listView.setCaption('Pourquoi?')
> listView.addColumn('Settings')
> listView.addColumn('Zoom')
> for item in list:
>     Item(listView, item)
> listView.show()
> app.exec_loop()
> --
> It crashes with
> --
> [packer at taco python]$ python bug.py
>    1:   520     3.14     2.72
>    2:   520     2.72     3.14
> (2, 520, 2.7200000000000002, 3.1400000000000001)
> (1, 520, 3.1400000000000001, 2.7200000000000002)
> (2, 520, 2.7200000000000002, 3.1400000000000001)
> (1, 520, 3.1400000000000001, 2.7200000000000002)
> (1, 520, 3.1400000000000001, 2.7200000000000002)
> (1, 520, 3.1400000000000001, 2.7200000000000002)
> (1, 520, 3.1400000000000001, 2.7200000000000002)
> Traceback (most recent call last):
>   File "bug.py", line 14, in text
>     return "%4d: %5d %8.2f %8.2f" % self.value[0] # this fails
> AttributeError: sipThis
> Segmentation fault
> [packer at taco python]$
> --
> 
> Of course, later, I realized that the (b)right way to do this
> is to write something like:
>      def __init__(self, parent, value):
>         QListViewItem.__init__(self, parent,
>         "format0" % value[0],  "format1" % value[1])
>         self.value = value
> 
> and not to redefine:
>      def text(self, column)
> 
> but why does line 14 crash after being executed successfully
> 7 times?

Bizzare...

It also works if you use %d, ie. it only seems to fail if you use %f. I
can't see (at the moment) how it could be a PyQt problem.

Phil




More information about the PyQt mailing list