ValueError: insecure string pickle

“are much more likely than a never-observed bug in Python itself in a functionality that’s used billions of times a day all over the world”: it always amazes me how cross people get in these forums.

One easy way to get this problem is by forgetting to close the stream that you’re using for dumping the data structure. I just did

>>> out = open('xxx.dmp', 'w')
>>> cPickle.dump(d, out)
>>> k = cPickle.load(open('xxx.dmp', 'r'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: insecure string pickle

Which is why I came here in the first place, because I couldn’t see what I’d done wrong.
And then I actually thought about it, rather than just coming here, and realized that I should have done:

>>> out = open('xxx.dmp', 'w')
>>> cPickle.dump(d, out)
>>> out.close() # close it to make sure it's all been written
>>> k = cPickle.load(open('xxx.dmp', 'r'))

Easy to forget. Didn’t need people being told that they are idiots.

Leave a Comment