Skip to content

Instantly share code, notes, and snippets.

@andresriancho
Created September 8, 2014 16:35
Show Gist options
  • Select an option

  • Save andresriancho/8015f5b92b6769fd9c9e to your computer and use it in GitHub Desktop.

Select an option

Save andresriancho/8015f5b92b6769fd9c9e to your computer and use it in GitHub Desktop.
Python cPickle bug
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
@andresriancho
Copy link
Author

@andresriancho
Copy link
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