Skip to content

Exception traceback contains handled exceptions #668

@BCSharp

Description

@BCSharp

I have encountered a strange issue:

import unittest, codecs, io

from iptest import run_test

class Test(unittest.TestCase):

    def test_1_fail(self):
        self.fail("deliberate failure")

    def test_2_pass(self):
        s = io.BytesIO(b"\xff\xff")
        f = codecs.getreader('utf-16')(s)
        self.assertRaises(UnicodeError, f.read)

run_test(__name__)

Instead of reporting only one exception (from the failed test), IronPython somehow also reports the handled exception from the good test, but in a wrong order (since the exception in test 2 happens after the exception form test 1).

Here is the complete output:

test_1_fail (__main__.Test) ... FAIL
test_2_pass (__main__.Test) ... ok

======================================================================
FAIL: test_1_fail (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".\ex_unittest.py", line 12, in test_1_fail
    self.fail("deliberate failure")
AssertionError: deliberate failure

----------------------------------------------------------------------
Ran 2 tests in 0.519s

FAILED (failures=1)
Traceback (most recent call last):
  File "C:\Code\ironlang\ironpython3\Src\StdLib\Lib\unittest\case.py", line 162, in handle
  File "C:\Code\ironlang\ironpython3\Src\StdLib\Lib\codecs.py", line 497, in read
  File "C:\Code\ironlang\ironpython3\Src\StdLib\Lib\encodings\utf_16.py", line 141, in decode
UnicodeError: UTF-16 stream does not start with BOM

During handing of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".\ex_unittest.py", line 20, in <module>
  File "C:\Code\ironlang\ironpython3\bin\Release\net45\Lib\iptest\ipunittest.py", line 193, in run_test
  File "C:\Code\ironlang\ironpython3\Src\StdLib\Lib\test\support\__init__.py", line 1813, in run_unittest
  File "C:\Code\ironlang\ironpython3\Src\StdLib\Lib\test\support\__init__.py", line 1788, in _run_suite
test.support.TestFailed: Traceback (most recent call last):
  File ".\ex_unittest.py", line 12, in test_1_fail
    self.fail("deliberate failure")
AssertionError: deliberate failure

By comparison, here is output from CPython:

test_1_fail (__main__.Test) ... FAIL
test_2_pass (__main__.Test) ... ok

======================================================================
FAIL: test_1_fail (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".\ex_unittest.py", line 12, in test_1_fail
    self.fail("deliberate failure")
AssertionError: deliberate failure

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=1)
Traceback (most recent call last):
  File ".\ex_unittest.py", line 20, in <module>
    run_test(__name__)
  File "C:\Code\ironlang\ironpython3\bin\Release\net45\Lib\iptest\ipunittest.py", line 193, in run_test
    support.run_unittest(name)
  File "C:\Code\ironlang\test\support\__init__.py", line 1813, in run_unittest
    _run_suite(suite)
  File "C:\Code\ironlang\test\support\__init__.py", line 1788, in _run_suite
    raise TestFailed(err)
test.support.TestFailed: Traceback (most recent call last):
  File ".\ex_unittest.py", line 12, in test_1_fail
    self.fail("deliberate failure")
AssertionError: deliberate failure

Because CPython does not have this problem, I conclude it is caused by IronPython, rather than the test framework, although I was unable to demontrate the problem in any different way. Also the problem disappears if test 2 calls a simple exception thrower:

def fun():
    raise UnicodeError()

class Test(unittest.TestCase):
    ...

    def test_2_pass(self):
        self.assertRaises(UnicodeError, fun)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions