4 new commits in pytest: https://bitbucket.org/hpk42/pytest/commits/a8cc1d22509d/ Changeset: a8cc1d22509d User: flub Date: 2014-08-18 20:07:38 Summary: 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. Affected #: 3 files diff -r f0d4a70ab98a3f1e82f25531ed7e1c9979c12b13 -r a8cc1d22509d432d8b9aa137d28263e4924f0fa8 CHANGELOG --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,9 @@ - fixed issue561: adapt autouse fixture example for python3. +- fixed issue453: assertion rewriting issue with __repr__ containing + "\n{", "\n}" and "\n~". + 2.6.1 ----------------------------------- diff -r f0d4a70ab98a3f1e82f25531ed7e1c9979c12b13 -r a8cc1d22509d432d8b9aa137d28263e4924f0fa8 _pytest/assertion/rewrite.py --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -326,7 +326,15 @@ 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 -r f0d4a70ab98a3f1e82f25531ed7e1c9979c12b13 -r a8cc1d22509d432d8b9aa137d28263e4924f0fa8 testing/test_assertrewrite.py --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -313,6 +313,17 @@ 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: https://bitbucket.org/hpk42/pytest/commits/dc8df0698bce/ Changeset: dc8df0698bce User: flub Date: 2014-08-19 20:50:25 Summary: Explain why this is important Affected #: 1 file diff -r a8cc1d22509d432d8b9aa137d28263e4924f0fa8 -r dc8df0698bce865c33dbed2cf0351c7c963462c6 _pytest/assertion/rewrite.py --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -327,6 +327,13 @@ def _saferepr(obj): + """Get a safe repr of an object for assertion error messages + + The assertion formatting (util.format_explanation()) requires + newlines to be escaped since they are a special character for it. + But py.io.saferepr allows newlines, so we need to escape them + here. + """ repr = py.io.saferepr(obj) if py.builtin._istext(repr): t = py.builtin.text https://bitbucket.org/hpk42/pytest/commits/18dc9d3ba496/ Changeset: 18dc9d3ba496 User: flub Date: 2014-08-23 12:10:16 Summary: Improve the docstring further Affected #: 1 file diff -r dc8df0698bce865c33dbed2cf0351c7c963462c6 -r 18dc9d3ba496f75d636bf88fa8ca5ca175f179ce _pytest/assertion/rewrite.py --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -327,12 +327,15 @@ def _saferepr(obj): - """Get a safe repr of an object for assertion error messages + """Get a safe repr of an object for assertion error messages. The assertion formatting (util.format_explanation()) requires newlines to be escaped since they are a special character for it. - But py.io.saferepr allows newlines, so we need to escape them - here. + Normally assertion.util.format_explanation() does this but for a + custom repr it is possible to contain one of the special escape + sequences, especially '\n{' and '\n}' are likely to be present in + JSON reprs. + """ repr = py.io.saferepr(obj) if py.builtin._istext(repr): https://bitbucket.org/hpk42/pytest/commits/aed87f841a76/ Changeset: aed87f841a76 User: flub Date: 2014-08-27 22:00:24 Summary: Merged in flub/pytest (pull request #196) Affected #: 3 files diff -r 5c5f7ef69bcd8586823b839d0abe88d1d72c1110 -r aed87f841a762133588d09f17f2ceb050d726569 CHANGELOG --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,9 @@ - fixed issue561: adapt autouse fixture example for python3. +- fixed issue453: assertion rewriting issue with __repr__ containing + "\n{", "\n}" and "\n~". + - Fix example in monkeypatch documentation, thanks t-8ch. - Do not mark as universal wheel because Python 2.6 is different from diff -r 5c5f7ef69bcd8586823b839d0abe88d1d72c1110 -r aed87f841a762133588d09f17f2ceb050d726569 _pytest/assertion/rewrite.py --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -326,7 +326,25 @@ AssertionRewriter().run(mod) -_saferepr = py.io.saferepr +def _saferepr(obj): + """Get a safe repr of an object for assertion error messages. + + The assertion formatting (util.format_explanation()) requires + newlines to be escaped since they are a special character for it. + Normally assertion.util.format_explanation() does this but for a + custom repr it is possible to contain one of the special escape + sequences, especially '\n{' and '\n}' are likely to be present in + JSON reprs. + + """ + 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 -r 5c5f7ef69bcd8586823b839d0abe88d1d72c1110 -r aed87f841a762133588d09f17f2ceb050d726569 testing/test_assertrewrite.py --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -313,6 +313,17 @@ 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: Repository URL: https://bitbucket.org/hpk42/pytest/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email. _______________________________________________ pytest-commit mailing list pytest-commit@python.org https://mail.python.org/mailman/listinfo/pytest-commit