Skip to content

Commit

Permalink
fix formatting utf-8 error explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
biern committed Feb 12, 2016
1 parent b1955c7 commit b44618b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
* Fix (`#1290`_): support Python 3.5's `@` operator in assertion rewriting.
Thanks `@Shinkenjoe`_ for report with test case and `@tomviner`_ for the PR.

* Fix formatting utf-8 explanation messages (`#1379`_).

.. _#1379: https://github.com/pytest-dev/pytest/issues/1379
.. _#1366: https://github.com/pytest-dev/pytest/issues/1366
.. _#1040: https://github.com/pytest-dev/pytest/pull/1040
.. _#680: https://github.com/pytest-dev/pytest/issues/680
Expand Down
18 changes: 10 additions & 8 deletions _pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
_reprcompare = None


# the re-encoding is needed for python2 repr
# with non-ascii characters (see issue 877 and 1379)
def ecu(s):
try:
return u(s, 'utf-8', 'replace')
except TypeError:
return s


def format_explanation(explanation):
"""This formats an explanation
Expand All @@ -28,6 +37,7 @@ def format_explanation(explanation):
for when one explanation needs to span multiple lines, e.g. when
displaying diffs.
"""
explanation = ecu(explanation)
explanation = _collapse_false(explanation)
lines = _split_explanation(explanation)
result = _format_lines(lines)
Expand Down Expand Up @@ -131,14 +141,6 @@ def assertrepr_compare(config, op, left, right):
left_repr = py.io.saferepr(left, maxsize=int(width/2))
right_repr = py.io.saferepr(right, maxsize=width-len(left_repr))

# the re-encoding is needed for python2 repr
# with non-ascii characters (see issue 877)
def ecu(s):
try:
return u(s, 'utf-8', 'replace')
except TypeError:
return s

summary = u('%s %s %s') % (ecu(left_repr), op, ecu(right_repr))

issequence = lambda x: (isinstance(x, (list, tuple, Sequence)) and
Expand Down
3 changes: 3 additions & 0 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ def __repr__(self):
expl = callequal(A(), '1')
assert expl

def test_format_nonascii_explanation(self):
assert util.format_explanation('λ')

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

0 comments on commit b44618b

Please sign in to comment.