Skip to content
Draft
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions sentry_sdk/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
MAX_DATABAG_DEPTH: int = 5
MAX_DATABAG_BREADTH: int = 10
CYCLE_MARKER = "<cyclic>"
_ATOMIC_TYPES = frozenset((str, int, float, bool, type(None)))


global_repr_processors: "List[ReprProcessor]" = []
Expand Down Expand Up @@ -199,6 +200,16 @@ def _serialize_node(
self.path.append(segment)

try:
if type(obj) in _ATOMIC_TYPES:
return self._serialize_node_impl(
obj,
is_databag=is_databag,
is_request_body=is_request_body,
should_repr_strings=should_repr_strings,
remaining_depth=remaining_depth,
remaining_breadth=remaining_breadth,
)

with self.memo.memoize(obj) as result:
if result:
return CYCLE_MARKER
Expand Down
45 changes: 44 additions & 1 deletion tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

import pytest

from sentry_sdk.serializer import MAX_DATABAG_BREADTH, MAX_DATABAG_DEPTH, serialize
from sentry_sdk.serializer import (
MAX_DATABAG_BREADTH,
MAX_DATABAG_DEPTH,
Memo,
serialize,
)

try:
import hypothesis.strategies as st
Expand Down Expand Up @@ -115,6 +120,44 @@ def test_custom_mapping_doesnt_mess_with_mock(extra_normalizer):
assert len(m.mock_calls) == 0


def test_atomic_values_serialization_output():
result = serialize(
{
"nested": {
"values": [
"string",
42,
3.14,
True,
False,
None,
float("inf"),
float("nan"),
]
}
}
)

assert result == {
"nested": {"values": ["string", 42, 3.14, True, False, None, "inf", "nan"]}
}


def test_atomic_values_are_not_memoized(monkeypatch):
memoized_objects = []
original_memoize = Memo.memoize

def counting_memoize(self, obj):
memoized_objects.append(obj)
return original_memoize(self, obj)

monkeypatch.setattr(Memo, "memoize", counting_memoize)
event = {"one": "1", "two": "2", "three": "3"}

assert serialize(event) == event
assert memoized_objects == [event]


def test_custom_repr(extra_normalizer):
class Foo:
pass
Expand Down