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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ pip-log.txt
venv
.vscode/tags
.pytest_cache
.hypothesis
5 changes: 0 additions & 5 deletions dev-requirements.txt

This file was deleted.

33 changes: 12 additions & 21 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,7 @@ def skip_internal_frames(frame):

def safe_str(value):
try:
try:
rv = text_type(value)
except UnicodeError:
rv = bytes(value)
if isinstance(rv, bytes):
rv = rv.decode('utf-8', 'replace')
return rv
return text_type(value)
except Exception:
return safe_repr(value)

Expand All @@ -236,25 +230,22 @@ def safe_repr(value):
rv = repr(value)
if isinstance(rv, bytes):
rv = rv.decode('utf-8', 'replace')
return rv
try:
return rv.encode('utf-8').decode('unicode-escape')
except Exception:
return rv
except Exception:
return '<broken repr>'
return u'<broken repr>'


def object_to_json(obj):
def _walk(obj, depth):
if depth >= 4:
return safe_repr(obj)
if obj is None or obj is True or obj is False or \
isinstance(obj, number_types) or isinstance(obj, text_type):
return obj
if isinstance(obj, bytes):
return obj.decode('utf-8', 'replace')
if isinstance(obj, Sequence):
return [_walk(x, depth + 1) for x in obj]
if isinstance(obj, Mapping):
return {safe_repr(k): _walk(v, depth + 1) for k, v in
obj.items()}
if depth < 4:
if isinstance(obj, Sequence):
return [_walk(x, depth + 1) for x in obj]
if isinstance(obj, Mapping):
return {safe_repr(k): _walk(v, depth + 1) for k, v in
obj.items()}
return safe_repr(obj)
return _walk(obj, 0)

Expand Down
25 changes: 25 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from hypothesis import given, assume, settings
import hypothesis.strategies as st

from sentry_sdk.utils import safe_repr
from sentry_sdk._compat import PY2, text_type

any_string = st.one_of(st.binary(), st.text())

@given(x=any_string)
@settings(max_examples=1000)
def test_safe_repr_never_broken_for_strings(x):
r = safe_repr(x)
assert isinstance(r, text_type)
assert u'broken repr' not in r

@given(x=any_string)
@settings(max_examples=1000)
def test_safe_repr_never_leaves_escapes_in(x):
if isinstance(x, bytes):
assume(b'\\u' not in x and b'\\x' not in x)
else:
assume(u'\\u' not in x and u'\\x' not in x)
r = safe_repr(x)
assert isinstance(r, text_type)
assert u'\\u' not in r and u'\\x' not in r
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ envlist =
[testenv]
deps =
pytest
hypothesis
django-{16,17,18}: pytest-django<3.0
django-{19,110,110,111,200,dev}: pytest-django>=3.0,<3.3
django-{18,19,110}: django-tastypie==0.14
Expand Down