From fa944f19645915682aa5c2c5c87b54967f753528 Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Mon, 25 Oct 2021 12:48:22 +0200 Subject: [PATCH] handle objects where creating string representation causes error --- inspector.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/inspector.py b/inspector.py index 120fe23..620ec92 100644 --- a/inspector.py +++ b/inspector.py @@ -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: