From: Finn B. <bc...@us...> - 2001-01-21 16:24:57
|
Update of /cvsroot/jython/jython/org/python/modules In directory usw-pr-cvs1:/tmp/cvs-serv1051 Modified Files: cPickle.java Log Message: Added support for UNICODE and BINUNICODE. Index: cPickle.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/modules/cPickle.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -r1.13 -r1.14 *** cPickle.java 2000/12/11 18:46:30 1.13 --- cPickle.java 2001/01/21 16:25:08 1.14 *************** *** 451,454 **** --- 451,456 ---- final static char BINSTRING = 'T'; final static char SHORT_BINSTRING = 'U'; + final static char UNICODE = 'V'; + final static char BINUNICODE = 'X'; final static char APPEND = 'a'; final static char BUILD = 'b'; *************** *** 1062,1081 **** final private void save_string(PyObject object) { if (bin) { ! int l = object.__len__(); ! if (l < 256) { ! file.write(SHORT_BINSTRING); ! file.write((char)l); ! } else { ! file.write(BINSTRING); ! file.write((char)( l & 0xFF)); ! file.write((char)((l >>> 8 ) & 0xFF)); ! file.write((char)((l >>> 16) & 0xFF)); ! file.write((char)((l >>> 24) & 0xFF)); ! } ! file.write(object.toString()); } else { ! file.write(STRING); ! file.write(object.__repr__().toString()); file.write("\n"); } --- 1064,1096 ---- final private void save_string(PyObject object) { + boolean unicode = ((PyString) object).isunicode(); + String str = object.toString(); + if (bin) { ! if (unicode) ! str = codecs.PyUnicode_EncodeUTF8(str, "struct"); ! int l = str.length(); ! if (l < 256 && !unicode) { ! file.write(SHORT_BINSTRING); ! file.write((char)l); ! } else { ! if (unicode) ! file.write(BINUNICODE); ! else ! file.write(BINSTRING); ! file.write((char)( l & 0xFF)); ! file.write((char)((l >>> 8 ) & 0xFF)); ! file.write((char)((l >>> 16) & 0xFF)); ! file.write((char)((l >>> 24) & 0xFF)); ! } ! file.write(str); } else { ! if (unicode) { ! file.write(UNICODE); ! file.write(codecs.PyUnicode_EncodeRawUnicodeEscape(str, "strict", true)); ! } else { ! file.write(STRING); ! file.write(object.__repr__().toString()); ! } file.write("\n"); } *************** *** 1533,1536 **** --- 1548,1553 ---- case BINSTRING: load_binstring(); break; case SHORT_BINSTRING: load_short_binstring(); break; + case UNICODE: load_unicode(); break; + case BINUNICODE: load_binunicode(); break; case TUPLE: load_tuple(); break; case EMPTY_TUPLE: load_empty_tuple(); break; *************** *** 1659,1663 **** int i; char ch = '\0'; ! for (i = 1; i < line.length(); i++) { ch = line.charAt(i); if (ch == quote && nslash % 2 == 0) --- 1676,1681 ---- int i; char ch = '\0'; ! int n = line.length(); ! for (i = 1; i < n; i++) { ch = line.charAt(i); if (ch == quote && nslash % 2 == 0) *************** *** 1669,1673 **** } if (ch != quote) ! throw Py.ValueError("insecure string pickle "); for (i++ ; i < line.length(); i++) { --- 1687,1691 ---- } if (ch != quote) ! throw Py.ValueError("insecure string pickle"); for (i++ ; i < line.length(); i++) { *************** *** 1675,1679 **** throw Py.ValueError("insecure string pickle " + i); } ! value = org.python.parser.SimpleNode.parseString(line, 1, 0, 0); push(new PyString(value)); } --- 1693,1698 ---- throw Py.ValueError("insecure string pickle " + i); } ! value = PyString.decode_UnicodeEscape(line, 1, n-1, "strict", false); ! push(new PyString(value)); } *************** *** 1695,1698 **** --- 1714,1734 ---- } + + final private void load_unicode() { + String line = file.readlineNoNl(); + int n = line.length(); + String value = codecs.PyUnicode_DecodeRawUnicodeEscape(line, "strict"); + push(new PyString(value)); + } + + final private void load_binunicode() { + String d = file.read(4); + int len = d.charAt(0) | + (d.charAt(1)<<8) | + (d.charAt(2)<<16) | + (d.charAt(3)<<24); + String line = file.read(len); + push(new PyString(codecs.PyUnicode_DecodeUTF8(line, "strict"))); + } final private void load_tuple() { |