bpo-36388: pdb: do_debug: restore previous sys.gettrace (might be None) by blueyed · Pull Request #12479 · python/cpython

A better test method might be good, this one is not using / cannot easily use readrc=False.

diff --git a/Lib/doctest.py b/Lib/doctest.py
index 79d91a040c..0ff4f2f746 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -353,7 +353,7 @@ def __init__(self, out):
         self.__out = out
         self.__debugger_used = False
         # do not play signal games in the pdb
-        pdb.Pdb.__init__(self, stdout=out, nosigint=True)
+        super().__init__(stdout=out, nosigint=True)
         # still use input() to get user input
         self.use_rawinput = 1
 
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index dd9e324c74..763f9f59c8 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -28,17 +28,28 @@ def __enter__(self):
         sys.stdin = _FakeInput(self.input)
         self.orig_trace = sys.gettrace() if hasattr(sys, 'gettrace') else None
 
+        class WrappedPdb(pdb.Pdb):
+            """Change defaults for readrc and nosigint for testing."""
+            def __init__(self, *args, **kwargs):
+                kwargs.setdefault('nosigint', True)
+                kwargs.setdefault('readrc', False)
+                super().__init__(*args, **kwargs)
+
+        self.orig_pdb = sys.modules['pdb']
+        pdb.Pdb = WrappedPdb
+
     def __exit__(self, *exc):
         sys.stdin = self.real_stdin
         if self.orig_trace:
             sys.settrace(self.orig_trace)
+        sys.modules['pdb'] = self.orig_pdb
 
 
 def test_pdb_displayhook():
     """This tests the custom displayhook for pdb.
 
     >>> def test_function(foo, bar):
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     pass
 
     >>> with PdbTestInput([
@@ -78,7 +89,7 @@ def test_pdb_basic_commands():
     ...     return foo.upper()
 
     >>> def test_function():
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     ret = test_function_2('baz')
     ...     print(ret)
 
@@ -177,7 +188,7 @@ def test_pdb_breakpoint_commands():
     """Test basic commands related to breakpoints.
 
     >>> def test_function():
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     print(1)
     ...     print(2)
     ...     print(3)
@@ -309,7 +320,7 @@ def test_list_commands():
     ...     return foo
 
     >>> def test_function():
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     ret = test_function_2('baz')
 
     >>> with PdbTestInput([  # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
@@ -332,7 +343,7 @@ def test_list_commands():
     -> ret = test_function_2('baz')
     (Pdb) list
       1         def test_function():
-      2             import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+      2             import pdb; pdb.Pdb().set_trace()
       3  ->         ret = test_function_2('baz')
     [EOF]
     (Pdb) step
@@ -395,7 +406,7 @@ def test_post_mortem():
     ...         print('Exception!')
 
     >>> def test_function():
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     test_function_2()
     ...     print('Not reached.')
 
@@ -428,7 +439,7 @@ def test_post_mortem():
     -> 1/0
     (Pdb) list
       1         def test_function():
-      2             import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+      2             import pdb; pdb.Pdb().set_trace()
       3  ->         test_function_2()
       4             print('Not reached.')
     [EOF]
@@ -487,7 +498,7 @@ def test_pdb_skip_modules():
 
     >>> def skip_module():
     ...     import string
-    ...     import pdb; pdb.Pdb(skip=['stri*'], nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb(skip=['stri*']).set_trace()
     ...     string.capwords('FOO')
 
     >>> with PdbTestInput([
@@ -516,7 +527,7 @@ def test_pdb_skip_modules_with_callback():
     >>> def skip_module():
     ...     def callback():
     ...         return None
-    ...     import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
     ...     mod.foo_pony(callback)
 
     >>> with PdbTestInput([
@@ -557,7 +568,7 @@ def test_pdb_continue_in_bottomframe():
     """Test that "continue" and "next" work properly in bottom frame (issue #5294).
 
     >>> def test_function():
-    ...     import pdb, sys; inst = pdb.Pdb(nosigint=True, readrc=False)
+    ...     import pdb, sys; inst = pdb.Pdb()
     ...     inst.set_trace()
     ...     inst.botframe = sys._getframe()  # hackery to get the right botframe
     ...     print(1)
@@ -646,7 +657,7 @@ def test_next_until_return_at_return_event():
     ...     x = 2
 
     >>> def test_function():
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     test_function_2()
     ...     test_function_2()
     ...     test_function_2()
@@ -712,7 +723,7 @@ def test_pdb_next_command_for_generator():
     ...     yield 2
 
     >>> def test_function():
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     it = test_gen()
     ...     try:
     ...         if next(it) != 0:
@@ -773,7 +784,7 @@ def test_pdb_next_command_for_coroutine():
     ...     await asyncio.sleep(0)
 
     >>> async def test_main():
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     await test_coro()
 
     >>> def test_function():
@@ -833,7 +844,7 @@ def test_pdb_next_command_for_asyncgen():
     ...         print(x)
 
     >>> async def test_main():
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     await test_coro()
 
     >>> def test_function():
@@ -889,7 +900,7 @@ def test_pdb_return_command_for_generator():
     ...     yield 2
 
     >>> def test_function():
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     it = test_gen()
     ...     try:
     ...         if next(it) != 0:
@@ -945,7 +956,7 @@ def test_pdb_return_command_for_coroutine():
     ...     await asyncio.sleep(0)
 
     >>> async def test_main():
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     await test_coro()
 
     >>> def test_function():
@@ -986,7 +997,7 @@ def test_pdb_until_command_for_generator():
     ...     yield 2
 
     >>> def test_function():
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     for i in test_gen():
     ...         print(i)
     ...     print("finished")
@@ -1036,7 +1047,7 @@ def test_pdb_until_command_for_coroutine():
     ...     print(3)
 
     >>> async def test_main():
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     await test_coro()
 
     >>> def test_function():
@@ -1075,7 +1086,7 @@ def test_pdb_next_command_in_generator_for_loop():
     ...     return 1
 
     >>> def test_function():
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     for i in test_gen():
     ...         print('value', i)
     ...     x = 123
@@ -1120,7 +1131,7 @@ def test_pdb_next_command_subiterator():
     ...     return x
 
     >>> def test_function():
-    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     import pdb; pdb.Pdb().set_trace()
     ...     for i in test_gen():
     ...         print('value', i)
     ...     x = 123
@@ -1161,7 +1172,7 @@ def test_pdb_issue_20766():
     >>> def test_function():
     ...     i = 1
     ...     while i <= 2:
-    ...         sess = pdb.Pdb()
+    ...         sess = pdb.Pdb(nosigint=False)
     ...         sess.set_trace(sys._getframe())
     ...         print('pdb %d: %s' % (i, sess._previous_sigint_handler))
     ...         i += 1