Why does pickle use eval for loading string values?

Brian Kelley bkelley at wi.mit.edu
Thu Jan 10 16:05:40 EST 2002


Here's something I never quite understood.  pickle and cPickle use eval 
to load string values.  This seems fairly neat but why is it necessary?

One fairly serious drawback is that the eval interns the string values 
which means that they can never get garbage collected.  This has been a 
problem for some of my applications (you can do a google search for 
pickle and intern to see an old thread on this)

I replaced load_string in pickle with the following:

self.append(rep[1:-1])

initially the loader was

self.append(eval(rep,
       {'__builtins__': {}})) # Let's be careful

I applied the same thing to cPickle.

This appears to be slightly faster and doesn't have the intern problem 
mentioned above.

So what am I missing?  Is there some reason why eval should be used?

I'm tempted to report this as a bug in pickle since interning strings is 
a undocumented side-effect.  The fix seems easy so please shoot my down 
if I'm being niave.

Brian Kelley
Whitehead Institute for Biomedical Research




More information about the Python-list mailing list