[Python-checkins] python/dist/src/Lib pickle.py,1.130,1.131 pickletools.py,1.24,1.25

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
2003年1月31日 08:43:42 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv21674/Lib
Modified Files:
	pickle.py pickletools.py 
Log Message:
It's Official: for LONG1/LONG4, a "byte count" of 0 is taken as a
shortcut meaning 0L. This allows LONG1 to encode 0L in two bytes
total.
Index: pickle.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/pickle.py,v
retrieving revision 1.130
retrieving revision 1.131
diff -C2 -d -r1.130 -r1.131
*** pickle.py	31 Jan 2003 04:04:23 -0000	1.130
--- pickle.py	31 Jan 2003 16:43:39 -0000	1.131
***************
*** 1292,1295 ****
--- 1292,1300 ----
 def encode_long(x):
 r"""Encode a long to a two's complement little-endian binary string.
+ Note that 0L is a special case, returning an empty string, to save a
+ byte in the LONG1 pickling context.
+ 
+ >>> encode_long(0L)
+ ''
 >>> encode_long(255L)
 '\xff\x00'
***************
*** 1308,1312 ****
 
 if x == 0:
! return '\x00'
 if x > 0:
 ashex = hex(x)
--- 1313,1317 ----
 
 if x == 0:
! return ''
 if x > 0:
 ashex = hex(x)
***************
*** 1317,1321 ****
 # need an even # of nibbles for unhexlify
 ashex = "0x0" + ashex[2:]
! elif ashex[2] >= '8':
 # "looks negative", so need a byte of sign bits
 ashex = "0x00" + ashex[2:]
--- 1322,1326 ----
 # need an even # of nibbles for unhexlify
 ashex = "0x0" + ashex[2:]
! elif int(ashex[2], 16) >= 8:
 # "looks negative", so need a byte of sign bits
 ashex = "0x00" + ashex[2:]
***************
*** 1331,1339 ****
 # need an even # of nibbles for unhexlify
 nibbles += 1
! nbytes = nibbles >> 1
! x += 1L << (nbytes * 8)
 assert x > 0
 ashex = hex(x)
! if x >> (nbytes * 8 - 1) == 0:
 # "looks positive", so need a byte of sign bits
 ashex = "0xff" + x[2:]
--- 1336,1344 ----
 # need an even # of nibbles for unhexlify
 nibbles += 1
! nbits = nibbles * 4
! x += 1L << nbits
 assert x > 0
 ashex = hex(x)
! if x >> (nbits - 1) == 0:
 # "looks positive", so need a byte of sign bits
 ashex = "0xff" + x[2:]
***************
*** 1349,1352 ****
--- 1354,1360 ----
 def decode_long(data):
 r"""Decode a long from a two's complement little-endian binary string.
+ 
+ >>> decode_long('')
+ 0L
 >>> decode_long("\xff\x00")
 255L
***************
*** 1363,1370 ****
 """
 
 ashex = _binascii.hexlify(data[::-1])
 n = long(ashex, 16)
 if data[-1] >= '\x80':
! n -= 1L << (len(data) * 8)
 return n
 
--- 1371,1381 ----
 """
 
+ nbytes = len(data)
+ if nbytes == 0:
+ return 0L
 ashex = _binascii.hexlify(data[::-1])
 n = long(ashex, 16)
 if data[-1] >= '\x80':
! n -= 1L << (nbytes * 8)
 return n
 
Index: pickletools.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/pickletools.py,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** pickletools.py	30 Jan 2003 16:35:08 -0000	1.24
--- pickletools.py	31 Jan 2003 16:43:39 -0000	1.25
***************
*** 621,624 ****
--- 621,626 ----
 r"""
 >>> import StringIO
+ >>> read_long1(StringIO.StringIO("\x00"))
+ 0L
 >>> read_long1(StringIO.StringIO("\x02\xff\x00"))
 255L
***************
*** 629,633 ****
 >>> read_long1(StringIO.StringIO("\x02\x00\x80"))
 -32768L
- >>>
 """
 
--- 631,634 ----
***************
*** 646,649 ****
--- 647,651 ----
 This first reads one byte as an unsigned size, then reads that
 many bytes and interprets them as a little-endian 2's-complement long.
+ If the size is 0, that's taken as a shortcut for the long 0L.
 """)
 
***************
*** 659,663 ****
 >>> read_long4(StringIO.StringIO("\x02\x00\x00\x00\x00\x80"))
 -32768L
! >>>
 """
 
--- 661,666 ----
 >>> read_long4(StringIO.StringIO("\x02\x00\x00\x00\x00\x80"))
 -32768L
! >>> read_long1(StringIO.StringIO("\x00\x00\x00\x00"))
! 0L
 """
 
***************
*** 678,682 ****
 This first reads four bytes as a signed size (but requires the
 size to be >= 0), then reads that many bytes and interprets them
! as a little-endian 2's-complement long.
 """)
 
--- 681,687 ----
 This first reads four bytes as a signed size (but requires the
 size to be >= 0), then reads that many bytes and interprets them
! as a little-endian 2's-complement long. If the size is 0, that's taken
! as a shortcut for the long 0L, although LONG1 should really be used
! then instead (and in any case where # of bytes < 256).
 """)
 

AltStyle によって変換されたページ (->オリジナル) /