[Python-checkins] CVS: python/dist/src/Lib pickle.py,1.47,1.48

Tim Peters tim_one@users.sourceforge.net
Mon, 09 Apr 2001 22:02:54 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv1600/python/dist/src/Lib

Modified Files:
	pickle.py 
Log Message:
test_pickle works on sizeof(long)==8 boxes again.
pickle.py
    The code implicitly assumed that all ints fit in 4 bytes, causing all
    sorts of mischief (from nonsense results to corrupted pickles).
    Repaired that.
marshal.c
    The int marshaling code assumed that right shifts of signed longs
    sign-extend.  Repaired that.


Index: pickle.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/pickle.py,v
retrieving revision 1.47
retrieving revision 1.48
diff -C2 -r1.47 -r1.48
*** pickle.py	2001/04/10 02:48:53	1.47
--- pickle.py	2001/04/10 05:02:52	1.48
***************
*** 244,259 ****
      def save_int(self, object):
          if self.bin:
!             i = mdumps(object)[1:]
!             if i[-2:] == '\000\000':
!                 if i[-3] == '\000':
!                     self.write(BININT1 + i[:-3])
!                     return
! 
!                 self.write(BININT2 + i[:-2])
                  return
! 
!             self.write(BININT + i)
!         else:
!             self.write(INT + `object` + '\n')
      dispatch[IntType] = save_int
  
--- 244,266 ----
      def save_int(self, object):
          if self.bin:
!             # If the int is small enough to fit in a signed 4-byte 2's-comp
!             # format, we can store it more efficiently than the general
!             # case.
!             high_bits = object >> 31  # note that Python shift sign-extends
!             if  high_bits == 0 or high_bits == -1:
!                 # All high bits are copies of bit 2**31, so the value
!                 # fits in a 4-byte signed int.
!                 i = mdumps(object)[1:]
!                 assert len(i) == 4
!                 if i[-2:] == '\000\000':    # fits in 2-byte unsigned int
!                     if i[-3] == '\000':     # fits in 1-byte unsigned int
!                         self.write(BININT1 + i[0])
!                     else:
!                         self.write(BININT2 + i[:2])
!                 else:
!                     self.write(BININT + i)
                  return
!         # Text pickle, or int too big to fit in signed 4-byte format.
!         self.write(INT + `object` + '\n')
      dispatch[IntType] = save_int