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.
This commit is contained in:
Floris Bruynooghe 2014-08-18 20:07:38 +02:00
parent 9232b88df3
commit 424479cf0f
3 changed files with 23 additions and 1 deletions

View File

@ -3,6 +3,9 @@ NEXT
- fixed issue561: adapt autouse fixture example for python3. - fixed issue561: adapt autouse fixture example for python3.
- fixed issue453: assertion rewriting issue with __repr__ containing
"\n{", "\n}" and "\n~".
2.6.1 2.6.1
----------------------------------- -----------------------------------

View File

@ -326,7 +326,15 @@ def rewrite_asserts(mod):
AssertionRewriter().run(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 from _pytest.assertion.util import format_explanation as _format_explanation # noqa
def _should_repr_global_name(obj): def _should_repr_global_name(obj):

View File

@ -313,6 +313,17 @@ class TestAssertionRewrite:
assert "%test" == "test" assert "%test" == "test"
assert getmsg(f).startswith("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: class TestRewriteOnImport: