Skip to content

Commit

Permalink
Avoid omitting copied objects as circular dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrHeinz committed Dec 10, 2024
1 parent bbe66be commit e4f1c76
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions logtail/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,28 @@ 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)):
if isinstance(obj, (str, int, float, bool, type(None))):
return obj
if id(obj) in memo:

# For mutable objects, check for circular references
obj_id = id(obj)
if obj_id in memo:
return "<omitted circular reference>"
memo.add(id(obj))
memo.add(obj_id)

if isinstance(obj, dict):
new_dict = {}
for key, value in obj.items():
new_dict[key] = _remove_circular_dependencies(value, memo)
new_dict[key] = _remove_circular_dependencies(value, memo.copy())
return new_dict
elif isinstance(obj, list):
return [_remove_circular_dependencies(item, memo) for item in obj]
return [_remove_circular_dependencies(item, memo.copy()) for item in obj]
elif isinstance(obj, tuple):
return tuple(_remove_circular_dependencies(item, memo) for item in obj)
return tuple(_remove_circular_dependencies(item, memo.copy()) for item in obj)
elif isinstance(obj, set):
return {_remove_circular_dependencies(item, memo) for item in obj}
return {_remove_circular_dependencies(item, memo.copy()) for item in obj}
else:
return obj

Expand Down

0 comments on commit e4f1c76

Please sign in to comment.