From 7658f6014627507a96b97eab9938214821999a29 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sun, 13 Sep 2015 21:34:52 +0200 Subject: [PATCH] #877 - reencoding causes typeerrors on python2 --- _pytest/assertion/util.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/_pytest/assertion/util.py b/_pytest/assertion/util.py index 6193b4a10..ca54f1692 100644 --- a/_pytest/assertion/util.py +++ b/_pytest/assertion/util.py @@ -130,13 +130,15 @@ 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 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'), - ) + # 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 not isinstance(x, basestring))