Clean up pdb state after each pdb doctest (#132577)

This commit is contained in:
Tian Gao 2025-04-16 10:44:33 -07:00 committed by GitHub
parent 14efcadfd4
commit 62173cc02c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -224,10 +224,6 @@ def test_pdb_basic_commands():
BAZ BAZ
""" """
def reset_Breakpoint():
import bdb
bdb.Breakpoint.clearBreakpoints()
def test_pdb_breakpoint_commands(): def test_pdb_breakpoint_commands():
"""Test basic commands related to breakpoints. """Test basic commands related to breakpoints.
@ -238,11 +234,6 @@ def test_pdb_breakpoint_commands():
... print(3) ... print(3)
... print(4) ... print(4)
First, need to clear bdb state that might be left over from previous tests.
Otherwise, the new breakpoints might get assigned different numbers.
>>> reset_Breakpoint()
Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because
the breakpoint list outputs a tab for the "stop only" and "ignore next" the breakpoint list outputs a tab for the "stop only" and "ignore next"
lines, which we don't want to put in here. lines, which we don't want to put in here.
@ -367,8 +358,6 @@ def test_pdb_breakpoint_commands():
def test_pdb_breakpoint_ignore_and_condition(): def test_pdb_breakpoint_ignore_and_condition():
""" """
>>> reset_Breakpoint()
>>> def test_function(): >>> def test_function():
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... for i in range(5): ... for i in range(5):
@ -384,26 +373,26 @@ def test_pdb_breakpoint_ignore_and_condition():
... 'continue', ... 'continue',
... ]): ... ]):
... test_function() ... test_function()
> <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[1]>(2)test_function() > <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[0]>(2)test_function()
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
(Pdb) break 4 (Pdb) break 4
Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[1]>:4 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[0]>:4
(Pdb) ignore 1 2 (Pdb) ignore 1 2
Will ignore next 2 crossings of breakpoint 1. Will ignore next 2 crossings of breakpoint 1.
(Pdb) continue (Pdb) continue
0 0
1 1
> <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[1]>(4)test_function() > <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[0]>(4)test_function()
-> print(i) -> print(i)
(Pdb) condition 1 i == 4 (Pdb) condition 1 i == 4
New condition set for breakpoint 1. New condition set for breakpoint 1.
(Pdb) continue (Pdb) continue
2 2
3 3
> <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[1]>(4)test_function() > <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[0]>(4)test_function()
-> print(i) -> print(i)
(Pdb) clear 1 (Pdb) clear 1
Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[1]>:4 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[0]>:4
(Pdb) continue (Pdb) continue
4 4
""" """
@ -420,8 +409,6 @@ def test_pdb_breakpoint_on_annotated_function_def():
>>> def foobar[T]() -> int: >>> def foobar[T]() -> int:
... return 0 ... return 0
>>> reset_Breakpoint()
>>> def test_function(): >>> def test_function():
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... pass ... pass
@ -433,7 +420,7 @@ def test_pdb_breakpoint_on_annotated_function_def():
... 'continue', ... 'continue',
... ]): ... ]):
... test_function() ... test_function()
> <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[4]>(2)test_function() > <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[3]>(2)test_function()
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
(Pdb) break foo (Pdb) break foo
Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[0]>:2 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[0]>:2
@ -453,8 +440,6 @@ def test_pdb_commands():
... print(2) ... print(2)
... print(3) ... print(3)
>>> reset_Breakpoint()
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
... 'b 3', ... 'b 3',
... 'commands', ... 'commands',
@ -501,13 +486,6 @@ def test_pdb_breakpoint_with_filename():
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... mod2.func88() ... mod2.func88()
... mod2.func114() ... mod2.func114()
... # Be a good citizen and clean up the mess
... reset_Breakpoint()
First, need to clear bdb state that might be left over from previous tests.
Otherwise, the new breakpoints might get assigned different numbers.
>>> reset_Breakpoint()
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
... 'break test.test_inspect.inspect_fodder2:90', ... 'break test.test_inspect.inspect_fodder2:90',
@ -535,7 +513,6 @@ def test_pdb_breakpoint_with_filename():
def test_pdb_breakpoint_on_disabled_line(): def test_pdb_breakpoint_on_disabled_line():
"""New breakpoint on once disabled line should work """New breakpoint on once disabled line should work
>>> reset_Breakpoint()
>>> def test_function(): >>> def test_function():
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... for i in range(3): ... for i in range(3):
@ -552,23 +529,23 @@ def test_pdb_breakpoint_on_disabled_line():
... 'c' ... 'c'
... ]): ... ]):
... test_function() ... test_function()
> <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[1]>(2)test_function() > <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[0]>(2)test_function()
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
(Pdb) break 5 (Pdb) break 5
Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[1]>:5 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[0]>:5
(Pdb) c (Pdb) c
> <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[1]>(5)test_function() > <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[0]>(5)test_function()
-> print(j) -> print(j)
(Pdb) clear 1 (Pdb) clear 1
Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[1]>:5 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[0]>:5
(Pdb) break 4 (Pdb) break 4
Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[1]>:4 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[0]>:4
(Pdb) c (Pdb) c
0 0
> <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[1]>(4)test_function() > <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[0]>(4)test_function()
-> j = i * 2 -> j = i * 2
(Pdb) clear 2 (Pdb) clear 2
Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[1]>:4 Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[0]>:4
(Pdb) c (Pdb) c
2 2
4 4
@ -577,7 +554,6 @@ def test_pdb_breakpoint_on_disabled_line():
def test_pdb_breakpoints_preserved_across_interactive_sessions(): def test_pdb_breakpoints_preserved_across_interactive_sessions():
"""Breakpoints are remembered between interactive sessions """Breakpoints are remembered between interactive sessions
>>> reset_Breakpoint()
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
... 'import test.test_pdb', ... 'import test.test_pdb',
... 'break test.test_pdb.do_something', ... 'break test.test_pdb.do_something',
@ -662,7 +638,6 @@ def test_pdb_break_anywhere():
>>> def test_function(): >>> def test_function():
... caller() ... caller()
>>> reset_Breakpoint()
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
... 'b 3', ... 'b 3',
... 'c', ... 'c',
@ -1776,7 +1751,6 @@ def test_pdb_return_to_different_file():
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... pprint.pprint(A()) ... pprint.pprint(A())
>>> reset_Breakpoint()
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
... 'b A.__repr__', ... 'b A.__repr__',
... 'continue', ... 'continue',
@ -2031,7 +2005,6 @@ def test_next_until_return_at_return_event():
... test_function_2() ... test_function_2()
... end = 1 ... end = 1
>>> reset_Breakpoint()
>>> with PdbTestInput(['break test_function_2', >>> with PdbTestInput(['break test_function_2',
... 'continue', ... 'continue',
... 'return', ... 'return',
@ -2501,7 +2474,6 @@ def test_pdb_next_command_in_generator_for_loop():
... print('value', i) ... print('value', i)
... x = 123 ... x = 123
>>> reset_Breakpoint()
>>> with PdbTestInput(['break test_gen', >>> with PdbTestInput(['break test_gen',
... 'continue', ... 'continue',
... 'next', ... 'next',
@ -2584,8 +2556,6 @@ def test_pdb_next_command_subiterator():
def test_pdb_breakpoint_with_throw(): def test_pdb_breakpoint_with_throw():
"""GH-132536: PY_THROW event should not be turned off """GH-132536: PY_THROW event should not be turned off
>>> reset_Breakpoint()
>>> def gen(): >>> def gen():
... yield 0 ... yield 0
@ -2604,15 +2574,15 @@ def test_pdb_breakpoint_with_throw():
... 'continue', ... 'continue',
... ]): ... ]):
... test_function() ... test_function()
> <doctest test.test_pdb.test_pdb_breakpoint_with_throw[2]>(2)test_function() > <doctest test.test_pdb.test_pdb_breakpoint_with_throw[1]>(2)test_function()
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
(Pdb) b 7 (Pdb) b 7
Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_with_throw[2]>:7 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_with_throw[1]>:7
(Pdb) continue (Pdb) continue
> <doctest test.test_pdb.test_pdb_breakpoint_with_throw[2]>(7)test_function() > <doctest test.test_pdb.test_pdb_breakpoint_with_throw[1]>(7)test_function()
-> pass -> pass
(Pdb) clear 1 (Pdb) clear 1
Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_with_throw[2]>:7 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_with_throw[1]>:7
(Pdb) continue (Pdb) continue
""" """
@ -2807,7 +2777,6 @@ def test_pdb_issue_20766():
... print('pdb %d: %s' % (i, sess._previous_sigint_handler)) ... print('pdb %d: %s' % (i, sess._previous_sigint_handler))
... i += 1 ... i += 1
>>> reset_Breakpoint()
>>> with PdbTestInput(['continue', >>> with PdbTestInput(['continue',
... 'continue']): ... 'continue']):
... test_function() ... test_function()
@ -2830,7 +2799,6 @@ def test_pdb_issue_43318():
... print(2) ... print(2)
... print(3) ... print(3)
... print(4) ... print(4)
>>> reset_Breakpoint()
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
... 'break 3', ... 'break 3',
... 'clear <doctest test.test_pdb.test_pdb_issue_43318[0]>:3', ... 'clear <doctest test.test_pdb.test_pdb_issue_43318[0]>:3',
@ -2867,7 +2835,6 @@ def test_pdb_issue_gh_91742():
... about() ... about()
>>> reset_Breakpoint()
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
... 'step', ... 'step',
... 'step', ... 'step',
@ -2914,7 +2881,6 @@ def test_pdb_issue_gh_94215():
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... func() ... func()
>>> reset_Breakpoint()
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
... 'step', ... 'step',
... 'step', ... 'step',
@ -3145,8 +3111,6 @@ def test_pdb_f_trace_lines():
pdb should work even if f_trace_lines is set to False on some frames. pdb should work even if f_trace_lines is set to False on some frames.
>>> reset_Breakpoint()
>>> def test_function(): >>> def test_function():
... import sys ... import sys
... frame = sys._getframe() ... frame = sys._getframe()
@ -3159,7 +3123,7 @@ def test_pdb_f_trace_lines():
... 'continue' ... 'continue'
... ]): ... ]):
... test_function() ... test_function()
> <doctest test.test_pdb.test_pdb_f_trace_lines[1]>(5)test_function() > <doctest test.test_pdb.test_pdb_f_trace_lines[0]>(5)test_function()
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
(Pdb) continue (Pdb) continue
""" """
@ -4719,6 +4683,21 @@ def load_tests(loader, tests, pattern):
# asyncio features are used. # asyncio features are used.
_set_event_loop_policy(None) _set_event_loop_policy(None)
# A doctest of pdb could have residues. For example, pdb could still
# be running, or breakpoints might be left uncleared. These residues
# could potentially interfere with the following test, especially
# when we switch backends. Here we clear all the residues to restore
# to its pre-test state.
# clear all the breakpoints left
import bdb
bdb.Breakpoint.clearBreakpoints()
# Stop tracing and clear the pdb instance cache
if pdb.Pdb._last_pdb_instance:
pdb.Pdb._last_pdb_instance.stop_trace()
pdb.Pdb._last_pdb_instance = None
tests.addTest( tests.addTest(
doctest.DocTestSuite( doctest.DocTestSuite(
test_pdb, test_pdb,