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

Greg Ward gward@python.net
Fri, 11 Jan 2002 09:49:29 -0500


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.

An object schema is a collection of classes (including the attributes in
each class and the type of each attribute), atomic types, and type
aliases.

An atomic type is a type with no sub-types; by default every Grouch
schema has five atomic types: int, string, long, complex, and float.
You can easily add new atomic types, eg. the MEMS Exchange virtual fab
has mxDateTime as an atomic type.

A type alias is just what it sounds like, eg. "Foo" might be an alias
for "foo.Foo" (a fully qualified class name representing a Grouch
instance type), and "real" might be an alias for "int|long|float" (a
Grouch union type).

See http://www.mems-exchange.org/software/grouch/

Anyways, that's not terribly relevant, but it gives me an excuse to plug
my most arcane and (IMHO) interesting Python hack.

[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?  I don't see how that makes any difference: I still
need to intercede at pickle/unpickle time to make this happen.

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.

> You should probably first check wether the pickle string is
> identical in 2.1 and 2.2 and then go on from there.

Excellent idea -- thanks!

        Greg
-- 
Greg Ward - nerd                                        gward@python.net
http://starship.python.net/~gward/
"Eine volk, eine reich, eine führer" --Hitler
"One world, one web, one program" --Microsoft