Created
September 8, 2014 16:35
-
-
Save andresriancho/8015f5b92b6769fd9c9e to your computer and use it in GitHub Desktop.
Python cPickle bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cPickle | |
| import Cookie | |
| c = Cookie.SimpleCookie() | |
| c['abc'] = 'def' | |
| unpickled_highest = cPickle.loads(cPickle.dumps(c, cPickle.HIGHEST_PROTOCOL)) | |
| unpickled_default = cPickle.loads(cPickle.dumps(c)) | |
| print "c['abc'].value ", c['abc'].value | |
| print "unpickled_default['abc'].value", unpickled_default['abc'].value | |
| print "unpickled_highest['abc'].value", unpickled_highest['abc'].value | |
| assert unpickled_default['abc'].value == c['abc'].value | |
| assert unpickled_highest['abc'].value == c['abc'].value |
Author
Author
Ugly, generic fix:
import cPickle
def cpickle_dumps(obj):
"""
pickle doesn't handle dict sub-classes when using HIGHEST_PROTOCOL, so
we do some checks here to avoid bugs.
We pickle using HIGHEST_PROTOCOL if obj is not a dict subclass, otherwise we
just use protocol version 1.
:see: http://bugs.python.org/issue826897
:param obj: The object to pickle
:return: The pickled version of obj
"""
if isinstance(obj, dict):
return cPickle.dumps(obj, 1)
return cPickle.dumps(obj, cPickle.HIGHEST_PROTOCOL)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://bugs.python.org/issue826897