Changeset 5048


Ignore:
Timestamp:
Feb 19, 2008, 10:30:48 AM (16 years ago)
Author:
ole
Message:

Refactored 64 bit fix into new function: safe_crc

Location:
anuga_core/source/anuga/utilities
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/utilities/system_tools.py

    r5046 r5048  
    170170        if verbose is True:
    171171            print 'Version info stored to %s' %filename
    172            
     172
     173def safe_crc(string):
     174    """64 bit safe crc computation.
     175
     176       See Guido's 64 bit fix at http://bugs.python.org/issue1202           
     177    """
     178
     179    from zlib import crc32
     180    import os
     181
     182    x = crc32(string)
     183       
     184    if os.name == 'posix' and os.uname()[4] == 'x86_64':
     185        crcval = x - ((x & 0x80000000) << 1)
     186    else:
     187        crcval = x
     188       
     189    return crcval
     190
    173191
    174192def compute_checksum(filename, max_length=2**20):
     
    180198    """
    181199
    182     from zlib import crc32
    183    
    184200    fid = open(filename, 'rb') # Use binary for portability
    185     x = crc32(fid.read(max_length))
    186 
     201    crcval = safe_crc(fid.read(max_length))
    187202    fid.close()
    188203
    189     if os.name == 'posix' and os.uname()[4] == 'x86_64':
    190         #Guido's 64 bit fix (http://bugs.python.org/issue1202)       
    191         crcval = x - ((x & 0x80000000) << 1)
    192     else:
    193         crcval = x
    194204    return crcval
  • anuga_core/source/anuga/utilities/test_system_tools.py

    r5044 r5048  
    3939        # Generate a text file
    4040        tmp_fd , tmp_name = mkstemp(suffix='.tmp', dir='.')
    41         fid = os.fdopen(tmp_fd, 'w')
     41        fid = os.fdopen(tmp_fd, 'w+b')
    4242        string = 'My temp file with textual content. AAAABBBBCCCC1234'
    4343        fid.write(string)
    4444        fid.close()
    4545
    46         ref_crc = zlib.crc32(string)
     46        # Have to apply the 64 bit fix here since we aren't comparing two
     47        # files, but rather a string and a file.
     48        ref_crc = safe_crc(string)
     49
    4750        checksum = compute_checksum(tmp_name)
    4851        assert checksum == ref_crc
     
    6063        fid.close()
    6164
    62 
    63         ref_crc = zlib.crc32(string)
     65        ref_crc = safe_crc(string)
    6466        checksum = compute_checksum(tmp_name)
    6567
Note: See TracChangeset for help on using the changeset viewer.