How much is set in stone?

Andrew Dalke dalke at dalkescientific.com
Tue Nov 13 09:22:20 EST 2001


Tim:
>Oddly enough, cPickle rejects that pickle [which passes an arbitrary
>string to os.remove] (it only deletes the file when
>using pickle.py).

I assume it's checking for a ClassType.  By testing, yes.

The following is a pickle of
      tempfile.TemporaryFileWrapper(StringIO.StringIO(""), "remove.me")

    "(itempfile\012TemporaryFileWrapper\012p1\012(dp2\012S'file'" + \
    "\012p3\012(iStringIO\012StringIO\012(dp4\012S'closed'\012p5" + \
    "\012I0\012sS'len'\012p6\012I0\012sS'buf'\012p7\012S''\012sS'" + \
    "buflist'\012p8\012(lsS'softspace'\012p9\012I0\012sS'pos'\012" + \
    "p10\012I0\012sbsS'path'\012p11\012S'remove.me'\012sb."

[dalke at pw600a ~]$ touch remove.me
[dalke at pw600a ~]$ cat remove.me
[dalke at pw600a ~]$ ./python
Python 2.2b1+ (#8, Nov  8 2001, 20:45:09)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = "(itempfile\012TemporaryFileWrapper\012p1\012(dp2\012S'file'" + \
...     "\012p3\012(iStringIO\012StringIO\012(dp4\012S'closed'\012p5" + \
...     "\012I0\012sS'len'\012p6\012I0\012sS'buf'\012p7\012S''\012sS'" + \
...     "buflist'\012p8\012(lsS'softspace'\012p9\012I0\012sS'pos'\012" + \
...     "p10\012I0\012sbsS'path'\012p11\012S'remove.me'\012sb."
>>> import cPickle
>>> cPickle.loads(s)
<tempfile.TemporaryFileWrapper instance at 0x1203492d8>
>>> 0
0
>>> ^D
[dalke at pw600a ~]$ cat remove.me
cat: remove.me: No such file or directory
[dalke at pw600a ~]$

The '0' is to force '_' to get rid of the reference to a
TemporaryFileWrapper, whose destructor closes the StringIO then
removes the given filename, which is 'remove.me'

>We'll try to repair that for 2.2.  In particular, you can set a find_global
>attribute on a cPickle Unpickler to supply your own function for resolving
>names, and of special interest is that if you set it to None, the Unpickler
>won't import modules or unpickle class instances at all.  You could, of
>course, also supply a function that accetps only a set of blessed names
>(etc).

Sure, and it was doable in pickle.py as well by making a subclass
and overriding the find_class method (example shown elsewhere).
But I didn't know I needed to do that, and it's hard to figure out
which classes are okay to pickle safely.  Some of my own classes
have these sorts of filesystem cleanup code in __del__.  So
pickles are hard to use safely for insecure-but-non-trivial data.

Hope this is all documented in nice friendly letters.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list