10000 fix: safe_repr for cyrillic (#79) · etherscan-io/sentry-python@1d54822 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d54822

Browse files
authored
fix: safe_repr for cyrillic (getsentry#79)
* fix: safe_repr for cyrillic * fix: Coding hint for Py2 * fix: Fix another py2 issue * fix: remove unused import
1 parent c9042c0 commit 1d54822

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

sentry_sdk/utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,25 @@ def safe_repr(value):
260260
rv = repr(value)
261261
if isinstance(rv, bytes):
262262
rv = rv.decode("utf-8", "replace")
263+
264+
# At this point `rv` contains a bunch of literal escape codes, like
265+
# this (exaggerated example):
266+
#
267+
# u"\\x2f"
268+
#
269+
# But we want to show this string as:
270+
#
271+
# u"/"
263272
try:
264-
return rv.encode("utf-8").decode("unicode-escape")
273+
# unicode-escape does this job, but can only decode latin1. So we
274+
# attempt to encode in latin1.
275+
return rv.encode("latin1").decode("unicode-escape")
265276
except Exception:
277+
# Since usually strings aren't latin1 this can break. In those
278+
# cases we just give up.
266279
return rv
267280
except Exception:
281+
# If e.g. the call to `repr` already fails
268282
return u"<broken repr>"
269283

270284

tests/test_utils.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
# coding: utf-8
12
import sys
23
import os
34

4-
from hypothesis import given, assume
5+
from hypothesis import given
56
import hypothesis.strategies as st
67

78
from sentry_sdk.utils import safe_repr, exceptions_from_error_tuple
@@ -17,15 +18,8 @@ def test_safe_repr_never_broken_for_strings(x):
1718
assert u"broken repr" not in r
1819

1920

20-
@given(x=any_string)
21-
def test_safe_repr_never_leaves_escapes_in(x):
22-
if isinstance(x, bytes):
23-
assume(b"\\u" not in x and b"\\x" not in x)
24-
else:
25-
assume(u"\\u" not in x and u"\\x" not in x)
26-
r = safe_repr(x)
27-
assert isinstance(r, text_type)
28-
assert u"\\u" not in r and u"\\x" not in r
21+
def test_safe_repr_regressions():
22+
assert u"лошадь" in safe_repr(u"лошадь")
2923

3024

3125
def test_abs_path():

0 commit comments

Comments
 (0)
0