copy_reg.py 501 Bytes
Newer Older
1 2
# Helper to provide extensibility for pickle/cPickle.

3 4 5 6 7 8
dispatch_table = {}
safe_constructors = {}

def pickle(ob_type, pickle_function, constructor_ob = None):
    dispatch_table[ob_type] = pickle_function

9
    if constructor_ob is not None:
10 11 12 13 14
        constructor(constructor_ob)

def constructor(object):
    safe_constructors[object] = 1

15 16
# Example: provide pickling support for complex numbers.

17
def pickle_complex(c):
18
    return complex, (c.real, c.imag)
19

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