How much is set in stone?

Andrew Dalke dalke at
Tue Nov 13 15:22:20 CET 2001

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

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

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

    "(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" + \

[dalke at pw600a ~]$ touch
[dalke at pw600a ~]$ cat
[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''\012sb."
>>> import cPickle
>>> cPickle.loads(s)
<tempfile.TemporaryFileWrapper instance at 0x1203492d8>
>>> 0
>>> ^D
[dalke at pw600a ~]$ cat
cat: 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 ''

>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

Sure, and it was doable in 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.

                    dalke at

More information about the Python-list mailing list