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