[PyQt] PyQt and integer overflows

Phil Thompson phil at riverbankcomputing.com
Wed May 14 15:42:18 BST 2014

On 14/05/2014 2:50 pm, Florian Bruhin wrote:
> Hi,
> I think the exception PyQt prints when passing an integer value which
> is too big for the underlying C function is rather confusing:
>     >>> from PyQt5.QtCore import QSize
>     >>> QSize(2**64, 0)
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in <module>
>     TypeError: arguments did not match any overloaded call:
>       [...]
>       QSize(int, int): argument 1 has unexpected type 'int'
> I think if possible it should detect the *type* is in fact correct,
> and raise an OverflowError or so instead.

I've changed it (in tonight's SIP snapshot) to accept the value without 
raising an exception. The value will be silently truncated, which make 
the behaviour consistent in all cases (see below).

> Also, how can I detect what the largest value is I can safely pass in?
> It seems to be different on my Windows and Linux system (both 64 bit
> though). On Windows, 2**32 / 2 - 1 is the largest value, on Linux
> 2**64 instead. I tried using struct to get the size of an int, but
> that's the same on both platforms:
>     >>> struct.calcsize('@i') * 8
>     >>> 32

An Overflow exception is raised when the value will not fit into a C 
long (not an int) which is 64 bits on Linux and 32 bits on Windows. On 
Linux if the value fits into 64 bits but doesn't fit into 32 bits then 
you will not get an Overflow exception but the value will still be 
truncated. Tonight's fix removes that inconsistency.


More information about the PyQt mailing list