Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
copy_reg.py
Go to the documentation of this file.
1 """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 
33  return complex, (c.real, c.imag)
34 
35 pickle(type(1j), pickle_complex, complex)
36 
37 # Support for picking new-style objects
38 
39 def _reconstructor(cls, base, state):
40  obj = base.__new__(cls, state)
41  base.__init__(obj, state)
42  return obj
43 _reconstructor.__safe_for_unpickling__ = 1
44 
45 _HEAPTYPE = 1<<9
46 
47 def _reduce(self):
48  for base in self.__class__.__mro__:
49  if hasattr(base, '__flags__') and not base.__flags__ & _HEAPTYPE:
50  break
51  else:
52  base = object # not really reachable
53  if base is object:
54  state = None
55  else:
56  if base is self.__class__:
57  raise TypeError, "can't pickle %s objects" % base.__name__
58  state = base(self)
59  args = (self.__class__, base, state)
60  try:
61  getstate = self.__getstate__
62  except AttributeError:
63  try:
64  dict = self.__dict__
65  except AttributeError:
66  dict = None
67  else:
68  dict = getstate()
69  if dict:
70  return _reconstructor, args, dict
71  else:
72  return _reconstructor, args