Guido van Rossum | 72be306 | 1997-05-20 18:03:22 +0000 | [diff] [blame] | 1 | # Helper to provide extensibility for pickle/cPickle. |
| 2 | |
Guido van Rossum | 4706562 | 1997-04-09 17:44:11 +0000 | [diff] [blame] | 3 | dispatch_table = {} |
| 4 | safe_constructors = {} |
| 5 | |
| 6 | def pickle(ob_type, pickle_function, constructor_ob = None): |
| 7 | dispatch_table[ob_type] = pickle_function |
| 8 | |
Guido van Rossum | 72be306 | 1997-05-20 18:03:22 +0000 | [diff] [blame] | 9 | if constructor_ob is not None: |
Guido van Rossum | 4706562 | 1997-04-09 17:44:11 +0000 | [diff] [blame] | 10 | constructor(constructor_ob) |
| 11 | |
| 12 | def constructor(object): |
| 13 | safe_constructors[object] = 1 |
| 14 | |
Guido van Rossum | 72be306 | 1997-05-20 18:03:22 +0000 | [diff] [blame] | 15 | # Example: provide pickling support for complex numbers. |
Guido van Rossum | 4706562 | 1997-04-09 17:44:11 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 72be306 | 1997-05-20 18:03:22 +0000 | [diff] [blame] | 17 | def pickle_complex(c): |
| 18 | return complex, (c.real, c.imag) |
| 19 | |
| 20 | pickle(type(1j), pickle_complex, complex) |
Guido van Rossum | 4706562 | 1997-04-09 17:44:11 +0000 | [diff] [blame] | 21 | |