Skip to content

bpo-36388: pdb: do_debug: restore previous sys.gettrace (might be None)#12479

Open
blueyed wants to merge 1 commit intopython:mainfrom
blueyed:pdb-do-debug-settrace
Open

bpo-36388: pdb: do_debug: restore previous sys.gettrace (might be None)#12479
blueyed wants to merge 1 commit intopython:mainfrom
blueyed:pdb-do-debug-settrace

Conversation

@blueyed
Copy link
Copy Markdown
Contributor

@blueyed blueyed commented Mar 21, 2019

@blueyed blueyed force-pushed the pdb-do-debug-settrace branch from e9a5ffe to 26aaf94 Compare March 21, 2019 09:15
@blueyed blueyed mentioned this pull request Mar 21, 2019
1 task
@blueyed blueyed changed the title bpo-36388: skip sys.settrace at end of do_debug with no tracing bpo-36388: pdb: do_debug: restore previous sys.settrace (might be None) Mar 21, 2019
@blueyed blueyed changed the title bpo-36388: pdb: do_debug: restore previous sys.settrace (might be None) bpo-36388: pdb: do_debug: restore previous sys.gettrace (might be None) Mar 21, 2019
Comment thread Lib/test/test_pdb.py
... try:
... raise Exception()
... except Exception:
... import pdb; pdb.post_mortem()
Copy link
Copy Markdown
Contributor Author

@blueyed blueyed Mar 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Maybe something like this?

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

But seems a bit intrusive - maybe $HOME/$USERPROFILE could be changed?

@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented Apr 6, 2025

The following commit authors need to sign the Contributor License Agreement:

CLA signed

@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Sep 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants