Skip to content

Commit d6832a8

Browse files
authored
Merge pull request #4133 from blueyed/pdb-quit
pdb: handle quitting in post_mortem
2 parents 3bfaa8a + 86c7dcf commit d6832a8

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/_pytest/debugging.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
""" interactive debugging with PDB, the Python Debugger. """
22
from __future__ import absolute_import, division, print_function
3+
4+
import os
35
import pdb
46
import sys
5-
import os
67
from doctest import UnexpectedException
78

9+
from _pytest import outcomes
810
from _pytest.config import hookimpl
911

1012
try:
@@ -161,8 +163,9 @@ def _enter_pdb(node, excinfo, rep):
161163
rep.toterminal(tw)
162164
tw.sep(">", "entering PDB")
163165
tb = _postmortem_traceback(excinfo)
164-
post_mortem(tb)
165166
rep._pdbshown = True
167+
if post_mortem(tb):
168+
outcomes.exit("Quitting debugger")
166169
return rep
167170

168171

@@ -193,3 +196,4 @@ def get_stack(self, f, t):
193196
p = Pdb()
194197
p.reset()
195198
p.interaction(None, t)
199+
return p.quitting

testing/test_pdb.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def custom_pdb_calls():
2525

2626
# install dummy debugger class and track which methods were called on it
2727
class _CustomPdb(object):
28+
quitting = False
29+
2830
def __init__(self, *args, **kwargs):
2931
called.append("init")
3032

@@ -142,6 +144,9 @@ def test_pdb_interaction(self, testdir):
142144
def test_1():
143145
i = 0
144146
assert i == 1
147+
148+
def test_not_called_due_to_quit():
149+
pass
145150
"""
146151
)
147152
child = testdir.spawn_pytest("--pdb %s" % p1)
@@ -150,8 +155,9 @@ def test_1():
150155
child.expect("Pdb")
151156
child.sendeof()
152157
rest = child.read().decode("utf8")
153-
assert "1 failed" in rest
158+
assert "= 1 failed in" in rest
154159
assert "def test_1" not in rest
160+
assert "Exit: Quitting debugger" in rest
155161
self.flush(child)
156162

157163
@staticmethod
@@ -321,7 +327,7 @@ def test_pdb_interaction_on_collection_issue181(self, testdir):
321327
child = testdir.spawn_pytest("--pdb %s" % p1)
322328
# child.expect(".*import pytest.*")
323329
child.expect("Pdb")
324-
child.sendeof()
330+
child.sendline("c")
325331
child.expect("1 error")
326332
self.flush(child)
327333

@@ -388,6 +394,7 @@ def test_1():
388394
rest = child.read().decode("utf8")
389395
assert "1 failed" in rest
390396
assert "reading from stdin while output" not in rest
397+
assert "BdbQuit" in rest
391398
self.flush(child)
392399

393400
def test_pdb_and_capsys(self, testdir):
@@ -530,7 +537,9 @@ def test_foo(a):
530537
def test_pdb_collection_failure_is_shown(self, testdir):
531538
p1 = testdir.makepyfile("xxx")
532539
result = testdir.runpytest_subprocess("--pdb", p1)
533-
result.stdout.fnmatch_lines(["*NameError*xxx*", "*1 error*"])
540+
result.stdout.fnmatch_lines(
541+
["E NameError: *xxx*", "*! *Exit: Quitting debugger !*"] # due to EOF
542+
)
534543

535544
def test_enter_pdb_hook_is_called(self, testdir):
536545
testdir.makeconftest(

0 commit comments

Comments
 (0)