[reportlab-users] Re: FutureWarnings in ttfonts.py with Python 2.3

Robin Becker reportlab-users@reportlab.com
Mon, 05 Apr 2004 13:34:57 +0100


Marius Gedminas wrote:

> 
> I spent a few minutes experimenting with various variants of add32 a
> while ago.  I was quite suprised to find out that using Python's longs
> with bit shifts/addition modulo 2**32 did not give the desired results
> when negative numbers came into the picture.  The best variant I could
> come up with looked like this:
> 
>     def add32(x, y):
>         z = x + y
>         if z > 2147483647:
>             return int(z - 4294967296L)
>         elif z < -2147483648:
>             return int(z + 4294967296L)
>         else:
>             return z
> 
> It was a tad slower than the current version with those future warnings
> and incompatible with older versions of Python that did not promote ints
> to longs on overflow.  I gave up at that point.
> 
> 

I'm a bit puzzled as I thought we're supposed to be adding unsigned 32 bit numbers.

So can we not use

def add32(x,y): return _L2U32((x+y) & 0xffffffffL)

I'm fairly sure the more complex stuff doesn't work with older pythons
any how.

I get this in Python-2.1 with the above
 >>> add32(0xfffffffe,1)
-1
 >>> add32(0xfffffffe,2)
0
 >>> add32(0xffffffff,2)
1
 >>> add32(-1,1)
0
 >>>

I get the same in Python-2.3, but have to use long constants to avoid the warnings.
-- 
Robin Becker