Skip to content

Commit

Permalink
reencode non-ascii python2 assertion reprs, fixes #877
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RonnyPfannschmidt committed Sep 18, 2015
1 parent f02d942 commit 49c99a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@
2.7.3 (compared to 2.7.2)
-----------------------------

- fix issue 877: propperly handle assertion explanations with non-ascii repr
Thanks Mathieu Agopian for the report

- Allow 'dev', 'rc', or other non-integer version strings in `importorskip`.
Thanks to Eric Hunsberger for the PR.

Expand Down
9 changes: 8 additions & 1 deletion _pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,14 @@ def assertrepr_compare(config, op, left, right):
width = 80 - 15 - len(op) - 2 # 15 chars indentation, 1 space around op
left_repr = py.io.saferepr(left, maxsize=int(width/2))
right_repr = py.io.saferepr(right, maxsize=width-len(left_repr))
summary = u('%s %s %s') % (left_repr, op, right_repr)

# the reencoding is needed for python2 repr
# with non-ascii characters (see isssue 877)
summary = u('%s %s %s') % (
u(left_repr, 'utf-8', 'replace'),
op,
u(right_repr, 'utf-8', 'replace'),
)

issequence = lambda x: (isinstance(x, (list, tuple, Sequence))
and not isinstance(x, basestring))
Expand Down
11 changes: 11 additions & 0 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ def test_unicode(self):
assert expl[1] == py.builtin._totext('- £€', 'utf-8')
assert expl[2] == py.builtin._totext('+ £', 'utf-8')

def test_nonascii_text(self):
"""
:issue: 877
non ascii python2 str caused a UnicodeDecodeError
"""
class A(str):
def __repr__(self):
return '\xff'
expl = callequal(A(), '1')
assert expl

This comment has been minimized.

Copy link
@blueyed

blueyed May 15, 2019

Contributor

Just a snarky remark from the future: comparing/asserting the actual result here would have helped me (since this is None when only comparing (real) bytes with (real) unicode here now). Just a friendly plea for more expressive tests.. :)


def test_mojibake(self):
# issue 429
left = 'e'
Expand Down

0 comments on commit 49c99a4

Please sign in to comment.