copy_reg problem

Rainer Deyke root at rainerdeyke.com
Wed Oct 4 22:39:23 EDT 2000


Using Python 1.5.2:

The documentation for module 'copy_reg' states this:

pickle (type, function[, constructor])
  Declares that function should be used as a ``reduction'' function for
objects
  of type or class type. function should return either a string or a tuple.
The
          ^^^^^^^^
  optional constructor parameter, if provided, is a callable object which
can be
  used to reconstruct the object when called with the tuple of arguments
returned
  by function at pickling time.

The module itself gives the following example:
  def pickle_complex(c):
    return complex, (c.real, c.imag)

  pickle(type(1j), pickle_complex, complex)

I wrote a simple module that relies that tests this behavior:

########## start program
import copy_reg
import pickle

class T:
  pass

the_T = T()

def get_the_T():
  return the_T

def pickle_T(t):
  return get_the_T, ()

copy_reg.pickle(T, pickle_T, get_the_T)

assert pickle.loads(pickle.dumps(the_T)) is the_T
########## end program

It fails with an assertion error.  Printing pickle.dumps(the_T) confirms
that the object is pickled as if 'copy_reg.pickle' had not been called.  Who
is wrong here: the documentation, the pickle module (and cPickle), or I?  In
any case, how do I fix this?


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list