fix formatting utf-8 error explanation

This commit is contained in:
Marcin Biernat 2016-02-12 15:54:36 +01:00
parent b1955c7f84
commit 1b431d6644
3 changed files with 18 additions and 8 deletions

View File

@ -62,12 +62,17 @@
* Fix (`#1290`_): support Python 3.5's `@` operator in assertion rewriting. * Fix (`#1290`_): support Python 3.5's `@` operator in assertion rewriting.
Thanks `@Shinkenjoe`_ for report with test case and `@tomviner`_ for the PR. Thanks `@Shinkenjoe`_ for report with test case and `@tomviner`_ for the PR.
* Fix formatting utf-8 explanation messages (`#1379`_).
Thanks `@biern`_ for the PR.
.. _#1379: https://github.com/pytest-dev/pytest/issues/1379
.. _#1366: https://github.com/pytest-dev/pytest/issues/1366 .. _#1366: https://github.com/pytest-dev/pytest/issues/1366
.. _#1040: https://github.com/pytest-dev/pytest/pull/1040 .. _#1040: https://github.com/pytest-dev/pytest/pull/1040
.. _#680: https://github.com/pytest-dev/pytest/issues/680 .. _#680: https://github.com/pytest-dev/pytest/issues/680
.. _#1287: https://github.com/pytest-dev/pytest/pull/1287 .. _#1287: https://github.com/pytest-dev/pytest/pull/1287
.. _#1226: https://github.com/pytest-dev/pytest/pull/1226 .. _#1226: https://github.com/pytest-dev/pytest/pull/1226
.. _#1290: https://github.com/pytest-dev/pytest/pull/1290 .. _#1290: https://github.com/pytest-dev/pytest/pull/1290
.. _@biern: https://github.com/biern
.. _@MichaelAquilina: https://github.com/MichaelAquilina .. _@MichaelAquilina: https://github.com/MichaelAquilina
.. _@bukzor: https://github.com/bukzor .. _@bukzor: https://github.com/bukzor
.. _@hpk42: https://github.com/hpk42 .. _@hpk42: https://github.com/hpk42

View File

@ -18,6 +18,15 @@ u = py.builtin._totext
_reprcompare = None _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): def format_explanation(explanation):
"""This formats an explanation """This formats an explanation
@ -28,6 +37,7 @@ def format_explanation(explanation):
for when one explanation needs to span multiple lines, e.g. when for when one explanation needs to span multiple lines, e.g. when
displaying diffs. displaying diffs.
""" """
explanation = ecu(explanation)
explanation = _collapse_false(explanation) explanation = _collapse_false(explanation)
lines = _split_explanation(explanation) lines = _split_explanation(explanation)
result = _format_lines(lines) result = _format_lines(lines)
@ -131,14 +141,6 @@ def assertrepr_compare(config, op, left, right):
left_repr = py.io.saferepr(left, maxsize=int(width/2)) left_repr = py.io.saferepr(left, maxsize=int(width/2))
right_repr = py.io.saferepr(right, maxsize=width-len(left_repr)) 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)) summary = u('%s %s %s') % (ecu(left_repr), op, ecu(right_repr))
issequence = lambda x: (isinstance(x, (list, tuple, Sequence)) and issequence = lambda x: (isinstance(x, (list, tuple, Sequence)) and

View File

@ -242,6 +242,9 @@ class TestAssert_reprcompare:
expl = callequal(A(), '1') expl = callequal(A(), '1')
assert expl assert expl
def test_format_nonascii_explanation(self):
assert util.format_explanation('λ')
def test_mojibake(self): def test_mojibake(self):
# issue 429 # issue 429
left = 'e' left = 'e'