PySide Bugzilla Closed for New Bugs

PySide is now a Qt Add-on and uses the Qt Project's JIRA Bug Tracker instead of this Bugzilla instance. This Bugzilla is left for reference purposes.

Bug 649 - Too-picky typechecking on numeric types: use __float__ / __int__.
: Too-picky typechecking on numeric types: use __float__ / __int__.
Status: CLOSED FIXED
Product: PySide
Classification: Unclassified
Component: Shiboken
: HEAD
: All All
: P3 normal
Assigned To: Marcelo Lira
:
:
:
  Show dependency treegraph
 
Reported: 2011-01-27 16:26 EET by Kenneth Arnold
Modified: 2011-02-17 19:18 EET (History)
8 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kenneth Arnold 2011-01-27 16:26:15 EET
Constructors for QPointF and QColor will not accept NumPy's C-like numeric
types (float32, int32, etc.):

QtCore.QPointF(np.float32(.5), np.float32(.5)) yields
TypeError: 'PySide.QtCore.QPointF' called with wrong argument types:
PySide.QtCore.QPointF(numpy.float32, numpy.float32)
Supported signatures:
PySide.QtCore.QPointF()
PySide.QtCore.QPointF(PySide.QtCore.QPoint)
PySide.QtCore.QPointF(PySide.QtCore.QPointF)
PySide.QtCore.QPointF(float, float)

and similarly for QColor:
TypeError: 'PySide.QtGui.QColor' called with wrong argument types:
PySide.QtGui.QColor(numpy.int32, numpy.int32, numpy.int32)
Supported signatures:
PySide.QtGui.QColor()
PySide.QtGui.QColor(QVariant)
PySide.QtGui.QColor(PySide.QtCore.Qt.GlobalColor)
PySide.QtGui.QColor(PySide.QtGui.QColor)
PySide.QtGui.QColor(QString)
PySide.QtGui.QColor(int, int, int, int = 255)
PySide.QtGui.QColor(unsigned int)

But they do support the standard __float__ and __int__ conversion methods. So
Shiboken wrappers should try to call those methods on numeric parameters.
(sip/PyQt4 does.)
Comment 1 Hugo Parente Lima 2011-01-27 16:57:05 EET
bug 535 is identical to this ans was marked as WONTFIX, but is was due to our
lack of information about __float__ and __int__ methods.

Thanks for doing a better bug report, it's valid. :-)
Comment 2 Kenneth Arnold 2011-01-27 18:03:21 EET
Did a bit of digging. One culprit is libshiboken/conversions.h line 41:
#define SbkNumber_Check(X) (PyInt_Check(X) || PyFloat_Check(X) ||
PyLong_Check(X))

It's not clear how to extend that to Numpy value types. See
https://github.com/numpy/numpy/blob/master/numpy/core/include/numpy/ndarrayobject.h
for a macro approach that would be awkward for Shiboken to take.

I'm unclear how PyQt4 accomplishes it (calls `builtin.float` and catches
exceptions perhaps?), but:

>>> from PyQt4 import QtCore
>>> import numpy as np
>>> QtCore.QPointF(np.float32(.5), np.float32(.5))
PyQt4.QtCore.QPointF(0.5, 0.5)
Comment 3 Matti Airas 2011-01-28 09:52:22 EET
Thanks for the bug report. Since Hugo already verified this is a valid issue,
I'm prioritizing this P3. :-)
Comment 4 Kenneth Arnold 2011-01-28 12:04:05 EET
Another note: the native Python int and float types are inherited by the
corresponding Numpy types, so test both int32 and int64 on 64-bit systems for
example:

>>> isinstance(np.float32(.5), float)
False
>>> isinstance(np.float64(.5), float)
True

Yet:
>>> from PyQt4 import QtCore
>>> import numpy as np
>>> QtCore.QPointF(np.float32(.5), np.float64(.5))
PyQt4.QtCore.QPointF(0.5, 0.5)

and even:
>>> QtCore.QPointF(np.uint8(6), np.int64(2))
PyQt4.QtCore.QPointF(6.0, 2.0)
Comment 5 Marcelo Lira 2011-02-16 13:25:21 EET
The problem is that for some historical reason we were using a custom macro
SbkNumber_Check, that checked if the object was of PyInt, PyLong or PyFloat
type, and Python's own PyNumber_Check looks for general number characteristics
(via tp_as_number PyObjectType attribute).
In shorter words, the object doesn't have to be of a numeric type, but to
follow the number protocol.
Comment 6 Marcelo Lira 2011-02-17 13:16:30 EET
Fixed in commit Shiboken/30d71761.
Comment 7 Hugo Parente Lima 2011-02-17 19:18:17 EET
Released in 1.0.0~rc1