comparison pyink/trait.py @ 1351:1a4d15fe2c62

Fix the issue for name confliction between two used traits. - When a class use two or more traits, and the name of a method from one trait conflicts with an attribute from another one, composite will be fault for setting method proxys. - Solution is to skip attributes that is not callable when setting proxys.
author Thinker K.F. Li <thinker@codemud.net>
date Sun, 13 Feb 2011 10:04:33 +0800
parents b0e54ae756f8
children 28769a82a72d
comparison
equal deleted inserted replaced
1350:17fa5d78200b 1351:1a4d15fe2c62
148 for attr in dir(a_trait): 148 for attr in dir(a_trait):
149 if attr.startswith('_'): 149 if attr.startswith('_'):
150 continue 150 continue
151 151
152 value = getattr(a_trait, attr) 152 value = getattr(a_trait, attr)
153 if isinstance(value, require): 153 if isinstance(value, require) or not callable(value):
154 continue 154 continue
155 155
156 attrname_cnts[attr] = attrname_cnts.setdefault(attr, 0) + 1 156 attrname_cnts[attr] = attrname_cnts.setdefault(attr, 0) + 1
157 pass 157 pass
158 pass 158 pass
185 continue 185 continue
186 186
187 value = getattr(derived, attr) 187 value = getattr(derived, attr)
188 188
189 if not callable(value): 189 if not callable(value):
190 raise TypeError, \ 190 continue
191 '%s.%s is not a callable' % (repr(a_trait), attr)
192 191
193 func = value.im_func 192 func = value.im_func
194 proxy = trait_method_proxy(derived, func) 193 proxy = trait_method_proxy(derived, func)
195 setattr(clazz, attr, proxy) 194 setattr(clazz, attr, proxy)
196 pass 195 pass
311 310
312 if __name__ == '__main__': 311 if __name__ == '__main__':
313 @trait 312 @trait
314 class hello(object): 313 class hello(object):
315 msg = require 314 msg = require
315 bye_provide = require
316 316
317 def hello(self, name): 317 def hello(self, name):
318 return self.msg + ' hello ' + name 318 return self.msg + ' hello ' + name
319
320 def hello_provide(self):
321 return 'hello provide'
322
323 def require_bye(self):
324 return self.bye_provide()
319 pass 325 pass
320 326
321 @trait 327 @trait
322 class bye(object): 328 class bye(object):
323 msg = require 329 msg = require
330 hello_provide = require
324 331
325 def bye(self, name): 332 def bye(self, name):
326 return self.msg + ' bye ' + name 333 return self.msg + ' bye ' + name
334
335 def bye_provide(self):
336 return 'bye provide'
337
338 def require_hello(self):
339 return self.hello_provide()
327 pass 340 pass
328 341
329 @composite 342 @composite
330 class hello_bye(object): 343 class hello_bye(object):
331 use_traits = (hello, bye) 344 use_traits = (hello, bye)
337 pass 350 pass
338 351
339 o = hello_bye() 352 o = hello_bye()
340 assert o.hello1('Miky') == 'hello_bye_msg1 hello Miky' 353 assert o.hello1('Miky') == 'hello_bye_msg1 hello Miky'
341 assert o.bye('Miky') == 'hello_bye bye Miky' 354 assert o.bye('Miky') == 'hello_bye bye Miky'
355 assert o.require_bye() == 'bye provide'
356 assert o.require_hello() == 'hello provide'
342 print 'OK' 357 print 'OK'
343 pass 358 pass