Skip to content

Commit 49c99a4

Browse files
reencode non-ascii python2 assertion reprs, fixes #877
i decided against using a warning since the problem goes away with python3 the support code can be removed once we drop python2 in 10 years or so
1 parent f02d942 commit 49c99a4

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Diff for: CHANGELOG

+3
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@
193193
2.7.3 (compared to 2.7.2)
194194
-----------------------------
195195

196+
- fix issue 877: propperly handle assertion explanations with non-ascii repr
197+
Thanks Mathieu Agopian for the report
198+
196199
- Allow 'dev', 'rc', or other non-integer version strings in `importorskip`.
197200
Thanks to Eric Hunsberger for the PR.
198201

Diff for: _pytest/assertion/util.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,14 @@ def assertrepr_compare(config, op, left, right):
129129
width = 80 - 15 - len(op) - 2 # 15 chars indentation, 1 space around op
130130
left_repr = py.io.saferepr(left, maxsize=int(width/2))
131131
right_repr = py.io.saferepr(right, maxsize=width-len(left_repr))
132-
summary = u('%s %s %s') % (left_repr, op, right_repr)
132+
133+
# the reencoding is needed for python2 repr
134+
# with non-ascii characters (see isssue 877)
135+
summary = u('%s %s %s') % (
136+
u(left_repr, 'utf-8', 'replace'),
137+
op,
138+
u(right_repr, 'utf-8', 'replace'),
139+
)
133140

134141
issequence = lambda x: (isinstance(x, (list, tuple, Sequence))
135142
and not isinstance(x, basestring))

Diff for: testing/test_assertion.py

+11
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,17 @@ def test_unicode(self):
231231
assert expl[1] == py.builtin._totext('- £€', 'utf-8')
232232
assert expl[2] == py.builtin._totext('+ £', 'utf-8')
233233

234+
def test_nonascii_text(self):
235+
"""
236+
:issue: 877
237+
non ascii python2 str caused a UnicodeDecodeError
238+
"""
239+
class A(str):
240+
def __repr__(self):
241+
return '\xff'
242+
expl = callequal(A(), '1')
243+
assert expl
244+
234245
def test_mojibake(self):
235246
# issue 429
236247
left = 'e'

0 commit comments

Comments
 (0)