Skip to content
Merged
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
68 changes: 36 additions & 32 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,39 +349,43 @@ def safe_repr(value):


def object_to_json(obj, remaining_depth=4, memo=None):
if memo is None:
memo = Memo()
if memo.memoize(obj):
return CYCLE_MARKER
with capture_internal_exceptions():
if memo is None:
memo = Memo()
if memo.memoize(obj):
return CYCLE_MARKER

try:
if remaining_depth > 0:
hints = {"memo": memo, "remaining_depth": remaining_depth}
for processor in global_repr_processors:
with capture_internal_exceptions():
result = processor(obj, hints)
if result is not NotImplemented:
return result

if isinstance(obj, (list, tuple)):
# It is not safe to iterate over another sequence types as this may raise errors or
# bring undesired side-effects (e.g. Django querysets are executed during iteration)
return [
object_to_json(x, remaining_depth=remaining_depth - 1, memo=memo)
for x in obj
]

if isinstance(obj, Mapping):
return {
safe_str(k): object_to_json(
v, remaining_depth=remaining_depth - 1, memo=memo
)
for k, v in list(obj.items())
}

return safe_repr(obj)
finally:
memo.unmemoize(obj)
try:
if remaining_depth > 0:
hints = {"memo": memo, "remaining_depth": remaining_depth}
for processor in global_repr_processors:
with capture_internal_exceptions():
result = processor(obj, hints)
if result is not NotImplemented:
return result

if isinstance(obj, (list, tuple)):
# It is not safe to iterate over another sequence types as this may raise errors or
# bring undesired side-effects (e.g. Django querysets are executed during iteration)
return [
object_to_json(
x, remaining_depth=remaining_depth - 1, memo=memo
)
for x in obj
]

if isinstance(obj, Mapping):
return {
safe_str(k): object_to_json(
v, remaining_depth=remaining_depth - 1, memo=memo
)
for k, v in list(obj.items())
}

return safe_repr(obj)
finally:
memo.unmemoize(obj)
return u"<broken repr>"


def extract_locals(frame):
Expand Down
42 changes: 41 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@
)
from sentry_sdk.hub import HubMeta
from sentry_sdk.transport import Transport
from sentry_sdk._compat import reraise, text_type
from sentry_sdk._compat import reraise, text_type, PY2
from sentry_sdk.utils import HAS_CHAINED_EXCEPTIONS

if PY2:
# Importing ABCs from collections is deprecated, and will stop working in 3.8
# https://github.com/python/cpython/blob/master/Lib/collections/__init__.py#L49
from collections import Mapping
else:
# New in 3.3
# https://docs.python.org/3/library/collections.abc.html
from collections.abc import Mapping


class EventCaptured(Exception):
pass
Expand Down Expand Up @@ -404,3 +413,34 @@ def test_chained_exceptions(sentry_init, capture_events):
event, = events

assert len(event["exception"]["values"]) == 2


@pytest.mark.tests_internal_exceptions
def test_broken_mapping(sentry_init, capture_events):
sentry_init()
events = capture_events()

class C(Mapping):
def broken(self, *args, **kwargs):
raise Exception("broken")

__getitem__ = broken
__setitem__ = broken
__delitem__ = broken
__iter__ = broken
__len__ = broken

def __repr__(self):
return "broken"

try:
a = C() # noqa
1 / 0
except Exception:
capture_exception()

event, = events
assert (
event["exception"]["values"][0]["stacktrace"]["frames"][0]["vars"]["a"]
== "<broken repr>"
)