Skip to content

Commit 18bcc19

Browse files
committed
Generatlize Sequence, Set to all iterables in serializer
1 parent add8b6f commit 18bcc19

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

sentry_sdk/serializer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
import math
3-
from collections.abc import Mapping, Sequence, Set
3+
from collections.abc import Mapping, Iterable
44
from datetime import datetime
55

66
from sentry_sdk.utils import (
@@ -331,9 +331,7 @@ def _serialize_node_impl(
331331

332332
return rv_dict
333333

334-
elif not isinstance(obj, serializable_str_types) and isinstance(
335-
obj, (Set, Sequence)
336-
):
334+
elif not isinstance(obj, serializable_str_types) and isinstance(obj, Iterable):
337335
rv_list = []
338336

339337
for i, v in enumerate(obj):

tests/integrations/django/test_basic.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
from werkzeug.test import Client
1111

1212
from django import VERSION as DJANGO_VERSION
13+
1314
from django.contrib.auth.models import User
1415
from django.core.management import execute_from_command_line
1516
from django.db.utils import OperationalError, ProgrammingError, DataError
1617
from django.http.request import RawPostDataException
1718
from django.utils.functional import SimpleLazyObject
19+
from django.template.context import make_context
1820

1921
try:
2022
from django.urls import reverse
@@ -310,6 +312,27 @@ def test_queryset_repr(sentry_init, capture_events):
310312
)
311313

312314

315+
@pytest.mark.forked
316+
@pytest_mark_django_db_decorator()
317+
def test_queryset_nested_repr(sentry_init, capture_events):
318+
sentry_init(integrations=[DjangoIntegration()])
319+
events = capture_events()
320+
User.objects.create_user("john", "[email protected]", "johnpassword")
321+
322+
try:
323+
context = make_context({"entries": User.objects.all()}) # noqa
324+
1 / 0
325+
except Exception:
326+
capture_exception()
327+
328+
(event,) = events
329+
330+
(exception,) = event["exception"]["values"]
331+
assert exception["type"] == "ZeroDivisionError"
332+
(frame,) = exception["stacktrace"]["frames"]
333+
assert "<User: " not in frame["vars"]["context"]
334+
335+
313336
def test_custom_error_handler_request_context(sentry_init, client, capture_events):
314337
sentry_init(integrations=[DjangoIntegration()])
315338
events = capture_events()

tests/test_serializer.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ def test_serialize_sets(extra_normalizer):
9393
assert result == [1, 2, 3]
9494

9595

96+
def test_serialize_custom_iter_class(extra_normalizer):
97+
class MyList:
98+
def __init__(self, list_):
99+
self.list_ = list_
100+
101+
def __iter__(self):
102+
return iter(self.list_)
103+
104+
result = extra_normalizer(MyList([1, 2, 3]))
105+
assert result == [1, 2, 3]
106+
107+
96108
def test_serialize_custom_mapping(extra_normalizer):
97109
class CustomReprDict(dict):
98110
def __sentry_repr__(self):

0 commit comments

Comments
 (0)