Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ def do_debug(self, arg):
argument (which is an arbitrary expression or statement to be
executed in the current environment).
"""
oldtrace = sys.gettrace()
sys.settrace(None)
globals = self.curframe.f_globals
locals = self.curframe_locals
Expand All @@ -1102,7 +1103,8 @@ def do_debug(self, arg):
exc_info = sys.exc_info()[:2]
self.error(traceback.format_exception_only(*exc_info)[-1].strip())
self.message("LEAVING RECURSIVE DEBUGGER")
sys.settrace(self.trace_dispatch)
if oldtrace:
sys.settrace(oldtrace)
self.lastcmd = p.lastcmd

complete_debug = _complete_expression
Expand Down
35 changes: 35 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,41 @@ def test_post_mortem():
Correctly reraised.
"""

def test_debug_in_post_mortem():
"""Test debug command in post_mortem.

pdb.Pdb.do_debug should not call settrace with no tracing being active.

This test does not work when run with `python -m pdb`.

>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
... 'debug 1',
... 'q',
... 'l',
... 'q',
... ]):
... 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?

> <doctest test.test_pdb.test_debug_in_post_mortem[0]>(8)<module>()
-> raise Exception()
(Pdb) debug 1
ENTERING RECURSIVE DEBUGGER
> <string>(1)<module>()
((Pdb)) q
LEAVING RECURSIVE DEBUGGER
(Pdb) l
5 'q',
6 ]):
7 try:
8 >> raise Exception()
9 except Exception:
10 -> import pdb; pdb.post_mortem()
[EOF]
(Pdb) q
"""


def test_pdb_skip_modules():
"""This illustrates the simple case of module skipping.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix pdb's ``debug`` command to properly restore only the existing
``sys.settrace()`` handler, which might be absent with ``post_mortem()``.
Patch provided by Daniel Hahler.