##// END OF EJS Templates
Sort on string representation if sorting fails
Jeroen Demeyer -
Show More
@@ -116,6 +116,21 b' class CUnicodeIO(StringIO):'
116 116 "Please use io.StringIO instead."),
117 117 DeprecationWarning, stacklevel=2)
118 118
119 def _sorted_for_pprint(items):
120 """
121 Sort the given items for pretty printing. Since some predictable
122 sorting is better than no sorting at all, we sort on the string
123 representation if normal sorting fails.
124 """
125 items = list(items)
126 try:
127 return sorted(items)
128 except Exception:
129 try:
130 return sorted(items, key=str)
131 except Exception:
132 return items
133
119 134 def pretty(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH):
120 135 """
121 136 Pretty print the object's representation.
@@ -568,13 +583,10 b' def _set_pprinter_factory(start, end, basetype):'
568 583 step = len(start)
569 584 p.begin_group(step, start)
570 585 # Like dictionary keys, we will try to sort the items if there aren't too many
571 items = obj
572 586 if not (p.max_seq_length and len(obj) >= p.max_seq_length):
573 try:
574 items = sorted(obj)
575 except Exception:
576 # Sometimes the items don't sort.
577 pass
587 items = _sorted_for_pprint(obj)
588 else:
589 items = obj
578 590 for idx, x in p._enumerate(items):
579 591 if idx:
580 592 p.text(',')
@@ -602,11 +614,7 b' def _dict_pprinter_factory(start, end, basetype=None):'
602 614 keys = obj.keys()
603 615 # if dict isn't large enough to be truncated, sort keys before displaying
604 616 if not (p.max_seq_length and len(obj) >= p.max_seq_length):
605 try:
606 keys = sorted(keys)
607 except Exception:
608 # Sometimes the keys don't sort.
609 pass
617 keys = _sorted_for_pprint(keys)
610 618 for idx, key in p._enumerate(keys):
611 619 if idx:
612 620 p.text(',')
General Comments 0
You need to be logged in to leave comments. Login now