Skip to content

Commit

Permalink
More robust inspector handling of objects for which vars() fails (#7)
Browse files Browse the repository at this point in the history
Objects without `__dict__`, like ones using `__slots__`, caused an exception message to be printed.
  • Loading branch information
Tronic committed May 21, 2023
1 parent c96c21f commit 1e514e5
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion tracerite/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def extract_variables(variables, sourcecode):
# Try to print members of objects that don't have proper __str__
elif no_str_conv.fullmatch(strvalue):
found = False
for n, v in vars(value).items():
for n, v in safe_vars(value).items():
mname = f'{name}.{n}'
if sourcecode and mname not in identifiers:
continue
Expand Down Expand Up @@ -73,6 +73,17 @@ def extract_variables(variables, sourcecode):
return rows


def safe_vars(obj):
"""Like vars(), but also supports objects with slots."""
ret = {}
for attr in dir(obj):
try:
ret[attr] = object.__getattribute__(obj, attr)
except AttributeError:
pass # Slots that haven't been set
return ret


def prettyvalue(val):
if isinstance(val, (list, tuple)):
if not 0 < len(val) <= 10:
Expand Down

0 comments on commit 1e514e5

Please sign in to comment.