Python update trouble (2.3 to 2.4): x<<y

Gonzalo Monzón gmc at serveisw3.net
Sun May 21 01:08:28 EDT 2006


Hi all!

I have been translating some Python custom C extension code into Python, 
as I need these modules to be portable and run on a PocketPC without the 
need of compile (for the purpose its a must 2.4 as it is the last 
PythonCE release with great improvements).

But I've been stuck with a script wich does not work as expected once 
translated to python, 2.4

In the meantime, I thought I could test it with an old 2.3 version I 
have installed too on my computer, and found it run as expected, but see 
the FutureWarning, so googled a bit and found PEP 237 and long integer 
integration issue, but then can't find any workaround to fix the code 
for Python 2.4

Hope somebody could point some suggestion, or perhaps, the solution is 
pretty simple, but I missed it.

As I said, the code works fine on 2.3. I attach the code below.

The trouble is on the CalcCRC16 function, as you can see on the 
FutureWarning message.

InitCRC16 function does some bitwise xor's too, but I checked it and 
works as expected. Thought because only happen to be with small values 
there.

Thanks in advance for any help,
Gonzalo

##############################
Python 2.3.2:

pytest1.py:90: FutureWarning: x<<y losing bits or changing sign will 
return a long in Python 2.4 and up
  crc = gCRC16Table[((crc >> 8) & 255)] ^ (crc << 8) ^ ord(str[x])
67560050

##############################
Python 2.4.2:

22002496167782427386022437441624938050682666541682


*Expected result is 67560050*


# #############################
# pytest1.py

gCRC16Table = []

def InitCRC16():
    global gCRC16Table

    for i in xrange(0,256):
        crc = i << 8
        for j in xrange(0,8):
            if (crc & 0x8000) != 0:
                tmp = 0x1021
            else:
                tmp = 0
              
            crc = (crc << 1) ^ tmp
        gCRC16Table.append(crc)
   
def CalcCRC16(str):
    global gCRC16Table

    crc = 0xFFFF  
    for x in xrange(0,len(str)):
        crc = gCRC16Table[((crc >> 8) & 255)] ^ (crc << 8) ^ ord(str[x])
       
    return crc
  
test = "123456asdfg12345123"
InitCRC16()
print CalcCRC16(test)




More information about the Python-list mailing list