blob: db4eb6627014d7c9d5f5ec4d898819cdad71efe4 [file] [log] [blame]
Guido van Rossum72be3061997-05-20 18:03:22 +00001# Helper to provide extensibility for pickle/cPickle.
2
Guido van Rossum47065621997-04-09 17:44:11 +00003dispatch_table = {}
4safe_constructors = {}
5
6def pickle(ob_type, pickle_function, constructor_ob = None):
7 dispatch_table[ob_type] = pickle_function
8
Guido van Rossum72be3061997-05-20 18:03:22 +00009 if constructor_ob is not None:
Guido van Rossum47065621997-04-09 17:44:11 +000010 constructor(constructor_ob)
11
12def constructor(object):
13 safe_constructors[object] = 1
14
Guido van Rossum72be3061997-05-20 18:03:22 +000015# Example: provide pickling support for complex numbers.
Guido van Rossum47065621997-04-09 17:44:11 +000016
Guido van Rossum72be3061997-05-20 18:03:22 +000017def pickle_complex(c):
18 return complex, (c.real, c.imag)
19
20pickle(type(1j), pickle_complex, complex)
Guido van Rossum47065621997-04-09 17:44:11 +000021