handle objects where creating string representation causes error
This commit is contained in:
15
inspector.py
15
inspector.py
@ -116,6 +116,11 @@ def sorted_naturally(iterable, reverse=False):
|
|||||||
return sorted(iterable, key=natural, reverse=reverse)
|
return sorted(iterable, key=natural, reverse=reverse)
|
||||||
|
|
||||||
def is_good_entry(k, v):
|
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
|
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)
|
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 ""
|
return ""
|
||||||
|
|
||||||
def format_value(obj): #TODO: make magic numbers configurable
|
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
|
# try if separate lines can be used to shorten
|
||||||
splitted = res.split("\n")
|
splitted = res.split("\n")
|
||||||
if len(splitted) > 4:
|
if len(splitted) > 4:
|
||||||
|
Reference in New Issue
Block a user