The version of the `struct' module included with Python 2.5 writes a
warning to `stderr' and truncates rather than raising an exception. Do
the work ourselves.
class _HashBase (object):
## The standard hash methods. Assume that `hash' is defined and returns
## the receiver.
class _HashBase (object):
## The standard hash methods. Assume that `hash' is defined and returns
## the receiver.
- def hashu8(me, n): return me.hash(_pack('B', n))
- def hashu16l(me, n): return me.hash(_pack('<H', n))
- def hashu16b(me, n): return me.hash(_pack('>H', n))
+ def _check_range(me, n, max):
+ if not (0 <= n <= max): raise OverflowError("out of range")
+ def hashu8(me, n):
+ me._check_range(n, 0xff)
+ return me.hash(_pack('B', n))
+ def hashu16l(me, n):
+ me._check_range(n, 0xffff)
+ return me.hash(_pack('<H', n))
+ def hashu16b(me, n):
+ me._check_range(n, 0xffff)
+ return me.hash(_pack('>H', n))
- def hashu32l(me, n): return me.hash(_pack('<L', n))
- def hashu32b(me, n): return me.hash(_pack('>L', n))
+ def hashu32l(me, n):
+ me._check_range(n, 0xffffffff)
+ return me.hash(_pack('<L', n))
+ def hashu32b(me, n):
+ me._check_range(n, 0xffffffff)
+ return me.hash(_pack('>L', n))
- def hashu64l(me, n): return me.hash(_pack('<Q', n))
- def hashu64b(me, n): return me.hash(_pack('>Q', n))
+ def hashu64l(me, n):
+ me._check_range(n, 0xffffffffffffffff)
+ return me.hash(_pack('<Q', n))
+ def hashu64b(me, n):
+ me._check_range(n, 0xffffffffffffffff)
+ return me.hash(_pack('>Q', n))
hashu64 = hashu64b
def hashbuf8(me, s): return me.hashu8(len(s)).hash(s)
def hashbuf16l(me, s): return me.hashu16l(len(s)).hash(s)
hashu64 = hashu64b
def hashbuf8(me, s): return me.hashu8(len(s)).hash(s)
def hashbuf16l(me, s): return me.hashu16l(len(s)).hash(s)