[Python-porting] pickle data

Barry Warsaw barry at python.org
Wed Dec 17 02:26:57 CET 2014


Here's an interesting situation I am faced with as I port Mailman 3 to Python
3.  I haven't seen any other discussion of it, so I thought I'd post here for
posterity.

Let's say you have a pickle created in Python 2 that is to be read in Python
3.  In Mailman 2, persistent mailing list data is stored in pickles.

It seems like both Python 2 types (unicode and str/bytes) get unpickled as
Python 3 str types.  It makes sense because:

* Python 2 unicode should unpickle as Python 3 str
* In Python 2, bytes are just an alias for str
* There's no way to know the intent of whether Python 2 "bytes" should be
  unpickled as Python 3 bytes or str.

Code:

-----put.py-----
from six.moves.cPickle import dump

d = {
    b'b': b'b',
    u'u': u'u',
    's': 's',
    }

with open('/tmp/foo.pck', 'wb') as fp:
    dump(d, fp)
-----put.py-----

-----get.py-----
from pprint import pprint
from six.moves.cPickle import load

with open('/tmp/foo.pck', 'rb') as fp:
    d = load(fp)

pprint(d)
-----get.py-----

$ python2 /tmp/put.py
$ python3 /tmp/get.py
{'b': 'b', 's': 's', 'u': 'u'}
$ python2 /tmp/get.py
{'b': 'b', 's': 's', u'u': u'u'}

Cheers,
-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-porting/attachments/20141216/9826d73a/attachment.sig>


More information about the Python-porting mailing list