Skip to content

Commit

Permalink
Omit circular dependencies in sets and tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrHeinz committed Sep 16, 2024
1 parent d6e4619 commit f3a95fd
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions logtail/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,23 @@ def _parse_custom_events(record, include_extra_attributes):
def _remove_circular_dependencies(obj, memo=None):
if memo is None:
memo = set()
# Skip immutable types, which can't contain circular dependencies
if isinstance(obj, (str, int, float, bool)):
return obj
if id(obj) in memo:
return "<omitted circular reference>"
memo.add(id(obj))
if isinstance(obj, dict):
memo.add(id(obj))
new_dict = {}
for key, value in obj.items():
new_dict[key] = _remove_circular_dependencies(value, memo)
return new_dict
elif isinstance(obj, list):
memo.add(id(obj))
new_list = [_remove_circular_dependencies(item, memo) for item in obj]
return new_list
return [_remove_circular_dependencies(item, memo) for item in obj]
elif isinstance(obj, tuple):
return tuple(_remove_circular_dependencies(item, memo) for item in obj)
elif isinstance(obj, set):
return {_remove_circular_dependencies(item, memo) for item in obj}
else:
return obj

Expand Down

0 comments on commit f3a95fd

Please sign in to comment.