[PySide] Bug with new hash table feature

John Ehresman jpe at wingware.com
Thu Jun 14 01:13:42 CEST 2012


On 6/13/12 6:32 PM, Nathan Smith wrote:
> My most recent approach was to return the address of the shiboken
> object, which is address of the PyObject.  I tried this approach (I
> haven't submitted it yet) on the example you provided below, and it
> seems to work.  I've been using it with my code for a few days now, and
> it's been working well.

In Python 2, leaving the tp_hash slot set to NULL will result in the 
PyObject* address being used for hash and equality if the compare slots 
are also NULL which is what I think we want.  This may be different in 
Python 3.  The bug report that apparently prompted this change -- 
https://bugreports.qt-project.org/browse/PYSIDE-42 -- doesn't specify 
the Python version.  The class in the bug report is QListWidgetItem, 
which isn't derived from QObject so there's no destroyed signal emitted 
when it's deleted.

> Note that using the address method has the drawback that two objects
> separated in time may have the same hash value because they may land at
> the same memory address.  You can see this in the following test case:
>
>      >>> hash(QtCore.QObject()) == hash(QtCore.QObject())
>     True

That's fine -- equivalent hash values just mean that the 2 might be 
equal, but you can't compare the two with == since they don't exist at 
the same time.

> Incidentally, what is the difference between shiboken.delete and
> shiboken.invalidate?  There don't appear to be any docstrings in the
> shiboken module and invalidate isn't in the online documentation.

The delete() function invokes C++ delete after invalidating the wrapper. 
  The invalidate() function invalidates the wrapper but leaves the C++ 
object alone.  The reason you may want invalidate is when working with 
non-QObject derived objects like QEvent, QStandardItem, and others where 
there's no destroyed signal that's emitted when the object is destroyed 
(or any other notification mechanism).  With non-QObject objects, the 
wrapper may outlive the C++ object it refers to and the invalidate 
function can be used to invalidate the wrapper before the C++ object is 
destroyed.

John



More information about the PySide mailing list