1

次の問題があります。コードは次のようになります。

import  binascii, struct
def crc32up(data):
    # little endian!!
    bin = struct.pack ('<I', binascii.crc32 (data))
    return string.upper (binascii.hexlify (bin))

# Generate crc of time code.
#
timecrc_code = crc32up(time_code)

警告は次のとおりです。

 DeprecationWarning: struct integer overflow masking is deprecated
 timecrc_code = crc32up(time_code)

このエラーの原因は何ですか?

4

1 に答える 1

6

割り当てた4バイトにパックしようとしている値が大きすぎます。

>>> import struct
>>> n = 2 ** 32
>>> n
4294967296L
>>> struct.pack('<I', n - 1)
'\xff\xff\xff\xff'
>>> struct.pack('<I', n)
__main__:1: DeprecationWarning: struct integer overflow masking is deprecated
'\x00\x00\x00\x00'

新しいPythonバージョン(> = 2.6)でも、受け入れられる値について警告が表示されます。

>>> import struct
>>> struct.pack('<I', -1)
__main__:1: DeprecationWarning: struct integer overflow masking is deprecated
__main__:1: DeprecationWarning: 'I' format requires 0 <= number <= 4294967295
'\xff\xff\xff\xff'

Pythonが言っているのは、4バイトに収まるように値をマスクする必要があるということです。でこれを自分で行うことができますvalue & 0xFFFFFFFF

警告は、Pythonプログラムの実行中に1回だけ発行されます。

2.6以降、binascii.crc32値は常に符号付き4バイト値であり、これらをパックするには常にマスクを使用する必要があることに注意してください。これは2.6より前では常に一貫しているわけではなく、プラットフォームによって異なります。詳細については、ドキュメントを参照してください。

于 2012-09-09T14:23:30.690 に答える