Author sbt
Recipients sbt
Date 2012-03-01.14:34:06
SpamBayes Score 2.23832e-11
Marked as misclassified No
Message-id <1330612450.14.0.527502526549.issue14166@psf.upfronthosting.co.za>
In-reply-to
Content
Currently the only documented way to have customised pickling for a type is to register a reduction function with the global dispatch table managed by the copyreg module.  But such global changes are liable to disrupt other code which uses pickling.

Multiprocessing deals with this by defining a ForkingPickler class which subclasses the pure python _Pickler class (using undocumented features), and supports registering reduction functions specifically for that class.

I would like to see some documented alternative which works with both C and Python implementations.  At least then multiprocessing can avoid using slow pure python pickling.  

The attached patch allows a pickler object to have a private dispatch table which it uses *instead* of the global one.  It lets one write code like

    p = pickle.Pickler(...)
    p.dispatch_table = copyreg.dispatch_table.copy()
    p.dispatch_table[SomeClass] = reduce_SomeClass

or

    class MyPickler(pickle.Pickler):
        dispatch_table = copyreg.dispatch_table.copy()

    MyPickler.dispatch_table[SomeClass] = reduce_SomeClass
    p = MyPickler(...)

The equivalent using copyreg would be

    copyreg.pickle(SomeClass, reduce_SomeClass)
    p = pickle.Pickler(...)
History
Date User Action Args
2012-03-01 14:34:10sbtsetrecipients: + sbt
2012-03-01 14:34:10sbtsetmessageid: <1330612450.14.0.527502526549.issue14166@psf.upfronthosting.co.za>
2012-03-01 14:34:09sbtlinkissue14166 messages
2012-03-01 14:34:08sbtcreate