handle objects where creating string representation causes error

This commit is contained in:
2021-10-25 12:48:22 +02:00
parent 27fbde0a74
commit fa944f1964

View File

@ -116,6 +116,11 @@ def sorted_naturally(iterable, reverse=False):
return sorted(iterable, key=natural, reverse=reverse)
def is_good_entry(k, v):
# #TODO: completely ignore objects with errors here? or use error as value (see format_value below)
# try:
# str(v) # have to be able to create a string representation for the object
# except Exception:
# return False
ignore_types = (VariableInspector, types.ModuleType, type) # hide modules and classes
return not k.startswith("_") and k not in IGNORE_NAMES and not isinstance(v, ignore_types)
@ -138,7 +143,15 @@ def format_size(obj):
return ""
def format_value(obj): #TODO: make magic numbers configurable
res = str(obj)
# if string representation cannot be created, use error as value
#TODO: cf. is_good_entry for alternative way of dealing with this
try:
res = str(obj)
except Exception as e:
tn = typename(e)
res = f"caused a {tn}"
if str(e): # e has a message
res += f": {e}"
# try if separate lines can be used to shorten
splitted = res.split("\n")
if len(splitted) > 4: