[issue14166] private dispatch table for picklers

sbt report at bugs.python.org
Thu Mar 1 15:34:10 CET 2012

New submission from sbt <shibturn at gmail.com>:

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


    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(...)

files: pickle_dispatch.patch
keywords: patch
messages: 154695
nosy: sbt
priority: normal
severity: normal
status: open
title: private dispatch table for picklers
type: enhancement
versions: Python 3.3
Added file: http://bugs.python.org/file24697/pickle_dispatch.patch

Python tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list