How to get rid of FutureWarning: hex/oct constants...

Bengt Richter bokr at oz.net
Thu Mar 24 23:57:48 EST 2005


On Fri, 25 Mar 2005 03:35:29 GMT, bokr at oz.net (Bengt Richter) wrote:

>On Thu, 24 Mar 2005 23:21:39 -0000, Grant Edwards <grante at visi.com> wrote:
>
>>How do I get rid of the following warning?
>>
>>  <path>.py:233: FutureWarning: hex/oct constants > sys.maxint will return positive values in Python 2.4 and up
>>  fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pack("HBB",i,0,0))
>>
>>I tried using 0xc0047a80L.  That got rid of the warning, but
>>then I got an exception when fcntl.ioctl() was called because
>>the long int was too large to be converted to an int.
>>  
>Lobby for a PEP for numeric literals allowing representation
>of negative numbers without writing a unary minus expression.
>E.g.,
>    16xfc0047a80
>would be explicitly negative and would not overflow 32-bit representation.
>The corresponding positive value
>    16x0c0047a80
>would overflow, of course, which would be proper.
>
>Some discussion, including analogous spellings for other bases:
>
>    http://groups-beta.google.com/group/comp.lang.python/msg/c23131df1e919435
>
>In the meantime, maybe (ugh):
>
> Python 2.3.2 (#49, Oct  2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> -2*0x40000000+0x40047a80
> -1073448320
> >>> hex(-2*0x40000000+0x40047a80)
> __main__:1: FutureWarning: hex()/oct() of negative int will return a signed string in Python 2.4
>  and up
> '0xc0047a80'
>
>That "signed string" is a unary minus expression using an absolute value forced by the inadequacy
>of the literal representation syntax.
>IOW, IMO '-' + hex_literal_of(abs(x)) is not a decent hex_literal_of(-x) !!
>Urk and argh...
>
I guess you could make a convenience function to convert long literals to signed i32:

 >>> def i32(x): return (x&0x80000000L and -2*0x40000000 or 0) + int(x&0x7fffffff)
 ...
 >>> i32(0xc0047a80L)
 -1073448320

so you can use i32(0xc0047a80L) where old python accepted 0xc0047a80

Regards,
Bengt Richter



More information about the Python-list mailing list