From 813a6dc7d5d7b39b3e689c8ac7b0658f01d79c6b 2017-08-31 14:02:34 From: Jeroen Demeyer Date: 2017-08-31 14:02:34 Subject: [PATCH] Sort on string representation if sorting fails --- diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index 9fe465d..899ff02 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -116,6 +116,21 @@ class CUnicodeIO(StringIO): "Please use io.StringIO instead."), DeprecationWarning, stacklevel=2) +def _sorted_for_pprint(items): + """ + Sort the given items for pretty printing. Since some predictable + sorting is better than no sorting at all, we sort on the string + representation if normal sorting fails. + """ + items = list(items) + try: + return sorted(items) + except Exception: + try: + return sorted(items, key=str) + except Exception: + return items + def pretty(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH): """ Pretty print the object's representation. @@ -568,13 +583,10 @@ def _set_pprinter_factory(start, end, basetype): step = len(start) p.begin_group(step, start) # Like dictionary keys, we will try to sort the items if there aren't too many - items = obj if not (p.max_seq_length and len(obj) >= p.max_seq_length): - try: - items = sorted(obj) - except Exception: - # Sometimes the items don't sort. - pass + items = _sorted_for_pprint(obj) + else: + items = obj for idx, x in p._enumerate(items): if idx: p.text(',') @@ -602,11 +614,7 @@ def _dict_pprinter_factory(start, end, basetype=None): keys = obj.keys() # if dict isn't large enough to be truncated, sort keys before displaying if not (p.max_seq_length and len(obj) >= p.max_seq_length): - try: - keys = sorted(keys) - except Exception: - # Sometimes the keys don't sort. - pass + keys = _sorted_for_pprint(keys) for idx, key in p._enumerate(keys): if idx: p.text(',')