|
46 | 46 | # * set - ['SET', elt1, elt2, elt3, ..., eltN] |
47 | 47 | # * dict - ['DICT', [key1, value1], [key2, value2], ..., [keyN, valueN]] |
48 | 48 | # * instance - ['INSTANCE', class name, [attr1, value1], [attr2, value2], ..., [attrN, valueN]] |
| 49 | +# * instance with __str__ defined - ['INSTANCE_PPRINT', class name, <__str__ value>] |
49 | 50 | # * class - ['CLASS', class name, [list of superclass names], [attr1, value1], [attr2, value2], ..., [attrN, valueN]] |
50 | 51 | # * function - ['FUNCTION', function name, parent frame ID (for nested functions)] |
51 | 52 | # * module - ['module', module name] |
@@ -246,10 +247,22 @@ def encode_class_or_instance(self, dat, new_obj): |
246 | 247 | # http://docs.python.org/release/3.1.5/c-api/capsule.html |
247 | 248 | class_name = get_name(type(dat)) |
248 | 249 |
|
249 | | - new_obj.extend(['INSTANCE', class_name]) |
250 | | - # don't traverse inside modules, or else risk EXPLODING the visualization |
251 | | - if class_name == 'module': |
252 | | - return |
| 250 | + if hasattr(dat, '__str__') and \ |
| 251 | + (not dat.__class__.__str__ is object.__str__): # make sure it's not the lame default __str__ |
| 252 | + # N.B.: when objects are being constructed, this call |
| 253 | + # might fail since not all fields have yet been populated |
| 254 | + try: |
| 255 | + pprint_str = str(dat) |
| 256 | + except: |
| 257 | + pprint_str = '<incomplete object>' |
| 258 | + |
| 259 | + new_obj.extend(['INSTANCE_PPRINT', class_name, pprint_str]) |
| 260 | + return # bail early |
| 261 | + else: |
| 262 | + new_obj.extend(['INSTANCE', class_name]) |
| 263 | + # don't traverse inside modules, or else risk EXPLODING the visualization |
| 264 | + if class_name == 'module': |
| 265 | + return |
253 | 266 | else: |
254 | 267 | superclass_names = [e.__name__ for e in dat.__bases__ if e is not object] |
255 | 268 | new_obj.extend(['CLASS', get_name(dat), superclass_names]) |
|
0 commit comments