[Python-3000] pickle compatibility between 2.x and 3.0

Christian Heimes lists at cheimes.de
Fri Nov 2 10:02:01 CET 2007


Gregory P. Smith wrote:
> Brainstorming here... how about an optional callable argument when
> unpickling to let the developers write their own "magic guess" function?
> This callable should take a single parameter and its return value would be
> used as the unpickled string.

The general idea is good however a single magic method won't do any
good. The system must be able to handle cases where an object may
contain text strings and byte strings. Developers must be able to handle
the conversation on a per class base.

Here is some pseudo code. It should be able to handle most use cases.

def convertMyClass(args, state, reduceargs):
    """Convert MyClass instances

    A converter is a callable which accepts three arguments:
      args - __getinitargs__() or __getnewargs__()
      state - __getstate__()
      reduce - __reduce__()
    """
    # code here
    return args, state, reduceargs

class MyClass:
    pass

pickletools.registerMigrator(MyClass, convertMyClass)

Christian



More information about the Python-3000 mailing list