Show More
@@ -18,7 +18,7 b' __docformat__ = "restructuredtext en"' | |||||
18 | import copy |
|
18 | import copy | |
19 | import logging |
|
19 | import logging | |
20 | import sys |
|
20 | import sys | |
21 | from types import FunctionType |
|
21 | from types import FunctionType, ClassType | |
22 |
|
22 | |||
23 | try: |
|
23 | try: | |
24 | import cPickle as pickle |
|
24 | import cPickle as pickle | |
@@ -110,6 +110,30 b' class CannedFunction(CannedObject):' | |||||
110 | newFunc = FunctionType(self.code, g, self.__name__, defaults) |
|
110 | newFunc = FunctionType(self.code, g, self.__name__, defaults) | |
111 | return newFunc |
|
111 | return newFunc | |
112 |
|
112 | |||
|
113 | class CannedClass(CannedObject): | |||
|
114 | ||||
|
115 | def __init__(self, cls): | |||
|
116 | self._check_type(cls) | |||
|
117 | self.name = cls.__name__ | |||
|
118 | self.old_style = not isinstance(cls, type) | |||
|
119 | self._canned_dict = {} | |||
|
120 | for k,v in cls.__dict__.items(): | |||
|
121 | if k not in ('__weakref__', '__dict__'): | |||
|
122 | self._canned_dict[k] = can(v) | |||
|
123 | if self.old_style: | |||
|
124 | mro = [] | |||
|
125 | else: | |||
|
126 | mro = cls.mro() | |||
|
127 | ||||
|
128 | self.parents = [ can(c) for c in mro[1:] ] | |||
|
129 | self.buffers = [] | |||
|
130 | ||||
|
131 | def _check_type(self, obj): | |||
|
132 | assert isinstance(obj, (type, ClassType)), "Not a class type" | |||
|
133 | ||||
|
134 | def get_object(self, g=None): | |||
|
135 | parents = tuple(uncan(p, g) for p in self.parents) | |||
|
136 | return type(self.name, parents, uncan_dict(self._canned_dict, g=g)) | |||
113 |
|
137 | |||
114 | class CannedArray(CannedObject): |
|
138 | class CannedArray(CannedObject): | |
115 | def __init__(self, obj): |
|
139 | def __init__(self, obj): | |
@@ -200,6 +224,12 b' def can(obj):' | |||||
200 |
|
224 | |||
201 | return obj |
|
225 | return obj | |
202 |
|
226 | |||
|
227 | def can_class(obj): | |||
|
228 | if isinstance(obj, (type, ClassType)) and obj.__module__ == '__main__': | |||
|
229 | return CannedClass(obj) | |||
|
230 | else: | |||
|
231 | return obj | |||
|
232 | ||||
203 | def can_dict(obj): |
|
233 | def can_dict(obj): | |
204 | """can the *values* of a dict""" |
|
234 | """can the *values* of a dict""" | |
205 | if isinstance(obj, dict): |
|
235 | if isinstance(obj, dict): | |
@@ -266,6 +296,8 b' can_map = {' | |||||
266 | FunctionType : CannedFunction, |
|
296 | FunctionType : CannedFunction, | |
267 | bytes : CannedBytes, |
|
297 | bytes : CannedBytes, | |
268 | buffer : CannedBuffer, |
|
298 | buffer : CannedBuffer, | |
|
299 | type : can_class, | |||
|
300 | ClassType : can_class, | |||
269 | } |
|
301 | } | |
270 |
|
302 | |||
271 | uncan_map = { |
|
303 | uncan_map = { |
General Comments 0
You need to be logged in to leave comments.
Login now