root / tags / v1_0_2_Build_912 / extensions / extScripting / scripts / jython / Lib / copy_reg.py @ 11422
History | View | Annotate | Download (1000 Bytes)
1 | 5782 | jmvivo | """Helper to provide extensibility for pickle/cPickle.
|
---|---|---|---|
2 |
|
||
3 | This is only useful to add pickle support for extension types defined in
|
||
4 | C, not for instances of user-defined classes.
|
||
5 | """
|
||
6 | |||
7 | from types import ClassType as _ClassType |
||
8 | |||
9 | __all__ = ["pickle","constructor"] |
||
10 | |||
11 | dispatch_table = {} |
||
12 | safe_constructors = {} |
||
13 | |||
14 | def pickle(ob_type, pickle_function, constructor_ob=None): |
||
15 | if type(ob_type) is _ClassType: |
||
16 | raise TypeError("copy_reg is not intended for use with classes") |
||
17 | |||
18 | if not callable(pickle_function): |
||
19 | raise TypeError("reduction functions must be callable") |
||
20 | dispatch_table[ob_type] = pickle_function |
||
21 | |||
22 | if constructor_ob is not None: |
||
23 | constructor(constructor_ob) |
||
24 | |||
25 | def constructor(object): |
||
26 | if not callable(object): |
||
27 | raise TypeError("constructors must be callable") |
||
28 | safe_constructors[object] = 1 |
||
29 | |||
30 | # Example: provide pickling support for complex numbers.
|
||
31 | |||
32 | def pickle_complex(c): |
||
33 | return complex, (c.real, c.imag) |
||
34 | |||
35 | pickle(type(1j), pickle_complex, complex) |