##// END OF EJS Templates
can classes
MinRK -
Show More
@@ -18,7 +18,7 b' __docformat__ = "restructuredtext en"'
18 18 import copy
19 19 import logging
20 20 import sys
21 from types import FunctionType
21 from types import FunctionType, ClassType
22 22
23 23 try:
24 24 import cPickle as pickle
@@ -110,6 +110,30 b' class CannedFunction(CannedObject):'
110 110 newFunc = FunctionType(self.code, g, self.__name__, defaults)
111 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 138 class CannedArray(CannedObject):
115 139 def __init__(self, obj):
@@ -200,6 +224,12 b' def can(obj):'
200 224
201 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 233 def can_dict(obj):
204 234 """can the *values* of a dict"""
205 235 if isinstance(obj, dict):
@@ -266,6 +296,8 b' can_map = {'
266 296 FunctionType : CannedFunction,
267 297 bytes : CannedBytes,
268 298 buffer : CannedBuffer,
299 type : can_class,
300 ClassType : can_class,
269 301 }
270 302
271 303 uncan_map = {
General Comments 0
You need to be logged in to leave comments. Login now