From e4f1c760c7f20403143d8c820ffd9997ffe5186a Mon Sep 17 00:00:00 2001 From: Petr Heinz Date: Tue, 10 Dec 2024 15:16:19 +0100 Subject: [PATCH] Avoid omitting copied objects as circular dependencies --- logtail/frame.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/logtail/frame.py b/logtail/frame.py index 8d730b5..919ed5c 100644 --- a/logtail/frame.py +++ b/logtail/frame.py @@ -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 "" - 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