Questions on Pickle and Shelve

Chris Warrick kwpolska at gmail.com
Fri Nov 6 11:43:17 EST 2015


On 6 November 2015 at 12:53, Virgil Stokes <vs at it.uu.se> wrote:
> Here is snippet of Python (vers. 2.7.10) code that bothers me.
>
> [snip bad code]
>
> 1)  Which (the pickle or shelve code) takes less total RAM, if dogs and cats
> were very large?
> 2)  When the last shelve.open is given, is the entire contents of
> shelve.data transferred to RAM?  Note, if the print sfile is uncommented
> then the entire contents of shelve.data is printed out.
>
> I was under the impression that the entire contents of a shelved file was
> not transferred to RAM when it was opened.
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list

1) That does not matter. I do know that shelve files can grow larger
over time though.
2) Shelve files are pickles in disguise. Which means they need to be
loaded into memory, executing *arbitrary code*.

Here’s a question for you:
3) Why are you using either? Both are unsafe and can lead to issues if
you take untrusted files. Use JSON.

-- 
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16



More information about the Python-list mailing list