[Python-checkins] CVS: python/dist/src/Modules cPickle.c,2.53,2.53.2.1

Moshe Zadka moshez@users.sourceforge.net
2001年3月30日 09:21:01 -0800


Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv30389/Modules
Modified Files:
 Tag: release20-maint
	cPickle.c 
Log Message:
#126161 and 123634 -- Unicode strings could not be pickled correctly.
This is *backwards incompatible* with the previous pickling scheme,
which wasnot reversible
Index: cPickle.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/cPickle.c,v
retrieving revision 2.53
retrieving revision 2.53.2.1
diff -C2 -r2.53 -r2.53.2.1
*** cPickle.c	2000年10月04日 16:22:26	2.53
--- cPickle.c	2001年03月30日 17:20:58	2.53.2.1
***************
*** 1150,1153 ****
--- 1150,1198 ----
 
 
+ /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
+ backslash and newline characters to \uXXXX escapes. */
+ static PyObject *
+ modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
+ {
+ PyObject *repr;
+ char *p;
+ char *q;
+ 
+ static const char *hexdigit = "0123456789ABCDEF";
+ 
+ repr = PyString_FromStringAndSize(NULL, 6 * size);
+ if (repr == NULL)
+ return NULL;
+ if (size == 0)
+ 	return repr;
+ 
+ p = q = PyString_AS_STRING(repr);
+ while (size-- > 0) {
+ Py_UNICODE ch = *s++;
+ 	/* Map 16-bit characters to '\uxxxx' */
+ 	if (ch >= 256 || ch == '\\' || ch == '\n') {
+ *p++ = '\\';
+ *p++ = 'u';
+ *p++ = hexdigit[(ch >> 12) & 0xf];
+ *p++ = hexdigit[(ch >> 8) & 0xf];
+ *p++ = hexdigit[(ch >> 4) & 0xf];
+ *p++ = hexdigit[ch & 15];
+ }
+ 	/* Copy everything else as-is */
+ 	else
+ *p++ = (char) ch;
+ }
+ *p = '0円';
+ if (_PyString_Resize(&repr, p - q))
+ 	goto onError;
+ 
+ return repr;
+ 
+ onError:
+ Py_DECREF(repr);
+ return NULL;
+ }
+ 
+ 
 static int
 save_unicode(Picklerobject *self, PyObject *args, int doput) {
***************
*** 1162,1166 ****
 static char string = UNICODE;
 
! UNLESS (repr = PyUnicode_AsRawUnicodeEscapeString(args))
 return -1;
 
--- 1207,1212 ----
 static char string = UNICODE;
 
! UNLESS(repr = modified_EncodeRawUnicodeEscape(
! 		PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args)))
 return -1;
 
***************
*** 2746,2750 ****
 
 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
! if (len < 2) return bad_readline();
 
 UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))
--- 2792,2796 ----
 
 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
! if (len < 1) return bad_readline();
 
 UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))

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