Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions sentry_sdk/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from array import array
from collections.abc import Mapping
from datetime import datetime
from itertools import islice
from typing import TYPE_CHECKING

from sentry_sdk.utils import (
Expand Down Expand Up @@ -299,16 +300,21 @@ def _serialize_node_impl(
)

elif isinstance(obj, Mapping):
# Create temporary copy here to avoid calling too much code that
# might mutate our dictionary while we're still iterating over it.
obj = dict(obj.items())
# Copy only as many pairs as we might keep - avoids O(len(obj)) work
# when obj is huge (e.g. a large cache captured as a frame local),
# while still protecting against mutation during iteration.
obj_len = len(obj)
if isinstance(remaining_breadth, int):
obj = dict(islice(obj.items(), remaining_breadth + 1))
else:
obj = dict(obj.items())

rv_dict: "Dict[str, Any]" = {}
i = 0

for k, v in obj.items():
if remaining_breadth is not None and i >= remaining_breadth:
self._annotate(len=len(obj))
self._annotate(len=obj_len)
break

str_k = str(k)
Expand Down
Loading