Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unittest hides traceback frames in chained exceptions #86413

Closed
dseomn mannequin opened this issue Nov 2, 2020 · 3 comments
Closed

unittest hides traceback frames in chained exceptions #86413

dseomn mannequin opened this issue Nov 2, 2020 · 3 comments
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@dseomn
Copy link
Mannequin

dseomn mannequin commented Nov 2, 2020

BPO 42247
Nosy @iritkatriel, @dseomn
PRs
  • bpo-42247: fix unittest.assertRaises bug where traceback entries were… #23687
  • bpo-24959: fix unittest.assertRaises bug where traceback entries are dropped from chained exceptions #23688
  • Superseder
  • bpo-24959: unittest swallows part of stack trace when raising AssertionError in a TestCase
  • Files
  • foo.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-04-09.13:31:56.772>
    created_at = <Date 2020-11-02.18:25:59.527>
    labels = ['type-bug', 'library', '3.10']
    title = 'unittest hides traceback frames in chained exceptions'
    updated_at = <Date 2021-04-09.13:31:56.772>
    user = 'https://github.com/dseomn'

    bugs.python.org fields:

    activity = <Date 2021-04-09.13:31:56.772>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-04-09.13:31:56.772>
    closer = 'iritkatriel'
    components = ['Library (Lib)']
    creation = <Date 2020-11-02.18:25:59.527>
    creator = 'dseomn'
    dependencies = []
    files = ['49563']
    hgrepos = []
    issue_num = 42247
    keywords = ['patch']
    message_count = 3.0
    messages = ['380244', '382705', '390489']
    nosy_count = 2.0
    nosy_names = ['iritkatriel', 'dseomn']
    pr_nums = ['23687', '23688']
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '24959'
    type = 'behavior'
    url = 'https://bugs.python.org/issue42247'
    versions = ['Python 3.10']

    @dseomn
    Copy link
    Mannequin Author

    dseomn mannequin commented Nov 2, 2020

    The traceback in the output of the attached test (see below) doesn't include line 5, which is where the original exception is raised. I think this is because

    if exctype is test.failureException:
    # Skip assert*() traceback levels
    length = self._count_relevant_tb_levels(tb)
    else:
    length = None
    tb_e = traceback.TracebackException(
    exctype, value, tb, limit=length, capture_locals=self.tb_locals)
    uses the limit parameter to try to hide the implementation of self.fail() from the traceback, but traceback.TracebackException.format() applies the limit to the chained exception. I'm not sure if that's a bug in unittest or traceback, but from the comment in the above part of unittest, I don't think it's intentional.

    F
    ======================================================================
    FAIL: test_foo (main.FooTest)
    ----------------------------------------------------------------------

    Traceback (most recent call last):
      File "foo.py", line 12, in test_foo
        foo()
    ValueError: foo
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "foo.py", line 14, in test_foo
        self.fail('foo() raised ValueError')
    AssertionError: foo() raised ValueError

    Ran 1 test in 0.000s

    FAILED (failures=1)

    @dseomn dseomn mannequin added 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Nov 2, 2020
    @iritkatriel iritkatriel added 3.10 only security fixes and removed 3.8 only security fixes labels Dec 8, 2020
    @iritkatriel
    Copy link
    Member

    I made a patch that replaces the limit hack by a filter on the traceback. We could stop here an just fix this, or we could create a more generic filter in the traceback module as suggested in bpo-31299.

    @iritkatriel
    Copy link
    Member

    I think this is the same as bpo-37712.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant