From 6a52fe165070dcbeda9b7c5b622975effcab5f40 Mon Sep 17 00:00:00 2001 From: Omer Hadari Date: Sat, 4 Mar 2017 10:26:46 +0200 Subject: [PATCH 1/5] fixed internal error on unprintable raised AssertionErrors --- _pytest/_code/code.py | 6 ++++-- testing/test_assertion.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/_pytest/_code/code.py b/_pytest/_code/code.py index 616d5c431..6eceb0c7f 100644 --- a/_pytest/_code/code.py +++ b/_pytest/_code/code.py @@ -352,6 +352,8 @@ class ExceptionInfo(object): help for navigating the traceback. """ _striptext = '' + _assert_start_repr = "AssertionError(u\'assert " if sys.version_info[0] < 3 else "AssertionError(\'assert " + def __init__(self, tup=None, exprinfo=None): import _pytest._code if tup is None: @@ -359,8 +361,8 @@ class ExceptionInfo(object): if exprinfo is None and isinstance(tup[1], AssertionError): exprinfo = getattr(tup[1], 'msg', None) if exprinfo is None: - exprinfo = py._builtin._totext(tup[1]) - if exprinfo and exprinfo.startswith('assert '): + exprinfo = py.io.saferepr(tup[1]) + if exprinfo and exprinfo.startswith(self._assert_start_repr): self._striptext = 'AssertionError: ' self._excinfo = tup #: the exception class diff --git a/testing/test_assertion.py b/testing/test_assertion.py index dfc9f60fb..bc814590a 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -916,6 +916,25 @@ def test_assert_with_unicode(monkeypatch, testdir): result = testdir.runpytest() result.stdout.fnmatch_lines(['*AssertionError*']) +def test_raise_unprintable_assertion_error(testdir): + testdir.makepyfile(r""" + def test_raise_assertion_error(): + raise AssertionError('\xff') + """) + result = testdir.runpytest() + result.stdout.fnmatch_lines([r"> raise AssertionError('\xff')", 'E AssertionError: *']) + +def test_raise_assertion_error_raisin_repr(testdir): + testdir.makepyfile(u""" + class RaisingRepr(object): + def __repr__(self): + raise Exception() + def test_raising_repr(): + raise AssertionError(RaisingRepr()) + """) + result = testdir.runpytest() + result.stdout.fnmatch_lines(['E AssertionError: ']) + def test_issue_1944(testdir): testdir.makepyfile(""" def f(): From 6aaf7ae18b2c786bbf7ff07d223860ee3f98c20c Mon Sep 17 00:00:00 2001 From: Omer Hadari Date: Sat, 4 Mar 2017 10:32:14 +0200 Subject: [PATCH 2/5] added to authors and changelog --- AUTHORS | 1 + CHANGELOG.rst | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index b49405bf7..c55be41e3 100644 --- a/AUTHORS +++ b/AUTHORS @@ -147,3 +147,4 @@ Victor Uriarte Vidar T. Fauske Wouter van Ackooy Xuecong Liao +Omer Hadari diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 32726274f..de3f334c2 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -22,7 +22,8 @@ * Replace ``raise StopIteration`` usages in the code by simple ``returns`` to finish generators, in accordance to `PEP-479`_ (`#2160`_). Thanks `@tgoodlet`_ for the report and `@nicoddemus`_ for the PR. -* +* Fix internal errors when an unprintable ``AssertionError`` is raised inside a test. + Thanks `@omerhadari`_ for the PR. * Skipping plugin now also works with test items generated by custom collectors (`#2231`_). Thanks to `@vidartf`_. From f71467f5b1c84fd662d4f1cca582e2d2d34a08d7 Mon Sep 17 00:00:00 2001 From: Omer Hadari Date: Sat, 4 Mar 2017 10:55:59 +0200 Subject: [PATCH 3/5] added link to changelog --- CHANGELOG.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index de3f334c2..216891484 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -43,6 +43,7 @@ .. _@sirex: https://github.com/sirex .. _@vidartf: https://github.com/vidartf .. _@kkoukiou: https://github.com/KKoukiou +.. _@omerhadari: https://github.com/omerhadari .. _#2248: https://github.com/pytest-dev/pytest/issues/2248 .. _#2137: https://github.com/pytest-dev/pytest/issues/2137 From 02dc54531150d715725885cb678209a4048142fb Mon Sep 17 00:00:00 2001 From: Omer Hadari Date: Sat, 4 Mar 2017 12:49:00 +0200 Subject: [PATCH 4/5] added in the correct alphabitcal order --- AUTHORS | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AUTHORS b/AUTHORS index c55be41e3..2332a51ef 100644 --- a/AUTHORS +++ b/AUTHORS @@ -101,7 +101,6 @@ Mathieu Clabaut Matt Bachmann Matt Williams Matthias Hafner -mbyt Michael Aquilina Michael Birtwell Michael Droettboom @@ -113,6 +112,7 @@ Nicolas Delaby Oleg Pidsadnyi Oliver Bestwalter Omar Kohl +Omer Hadari Patrick Hayes Pieter Mulder Piotr Banaszkiewicz @@ -147,4 +147,4 @@ Victor Uriarte Vidar T. Fauske Wouter van Ackooy Xuecong Liao -Omer Hadari +mbyt From dd25ae7f3369fdbb4ec9fe3d50f0e4b25c9e6c25 Mon Sep 17 00:00:00 2001 From: Omer Hadari Date: Sat, 4 Mar 2017 12:50:02 +0200 Subject: [PATCH 5/5] added in the correct alphabitcal order --- AUTHORS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 2332a51ef..821bd2a6d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -101,6 +101,7 @@ Mathieu Clabaut Matt Bachmann Matt Williams Matthias Hafner +mbyt Michael Aquilina Michael Birtwell Michael Droettboom @@ -147,4 +148,3 @@ Victor Uriarte Vidar T. Fauske Wouter van Ackooy Xuecong Liao -mbyt