From 0b626be1b503805c03e685993ac6d55d040c0a97 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Tue, 25 Jun 2019 12:40:07 +0100 Subject: [PATCH] use safe_str to serialize Exceptions Fixes #5478 --- changelog/5478.bugfix.rst | 1 + src/_pytest/_code/code.py | 5 +++-- testing/python/raises.py | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 changelog/5478.bugfix.rst diff --git a/changelog/5478.bugfix.rst b/changelog/5478.bugfix.rst new file mode 100644 index 00000000000..b9e80a88ee6 --- /dev/null +++ b/changelog/5478.bugfix.rst @@ -0,0 +1 @@ +Use safe_str to serialize Exceptions in pytest.raises diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 8c73ccc6adc..83403cd42f2 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -572,8 +572,9 @@ def match(self, regexp): raised. """ __tracebackhide__ = True - if not re.search(regexp, str(self.value)): - assert 0, "Pattern '{!s}' not found in '{!s}'".format(regexp, self.value) + value = safe_str(self.value) + if not re.search(regexp, value): + assert 0, "Pattern {!r} not found in {!r}".format(regexp, value) return True diff --git a/testing/python/raises.py b/testing/python/raises.py index cd463d74b07..db34c6624f5 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -278,3 +278,7 @@ def __class__(self): with pytest.raises(CrappyClass()): pass assert "via __class__" in excinfo.value.args[0] + + def test_u(self): + with pytest.raises(AssertionError, match=u"\u2603"): + assert False, u"\u2603"