From 0d65783dce057dbfb276d1d7867037a8467dd20f Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 22 Aug 2018 19:00:43 -0700 Subject: [PATCH] Fix unicode errors when changing to .format(...) --- src/_pytest/_code/_py2traceback.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/_pytest/_code/_py2traceback.py b/src/_pytest/_code/_py2traceback.py index 2dd100c33..cceed40ed 100644 --- a/src/_pytest/_code/_py2traceback.py +++ b/src/_pytest/_code/_py2traceback.py @@ -2,7 +2,7 @@ # CHANGES: # - some_str is replaced, trying to create unicode strings # -from __future__ import absolute_import, division, print_function +from __future__ import absolute_import, division, print_function, unicode_literals import types from six import text_type @@ -51,17 +51,17 @@ def format_exception_only(etype, value): pass else: filename = filename or "" - lines.append(' File "%s", line %d\n' % (filename, lineno)) + lines.append(' File "{}", line {}\n'.format(filename, lineno)) if badline is not None: if isinstance(badline, bytes): # python 2 only badline = badline.decode("utf-8", "replace") - lines.append(u" %s\n" % badline.strip()) + lines.append(" {}\n".format(badline.strip())) if offset is not None: caretspace = badline.rstrip("\n")[:offset].lstrip() # non-space whitespace (likes tabs) must be kept for alignment caretspace = ((c.isspace() and c or " ") for c in caretspace) # only three spaces to account for offset1 == pos 0 - lines.append(" %s^\n" % "".join(caretspace)) + lines.append(" {}^\n".format("".join(caretspace))) value = msg lines.append(_format_final_exc_line(stype, value)) @@ -72,9 +72,9 @@ def _format_final_exc_line(etype, value): """Return a list of a single line -- normal case for format_exception_only""" valuestr = _some_str(value) if value is None or not valuestr: - line = "%s\n" % etype + line = "{}\n".format(etype) else: - line = "%s: %s\n" % (etype, valuestr) + line = "{}: {}\n".format(etype, valuestr) return line @@ -83,7 +83,7 @@ def _some_str(value): return text_type(value) except Exception: try: - return str(value) + return bytes(value).decode("UTF-8", "replace") except Exception: pass - return "" % type(value).__name__ + return "".format(type(value).__name__)