File tree Expand file tree Collapse file tree 2 files changed +19
-11
lines changed Expand file tree Collapse file tree 2 files changed +19
-11
lines changed Original file line number Diff line number Diff line change @@ -260,11 +260,25 @@ def safe_repr(value):
260
260
rv = repr (value )
261
261
if isinstance (rv , bytes ):
262
262
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"/"
263
272
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" )
265
276
except Exception :
277
+ # Since usually strings aren't latin1 this can break. In those
278
+ # cases we just give up.
266
279
return rv
267
280
except Exception :
281
+ # If e.g. the call to `repr` already fails
268
282
return u"<broken repr>"
269
283
270
284
Original file line number Diff line number Diff line change
1
+ # coding: utf-8
1
2
import sys
2
3
import os
3
4
4
- from hypothesis import given , assume
5
+ from hypothesis import given
5
6
import hypothesis .strategies as st
6
7
7
8
from sentry_sdk .utils import safe_repr , exceptions_from_error_tuple
@@ -17,15 +18,8 @@ def test_safe_repr_never_broken_for_strings(x):
17
18
assert u"broken repr" not in r
18
19
19
20
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"лошадь" )
29
23
30
24
31
25
def test_abs_path ():
You can’t perform that action at this time.
0 commit comments