annotate Lib/copyreg.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 8e57b5d8f58f
children 992ef855b3ed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46867
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1 """Helper to provide extensibility for pickle.
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3 This is only useful to add pickle support for extension types defined in
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4 C, not for instances of user-defined classes.
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5 """
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7 __all__ = ["pickle", "constructor",
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
8 "add_extension", "remove_extension", "clear_extension_cache"]
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
9
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
10 dispatch_table = {}
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
11
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
12 def pickle(ob_type, pickle_function, constructor_ob=None):
73165
8e57b5d8f58f Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 46867
diff changeset
13 if not callable(pickle_function):
46867
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
14 raise TypeError("reduction functions must be callable")
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
15 dispatch_table[ob_type] = pickle_function
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
16
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
17 # The constructor_ob function is a vestige of safe for unpickling.
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
18 # There is no reason for the caller to pass it anymore.
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
19 if constructor_ob is not None:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
20 constructor(constructor_ob)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
21
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
22 def constructor(object):
73165
8e57b5d8f58f Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 46867
diff changeset
23 if not callable(object):
46867
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
24 raise TypeError("constructors must be callable")
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
25
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
26 # Example: provide pickling support for complex numbers.
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
27
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
28 try:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
29 complex
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
30 except NameError:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
31 pass
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
32 else:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
33
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
34 def pickle_complex(c):
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
35 return complex, (c.real, c.imag)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
36
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
37 pickle(complex, pickle_complex, complex)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
38
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
39 # Support for pickling new-style objects
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
40
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
41 def _reconstructor(cls, base, state):
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
42 if base is object:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
43 obj = object.__new__(cls)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
44 else:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
45 obj = base.__new__(cls, state)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
46 if base.__init__ != object.__init__:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
47 base.__init__(obj, state)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
48 return obj
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
49
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
50 _HEAPTYPE = 1<<9
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
51
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
52 # Python code for object.__reduce_ex__ for protocols 0 and 1
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
53
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
54 def _reduce_ex(self, proto):
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
55 assert proto < 2
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
56 for base in self.__class__.__mro__:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
57 if hasattr(base, '__flags__') and not base.__flags__ & _HEAPTYPE:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
58 break
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
59 else:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
60 base = object # not really reachable
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
61 if base is object:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
62 state = None
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
63 else:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
64 if base is self.__class__:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
65 raise TypeError("can't pickle %s objects" % base.__name__)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
66 state = base(self)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
67 args = (self.__class__, base, state)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
68 try:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
69 getstate = self.__getstate__
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
70 except AttributeError:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
71 if getattr(self, "__slots__", None):
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
72 raise TypeError("a class that defines __slots__ without "
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
73 "defining __getstate__ cannot be pickled")
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
74 try:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
75 dict = self.__dict__
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
76 except AttributeError:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
77 dict = None
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
78 else:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
79 dict = getstate()
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
80 if dict:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
81 return _reconstructor, args, dict
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
82 else:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
83 return _reconstructor, args
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
84
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
85 # Helper for __reduce_ex__ protocol 2
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
86
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
87 def __newobj__(cls, *args):
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
88 return cls.__new__(cls, *args)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
89
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
90 def _slotnames(cls):
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
91 """Return a list of slot names for a given class.
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
92
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
93 This needs to find slots defined by the class and its bases, so we
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
94 can't simply return the __slots__ attribute. We must walk down
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
95 the Method Resolution Order and concatenate the __slots__ of each
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
96 class found there. (This assumes classes don't modify their
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
97 __slots__ attribute to misrepresent their slots after the class is
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
98 defined.)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
99 """
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
100
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
101 # Get the value from a cache in the class if possible
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
102 names = cls.__dict__.get("__slotnames__")
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
103 if names is not None:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
104 return names
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
105
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
106 # Not cached -- calculate the value
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
107 names = []
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
108 if not hasattr(cls, "__slots__"):
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
109 # This class has no slots
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
110 pass
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
111 else:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
112 # Slots found -- gather slot names from all base classes
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
113 for c in cls.__mro__:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
114 if "__slots__" in c.__dict__:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
115 slots = c.__dict__['__slots__']
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
116 # if class has a single slot, it can be given as a string
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
117 if isinstance(slots, str):
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
118 slots = (slots,)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
119 for name in slots:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
120 # special descriptors
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
121 if name in ("__dict__", "__weakref__"):
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
122 continue
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
123 # mangled names
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
124 elif name.startswith('__') and not name.endswith('__'):
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
125 names.append('_%s%s' % (c.__name__, name))
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
126 else:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
127 names.append(name)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
128
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
129 # Cache the outcome in the class if at all possible
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
130 try:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
131 cls.__slotnames__ = names
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
132 except:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
133 pass # But don't die if we can't
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
134
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
135 return names
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
136
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
137 # A registry of extension codes. This is an ad-hoc compression
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
138 # mechanism. Whenever a global reference to <module>, <name> is about
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
139 # to be pickled, the (<module>, <name>) tuple is looked up here to see
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
140 # if it is a registered extension code for it. Extension codes are
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
141 # universal, so that the meaning of a pickle does not depend on
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
142 # context. (There are also some codes reserved for local use that
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
143 # don't have this restriction.) Codes are positive ints; 0 is
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
144 # reserved.
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
145
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
146 _extension_registry = {} # key -> code
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
147 _inverted_registry = {} # code -> key
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
148 _extension_cache = {} # code -> object
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
149 # Don't ever rebind those names: pickling grabs a reference to them when
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
150 # it's initialized, and won't see a rebinding.
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
151
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
152 def add_extension(module, name, code):
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
153 """Register an extension code."""
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
154 code = int(code)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
155 if not 1 <= code <= 0x7fffffff:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
156 raise ValueError("code out of range")
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
157 key = (module, name)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
158 if (_extension_registry.get(key) == code and
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
159 _inverted_registry.get(code) == key):
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
160 return # Redundant registrations are benign
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
161 if key in _extension_registry:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
162 raise ValueError("key %s is already registered with code %s" %
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
163 (key, _extension_registry[key]))
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
164 if code in _inverted_registry:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
165 raise ValueError("code %s is already in use for key %s" %
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
166 (code, _inverted_registry[code]))
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
167 _extension_registry[key] = code
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
168 _inverted_registry[code] = key
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
169
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
170 def remove_extension(module, name, code):
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
171 """Unregister an extension code. For testing only."""
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
172 key = (module, name)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
173 if (_extension_registry.get(key) != code or
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
174 _inverted_registry.get(code) != key):
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
175 raise ValueError("key %s is not registered with code %s" %
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
176 (key, code))
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
177 del _extension_registry[key]
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
178 del _inverted_registry[code]
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
179 if code in _extension_cache:
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
180 del _extension_cache[code]
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
181
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
182 def clear_extension_cache():
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
183 _extension_cache.clear()
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
184
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
185 # Standard extension code assignments
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
186
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
187 # Reserved ranges
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
188
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
189 # First Last Count Purpose
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
190 # 1 127 127 Reserved for Python standard library
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
191 # 128 191 64 Reserved for Zope
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
192 # 192 239 48 Reserved for 3rd parties
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
193 # 240 255 16 Reserved for private use (will never be assigned)
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
194 # 256 Inf Inf Reserved for future assignment
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
195
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
196 # Extension codes are assigned by the Python Software Foundation.