Issue #13505: Make pickling of bytes object compatible with Python 2.

Initial patch by sbt.
This commit is contained in:
Alexandre Vassalotti 2011-12-13 13:08:09 -05:00
parent 7b7e39a61f
commit 3bfc65a25b
5 changed files with 94 additions and 57 deletions

View file

@ -487,7 +487,11 @@ class _Pickler:
def save_bytes(self, obj, pack=struct.pack):
if self.proto < 3:
self.save_reduce(bytes, (list(obj),), obj=obj)
if len(obj) == 0:
self.save_reduce(bytes, (), obj=obj)
else:
self.save_reduce(codecs.encode,
(str(obj, 'latin1'), 'latin1'), obj=obj)
return
n = len(obj)
if n < 256: