[Python-Dev] Change in unpickle order in 2.2?

M.-A. Lemburg mal@lemburg.com
Fri, 11 Jan 2002 16:11:59 +0100


Greg Ward wrote:
> 
> On 10 January 2002, M.-A. Lemburg said:
> > What's Grouch ?
> 
> [Grouch is a system for 1) describing a Python object schema, and 2)
> traversing an existing object graph (eg. a pickle or ZODB) to ensure
> that it conforms to that object schema.]

Sounds very interesting :-)

> [me]
> > (The reason for the pickle-time intervention is that Grouch stores type
> > objects in its data structure, and you can't pickle type objects.  So it
> > hangs on to a representive value of the type for pickling -- eg. for the
> > "integer" type, it keeps both IntType and 0 in memory, but only pickles
> > 0, and uses type(0) to get IntType back at unpickle time.)
> 
> [MAL]
> > Why don't you use a special reduce function which takes the
> > tp_name as index into the types module ? Storing strings should
> > avoid all complicated type object saving.
> 
> I'm not sure I understand what you're saying.  Are you just suggesting
> that, when I need to pickle IntType, I pickle the string "int" instead
> of the integer 0? 

Right. It needn't be 'int', any string will do as long as you
have a mapping from strings to type objects.

> I don't see how that makes any difference: I still
> need to intercede at pickle/unpickle time to make this happen.

Well, I suppose with the new Python 2.2 version you could add a
special __reduce__ method to type objects which takes of this
for you.

For older versions, you should probably register a pickle handler
for type objects which does the same. Pickle should then use this
handler for pickling the type object.

> Also, the fact that type(x).__name__ is not consistent across Python
> versions or implementations (Jython) screws this up.  Grouch now has its
> own canonical set of type names because of this, and I could easily
> reverse that dictionary to make a typename->typeobject mapping.  But I
> don't see how pickling "int" is a win over pickling 0, when what I
> *really* want to do is pickle IntType.

True, but it saves you the trouble of storing global references
to the type constructors in the pickle. Your system will do the
mapping using the above hooks.

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Company & Consulting:                           http://www.egenix.com/
Python Software:                   http://www.egenix.com/files/python/