From 424479cf0f8bb84b97596347a120307bead40916 Mon Sep 17 00:00:00 2001 From: Floris Bruynooghe Date: Mon, 18 Aug 2014 20:07:38 +0200 Subject: [PATCH] Escape newlines in repr for assertion rewriting The assertion formatting mini-language depends on newlines being escaped. Unfortunately if the repr of an object contained newlines the rewriting module did not escape those, which is now fixed. Fixes issue453. --- CHANGELOG | 3 +++ _pytest/assertion/rewrite.py | 10 +++++++++- testing/test_assertrewrite.py | 11 +++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 9ff564f85..93b34747e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,9 @@ NEXT - fixed issue561: adapt autouse fixture example for python3. +- fixed issue453: assertion rewriting issue with __repr__ containing + "\n{", "\n}" and "\n~". + 2.6.1 ----------------------------------- diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py index 523d2b2dc..772f3ac03 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -326,7 +326,15 @@ def rewrite_asserts(mod): AssertionRewriter().run(mod) -_saferepr = py.io.saferepr +def _saferepr(obj): + repr = py.io.saferepr(obj) + if py.builtin._istext(repr): + t = py.builtin.text + else: + t = py.builtin.bytes + return repr.replace(t("\n"), t("\\n")) + + from _pytest.assertion.util import format_explanation as _format_explanation # noqa def _should_repr_global_name(obj): diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 72e2d4e10..7e0cfbf83 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -313,6 +313,17 @@ class TestAssertionRewrite: assert "%test" == "test" assert getmsg(f).startswith("assert '%test' == 'test'") + def test_custom_repr(self): + def f(): + class Foo(object): + a = 1 + + def __repr__(self): + return "\n{ \n~ \n}" + f = Foo() + assert 0 == f.a + assert r"where 1 = \n{ \n~ \n}.a" in util._format_lines([getmsg(f)])[0] + class TestRewriteOnImport: