##// END OF EJS Templates
Backport PR #10767: Sort on string representation if sorting fails
Matthias Bussonnier -
Show More
@@ -119,6 +119,21 b' else:'
119 cast_unicode(text, encoding=get_stream_enc(sys.stdout)))
119 cast_unicode(text, encoding=get_stream_enc(sys.stdout)))
120
120
121
121
122 def _sorted_for_pprint(items):
123 """
124 Sort the given items for pretty printing. Since some predictable
125 sorting is better than no sorting at all, we sort on the string
126 representation if normal sorting fails.
127 """
128 items = list(items)
129 try:
130 return sorted(items)
131 except Exception:
132 try:
133 return sorted(items, key=str)
134 except Exception:
135 return items
136
122 def pretty(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH):
137 def pretty(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH):
123 """
138 """
124 Pretty print the object's representation.
139 Pretty print the object's representation.
@@ -576,13 +591,10 b' def _set_pprinter_factory(start, end, basetype):'
576 step = len(start)
591 step = len(start)
577 p.begin_group(step, start)
592 p.begin_group(step, start)
578 # Like dictionary keys, we will try to sort the items if there aren't too many
593 # Like dictionary keys, we will try to sort the items if there aren't too many
579 items = obj
580 if not (p.max_seq_length and len(obj) >= p.max_seq_length):
594 if not (p.max_seq_length and len(obj) >= p.max_seq_length):
581 try:
595 items = _sorted_for_pprint(obj)
582 items = sorted(obj)
596 else:
583 except Exception:
597 items = obj
584 # Sometimes the items don't sort.
585 pass
586 for idx, x in p._enumerate(items):
598 for idx, x in p._enumerate(items):
587 if idx:
599 if idx:
588 p.text(',')
600 p.text(',')
@@ -610,11 +622,7 b' def _dict_pprinter_factory(start, end, basetype=None):'
610 keys = obj.keys()
622 keys = obj.keys()
611 # if dict isn't large enough to be truncated, sort keys before displaying
623 # if dict isn't large enough to be truncated, sort keys before displaying
612 if not (p.max_seq_length and len(obj) >= p.max_seq_length):
624 if not (p.max_seq_length and len(obj) >= p.max_seq_length):
613 try:
625 keys = _sorted_for_pprint(keys)
614 keys = sorted(keys)
615 except Exception:
616 # Sometimes the keys don't sort.
617 pass
618 for idx, key in p._enumerate(keys):
626 for idx, key in p._enumerate(keys):
619 if idx:
627 if idx:
620 p.text(',')
628 p.text(',')
General Comments 0
You need to be logged in to leave comments. Login now