[PyQt] Bug in QAbstractItemModel::createIndex()?

Phil Thompson phil at riverbankcomputing.co.uk
Thu Apr 5 17:19:56 BST 2007


On Thursday 05 April 2007 3:02 pm, Andreas Pakulat wrote:
> On 05.04.07 14:21:44, Phil Thompson wrote:
> > On Thursday 05 April 2007 1:12 pm, Andreas Pakulat wrote:
> > > On 05.04.07 12:14:01, Phil Thompson wrote:
> > > > The spelling of itemdict on line 120 - the line that generates all
> > > > those exception messages. Maybe you didn't attach the version you
> > > > intended to.
> > >
> > > Uhm, right, overlooked that one.
> > > But the problem still persists, what I see here is:
> > > KeyError: -1212033192L
> > > Traceback (most recent call last):
> > >   File "simpletreemodel.py", line 88, in data
> > >     item = self.itemdict[index.internalId()]
> > > KeyError: -1212033192L
> > > Traceback (most recent call last):
> > >   File "simpletreemodel.py", line 88, in data
> > >     item = self.itemdict[index.internalId()]
> > > KeyError: -1212033096L
> > >
> > > And that is with the latest snapshot. I have absolutely no idea how
> > > that can happen and so far it seems I'm the only one seeing this.
> >
> > Jeremy Sanders posted about a similar problem last August. Attached is
> > his test script. He was getting the same values on a 32 bit system but
> > different values on a 64 bit system.
>
> I'm not on 64Bit system, this is a plain 32Bit Linux running Python 2.5
> using shared python lib.
>
> > You getting different values would explain the behaviour you are seeing.
> > However I get the same values on both 32 and 64 bit systems - but none of
> > my values go negative.
>
> Hmm, you have any idea where this might come from? I'm prepared to dig
> into this as its keeping me from trying some things with eric4's VCS...
>
> > Certainly internalPointer() should be used in preference to internalId().
>
> Well, the problem with internalPointer is that you can't easily check
> wether the object it carries is valid. With internalId you get an
> KeyError when trying to index into the dict...

An internal pointer is always valid if you still have a reference to the 
actual data.

The internal ID would be useful if the value returned by internalId() was the 
same size (ie. an int) as that passed to createIndex().

> Uhm, I just tested the following code, maybe this helps in finding the
> cause. The output here is:
> -1211419440 3083330668 3083330668

The first number makes no sense. I would expect it to be -1211636628.

> ,----
>
> | from PyQt4 import QtCore
> | class c(QtCore.QAbstractItemModel):
> |    pass
> | a=c()
> | i=id(a)
> | x=a.createIndex(0,0,i)
> | print x.internalId(),x.internalPointer(),i
>
> `----
>
> Correct me if I'm wrong, but could it be that due to the fact that id()
> returns a python object the 2nd overload for createIndex is used? I
> tried to use this in the simpletreemodel.py but it still throws key
> errors with negative values. If I try to give createIndex() an int-object
> that the model keeps a reference to (i.e.
> self.itemdict.keys().index(id(item))) then I get a segfault. I'll
> rebuild with debug symbols now...

You could put printf()s in the SIP generated code to make absolutely sure. 
Look for the function meth_QAbstractItemModel_createIndex(). The only way it 
could be happening is if the convertion to an int raised an exception.

Phil


More information about the PyQt mailing list