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

Guido van Rossum guido@cnri.reston.va.us
Fri, 10 Mar 2000 18:20:12 -0500 (EST)


Update of /projects/cvsroot/python/dist/src/Lib
In directory eric:/home/guido/hp/mal/py-patched/Lib

Modified Files:
	pickle.py 
Log Message:
Marc-Andre Lemburg: support pickling Unicode objects, both in text
mode ('V') and in binary mode ('X').


Index: pickle.py
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Lib/pickle.py,v
retrieving revision 1.36
retrieving revision 1.37
diff -C2 -r1.36 -r1.37
*** pickle.py	2000/02/04 15:10:33	1.36
--- pickle.py	2000/03/10 23:20:09	1.37
***************
*** 24,28 ****
  """
  
! __version__ = "$Revision: 1.36 $"       # Code version
  
  from types import *
--- 24,28 ----
  """
  
! __version__ = "$Revision: 1.37 $"       # Code version
  
  from types import *
***************
*** 65,68 ****
--- 65,70 ----
  BINSTRING       = 'T'
  SHORT_BINSTRING = 'U'
+ UNICODE         = 'V'
+ BINUNICODE      = 'X'
  APPEND          = 'a'
  BUILD           = 'b'
***************
*** 276,279 ****
--- 278,298 ----
      dispatch[StringType] = save_string
  
+     def save_unicode(self, object):
+         d = id(object)
+         memo = self.memo
+ 
+         if (self.bin):
+             encoding = object.encode('utf-8')
+             l = len(encoding)
+             s = mdumps(l)[1:]
+             self.write(BINUNICODE + s + encoding)
+         else:
+             self.write(UNICODE + object.encode('raw-unicode-escape') + '\n')
+ 
+         memo_len = len(memo)
+         self.write(self.put(memo_len))
+         memo[d] = (memo_len, object)
+     dispatch[UnicodeType] = save_unicode
+ 
      def save_tuple(self, object):
  
***************
*** 566,569 ****
--- 585,597 ----
          self.append(self.read(len))
      dispatch[BINSTRING] = load_binstring
+ 
+     def load_unicode(self):
+         self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
+     dispatch[UNICODE] = load_unicode
+ 
+     def load_binunicode(self):
+         len = mloads('i' + self.read(4))
+         self.append(unicode(self.read(len),'utf-8'))
+     dispatch[BINUNICODE] = load_binunicode
  
      def load_short_binstring(self):