inspect: Sort dictionary keys when possible

This should help for reproducible builds and for finding items in large
dictionaries.
This commit is contained in:
Dmitry Shachnev 2017-10-22 21:51:47 +03:00
parent 92cdbb99f2
commit 49e486ff47

View File

@ -203,6 +203,14 @@ def safe_getmembers(object, predicate=None, attr_getter=safe_getattr):
def object_description(object):
# type: (Any) -> unicode
"""A repr() implementation that returns text safe to use in reST context."""
if isinstance(object, dict):
try:
sorted_keys = sorted(object)
except TypeError:
pass # Cannot sort dict keys, fall back to generic repr
else:
items = ("%r: %r" % (key, object[key]) for key in sorted_keys)
return "{%s}" % ", ".join(items)
try:
s = repr(object)
except Exception: