diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2007-07-18 02:28:27 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2007-07-18 02:28:27 (GMT) |
commit | 10a60b3ec0cdf7eeac98258fc53a33b7026f8ff3 (patch) | |
tree | 533e2a67c2c082cd9534c70293977f9245425188 /Lib/pickle.py | |
parent | 6f2df4d5e193d54244b0c2de91ef0ab1604b9243 (diff) | |
download | cpython-10a60b3ec0cdf7eeac98258fc53a33b7026f8ff3.zip cpython-10a60b3ec0cdf7eeac98258fc53a33b7026f8ff3.tar.gz cpython-10a60b3ec0cdf7eeac98258fc53a33b7026f8ff3.tar.bz2 |
Change Py_BuildValue to generate Unicode objects for
's' and 'c' codes.
Change pickle to dump bytes objects using the 'S'
code, and to load the 'S' code as byte objects.
Change datetime and array to generate and expect
bytes objects in reduce/unreduce.
Diffstat (limited to 'Lib/pickle.py')
-rw-r--r-- | Lib/pickle.py | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py index 27f7eca..c158b8d 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -506,6 +506,20 @@ class Pickler: self.memoize(obj) dispatch[str8] = save_string + def save_bytes(self, obj): + # Like save_string + if self.bin: + n = len(obj) + if n < 256: + self.write(SHORT_BINSTRING + bytes([n]) + bytes(obj)) + else: + self.write(BINSTRING + pack("<i", n) + bytes(obj)) + else: + # Strip leading 'b' + self.write(STRING + bytes(repr(obj).lstrip("b")) + b'\n') + self.memoize(obj) + dispatch[bytes] = save_bytes + def save_unicode(self, obj, pack=struct.pack): if self.bin: encoded = obj.encode('utf-8') @@ -931,12 +945,12 @@ class Unpickler: break else: raise ValueError, "insecure string pickle" - self.append(str8(codecs.escape_decode(rep)[0])) + self.append(bytes(codecs.escape_decode(rep)[0])) dispatch[STRING[0]] = load_string def load_binstring(self): len = mloads(b'i' + self.read(4)) - self.append(str8(self.read(len))) + self.append(self.read(len)) dispatch[BINSTRING[0]] = load_binstring def load_unicode(self): @@ -950,7 +964,7 @@ class Unpickler: def load_short_binstring(self): len = ord(self.read(1)) - self.append(str8(self.read(len))) + self.append(self.read(len)) dispatch[SHORT_BINSTRING[0]] = load_short_binstring def load_tuple(self): |