diff -r 336a614f35a3 Lib/pickle.py --- a/Lib/pickle.py Sun Feb 19 10:17:30 2012 -0500 +++ b/Lib/pickle.py Sun Feb 19 20:01:08 2012 +0100 @@ -174,7 +174,7 @@ class _Pickler: - def __init__(self, file, protocol=None, *, fix_imports=True): + def __init__(self, file, protocol=None, *, fix_imports=True, bytestr=False): """This takes a binary file for writing a pickle data stream. The optional protocol argument tells the pickler to use the @@ -195,6 +195,9 @@ If fix_imports is True and protocol is less than 3, pickle will try to map the new Python 3.x names to the old module names used in Python 2.x, so that the pickle data stream is readable with Python 2.x. + + If bytestr is True and protocol is less than 3, bytes will be stored as + 8-bit string instead of as bytes object. """ if protocol is None: protocol = DEFAULT_PROTOCOL @@ -211,6 +214,7 @@ self.bin = protocol >= 1 self.fast = 0 self.fix_imports = fix_imports and protocol < 3 + self.bytestr = bytestr and protocol < 3 def clear_memo(self): """Clears the pickler's "memo". @@ -483,7 +487,7 @@ self.write(FLOAT + repr(obj).encode("ascii") + b'\n') dispatch[float] = save_float - def save_bytes(self, obj, pack=struct.pack): + def save_bytes_as_bytes(self, obj, pack=struct.pack): if self.proto < 3: if len(obj) == 0: self.save_reduce(bytes, (), obj=obj) @@ -497,6 +501,24 @@ else: self.write(BINBYTES + pack("d', self.read(8))[0]) dispatch[BINFLOAT[0]] = load_binfloat + def decode_string(self, value): + if self.bytestr: + return value + else: + return value.decode(self.encoding, self.errors) + def load_string(self): orig = self.readline() rep = orig[:-1] @@ -946,15 +977,13 @@ break else: raise ValueError("insecure string pickle: %r" % orig) - self.append(codecs.escape_decode(rep)[0] - .decode(self.encoding, self.errors)) + self.append(self.decode_string(codecs.escape_decode(rep)[0])) dispatch[STRING[0]] = load_string def load_binstring(self): len = mloads(b'i' + self.read(4)) data = self.read(len) - value = str(data, self.encoding, self.errors) - self.append(value) + self.append(self.decode_string(data)) dispatch[BINSTRING[0]] = load_binstring def load_binbytes(self): @@ -973,9 +1002,8 @@ def load_short_binstring(self): len = ord(self.read(1)) - data = bytes(self.read(len)) - value = str(data, self.encoding, self.errors) - self.append(value) + data = self.read(len) + self.append(self.decode_string(data)) dispatch[SHORT_BINSTRING[0]] = load_short_binstring def load_short_binbytes(self):