Why does pickle use eval for loading string values?

Mike C. Fletcher mcfletch at rogers.com
Thu Jan 10 23:55:43 EST 2002

  >>> b = '"this \\" there"'
  >>> eval( b)
'this " there'
  >>> b[1:-1]
'this \\" there'

That is, if you're storing the string in a format for eval, then you're
_not_ formatting it for restore from [1:-1].  Because of that the change
would be backward incompatible with every previous version of


Brian Kelley wrote:

 > 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

    Mike C. Fletcher

More information about the Python-list mailing list