Skip to content
Merged
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
2 changes: 1 addition & 1 deletion _pytest/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def pytest_addoption(parser):
group = parser.getgroup("general")
group._addoption(
'--pdb', dest="usepdb", action="store_true",
help="start the interactive Python debugger on errors.")
help="start the interactive Python debugger on errors or KeyboardInterrupt.")
group._addoption(
'--pdbcls', dest="usepdb_cls", metavar="modulename:classname",
help="start a custom interactive Python debugger on errors. "
Expand Down
12 changes: 8 additions & 4 deletions _pytest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,24 +176,28 @@ def check_interactive_exception(call, report):
def call_runtest_hook(item, when, **kwds):
hookname = "pytest_runtest_" + when
ihook = getattr(item.ihook, hookname)
return CallInfo(lambda: ihook(item=item, **kwds), when=when)
return CallInfo(lambda: ihook(item=item, **kwds), when=when,
treat_keyboard_interrupt_as_exception=item.config.getvalue("usepdb"))


class CallInfo(object):
""" Result/Exception info a function invocation. """
#: None or ExceptionInfo object.
excinfo = None

def __init__(self, func, when):
def __init__(self, func, when, treat_keyboard_interrupt_as_exception=False):
#: context of invocation: one of "setup", "call",
#: "teardown", "memocollect"
self.when = when
self.start = time()
try:
self.result = func()
except KeyboardInterrupt:
self.stop = time()
raise
if treat_keyboard_interrupt_as_exception:
self.excinfo = ExceptionInfo()
else:
self.stop = time()
raise
except: # noqa
self.excinfo = ExceptionInfo()
self.stop = time()
Expand Down
2 changes: 2 additions & 0 deletions changelog/3299.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The ``--pdb`` option now causes KeyboardInterrupt to enter the debugger, instead of stopping the test session.
Copy link
Member

@nicoddemus nicoddemus May 1, 2018

Choose a reason for hiding this comment

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

Just to make sure: hitting CTRL+C again after landing on the debugger causes the debugger to exit right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I'll try to add a test that verifies that. Should I mention it in the changelog or docs?

Copy link
Member

Choose a reason for hiding this comment

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

I think just mentioning it in the changelog is sufficient, something like:

... instead of stopping the test session. Hitting CTRL+C again exits the debugger.

Copy link
Member

Choose a reason for hiding this comment

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

I see the test failed... not sure if it is easy to fix it; if not I'm OK with dropping the test, I think it is enough to ensure that we enter pdb when we get a KeyboardInterrupt.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also expected the test to fail because of pexpect's kill method not being cross-platform or something like that, but actually the test failed because pdb indeed behaves differently in python 3.6, and the KeyboardInterrupt does not cause it to exit. Are we ok with that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

looks like it was an intentional change: https://bugs.python.org/issue7245.
Haha now I understand the discussion on the issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The nosigint parameter to the Pdb constructor is supposed to disable that functionality, according to the docs (https://docs.python.org/3/library/pdb.html#pdb.Pdb). For some reason it doesn't seem to be working for me, but I can continue looking into it if we care about it.

On the other hand, probably better to let the pdb act like it usually acts for each version of python.

Copy link
Member

Choose a reason for hiding this comment

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

On the other hand, probably better to let the pdb act like it usually acts for each version of python.

Definitely, I agree! Thanks for researching this. 👍

On python 2.7, hitting CTRL+C again exits the debugger. On python 3.2 and higher, use CTRL+D.
6 changes: 3 additions & 3 deletions doc/en/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ allows one to drop into the PDB_ prompt via a command line option::

pytest --pdb

This will invoke the Python debugger on every failure. Often you might
only want to do this for the first failing test to understand a certain
failure situation::
This will invoke the Python debugger on every failure (or KeyboardInterrupt).
Often you might only want to do this for the first failing test to understand
a certain failure situation::

pytest -x --pdb # drop to PDB on first failure, then end test session
pytest --pdb --maxfail=3 # drop to PDB for first three failures
Expand Down
8 changes: 8 additions & 0 deletions testing/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ def test_func():
assert rep.failed
assert len(pdblist) == 0

def test_pdb_on_KeyboardInterrupt(self, testdir, pdblist):
rep = runpdb_and_get_report(testdir, """
def test_func():
raise KeyboardInterrupt
""")
assert rep.failed
assert len(pdblist) == 1

def test_pdb_interaction(self, testdir):
p1 = testdir.makepyfile("""
def test_1():
Expand Down