diff options
author | Matthias Bussonnier <bussonniermatthias@gmail.com> | 2023-09-06 09:41:56 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-06 09:41:56 (GMT) |
commit | 5f3433f210d25d366fcebfb40adf444c8f46cd59 (patch) | |
tree | d46a2f714bfcbb049ec3495fcc2f01ef0c67be12 /Lib/test/test_pdb.py | |
parent | 44892b1c521c593a0ca23614ae27e96f846dd37c (diff) | |
download | cpython-5f3433f210d25d366fcebfb40adf444c8f46cd59.zip cpython-5f3433f210d25d366fcebfb40adf444c8f46cd59.tar.gz cpython-5f3433f210d25d366fcebfb40adf444c8f46cd59.tar.bz2 |
gh-106670: Fix Pdb handling of chained Exceptions with no stacks. (#108865)
Diffstat (limited to 'Lib/test/test_pdb.py')
-rw-r--r-- | Lib/test/test_pdb.py | 109 |
1 files changed, 84 insertions, 25 deletions
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index a9edd1a..f6bed84 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -848,9 +848,7 @@ def test_post_mortem_chained(): ... try: ... test_function_reraise() ... except Exception as e: - ... # same as pdb.post_mortem(e), but with custom pdb instance. - ... instance.reset() - ... instance.interaction(None, e) + ... pdb._post_mortem(e, instance) >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE ... 'exceptions', @@ -907,11 +905,18 @@ def test_post_mortem_chained(): def test_post_mortem_cause_no_context(): """Test post mortem traceback debugging of chained exception + >>> def make_exc_with_stack(type_, *content, from_=None): + ... try: + ... raise type_(*content) from from_ + ... except Exception as out: + ... return out + ... + >>> def main(): ... try: ... raise ValueError('Context Not Shown') ... except Exception as e1: - ... raise ValueError("With Cause") from TypeError('The Cause') + ... raise ValueError("With Cause") from make_exc_with_stack(TypeError,'The Cause') >>> def test_function(): ... import pdb; @@ -919,12 +924,11 @@ def test_post_mortem_cause_no_context(): ... try: ... main() ... except Exception as e: - ... # same as pdb.post_mortem(e), but with custom pdb instance. - ... instance.reset() - ... instance.interaction(None, e) + ... pdb._post_mortem(e, instance) >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE ... 'exceptions', + ... 'exceptions 0', ... 'exceptions 1', ... 'up', ... 'down', @@ -934,20 +938,23 @@ def test_post_mortem_cause_no_context(): ... test_function() ... except ValueError: ... print('Ok.') - > <doctest test.test_pdb.test_post_mortem_cause_no_context[0]>(5)main() - -> raise ValueError("With Cause") from TypeError('The Cause') + > <doctest test.test_pdb.test_post_mortem_cause_no_context[1]>(5)main() + -> raise ValueError("With Cause") from make_exc_with_stack(TypeError,'The Cause') (Pdb) exceptions - 0 TypeError('The Cause') - > 1 ValueError('With Cause') + 0 TypeError('The Cause') + > 1 ValueError('With Cause') + (Pdb) exceptions 0 + > <doctest test.test_pdb.test_post_mortem_cause_no_context[0]>(3)make_exc_with_stack() + -> raise type_(*content) from from_ (Pdb) exceptions 1 - > <doctest test.test_pdb.test_post_mortem_cause_no_context[0]>(5)main() - -> raise ValueError("With Cause") from TypeError('The Cause') + > <doctest test.test_pdb.test_post_mortem_cause_no_context[1]>(5)main() + -> raise ValueError("With Cause") from make_exc_with_stack(TypeError,'The Cause') (Pdb) up - > <doctest test.test_pdb.test_post_mortem_cause_no_context[1]>(5)test_function() + > <doctest test.test_pdb.test_post_mortem_cause_no_context[2]>(5)test_function() -> main() (Pdb) down - > <doctest test.test_pdb.test_post_mortem_cause_no_context[0]>(5)main() - -> raise ValueError("With Cause") from TypeError('The Cause') + > <doctest test.test_pdb.test_post_mortem_cause_no_context[1]>(5)main() + -> raise ValueError("With Cause") from make_exc_with_stack(TypeError,'The Cause') (Pdb) exit""" @@ -971,9 +978,7 @@ def test_post_mortem_context_of_the_cause(): ... try: ... main() ... except Exception as e: - ... # same as pdb.post_mortem(e), but with custom pdb instance. - ... instance.reset() - ... instance.interaction(None, e) + ... pdb._post_mortem(e, instance) >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE ... 'exceptions', @@ -1046,9 +1051,7 @@ def test_post_mortem_from_none(): ... try: ... main() ... except Exception as e: - ... # same as pdb.post_mortem(e), but with custom pdb instance. - ... instance.reset() - ... instance.interaction(None, e) + ... pdb._post_mortem(e, instance) >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE ... 'exceptions', @@ -1066,6 +1069,64 @@ def test_post_mortem_from_none(): """ +def test_post_mortem_from_no_stack(): + """Test post mortem traceback debugging of chained exception + + especially when one exception has no stack. + + >>> def main(): + ... raise Exception() from Exception() + + + >>> def test_function(): + ... import pdb; + ... instance = pdb.Pdb(nosigint=True, readrc=False) + ... try: + ... main() + ... except Exception as e: + ... pdb._post_mortem(e, instance) + + >>> with PdbTestInput( # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE + ... ["exceptions", + ... "exceptions 0", + ... "exit"], + ... ): + ... try: + ... test_function() + ... except ValueError: + ... print('Correctly reraised.') + > <doctest test.test_pdb.test_post_mortem_from_no_stack[0]>(2)main() + -> raise Exception() from Exception() + (Pdb) exceptions + - Exception() + > 1 Exception() + (Pdb) exceptions 0 + *** This exception does not have a traceback, cannot jump to it + (Pdb) exit + """ + + +def test_post_mortem_single_no_stack(): + """Test post mortem called when origin exception has no stack + + + >>> def test_function(): + ... import pdb; + ... instance = pdb.Pdb(nosigint=True, readrc=False) + ... import sys + ... sys.last_exc = Exception() + ... pdb._post_mortem(sys.last_exc, instance) + + >>> with PdbTestInput( # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE + ... [] + ... ): + ... try: + ... test_function() + ... except ValueError as e: + ... print(e) + A valid traceback must be passed if no exception is being handled + """ + def test_post_mortem_complex(): """Test post mortem traceback debugging of chained exception @@ -1130,9 +1191,7 @@ def test_post_mortem_complex(): ... try: ... main() ... except Exception as e: - ... # same as pdb.post_mortem(e), but with custom pdb instance. - ... instance.reset() - ... instance.interaction(None, e) + ... pdb._post_mortem(e, instance) >>> with PdbTestInput( # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE ... ["exceptions", |