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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ Segev Finer
SemTiOne
Serhii Mozghovyi
Seth Junot
Shahab Ahmed
Shantanu Jain
Sharad Nair
Shaygan Hooshyari
Expand Down
1 change: 1 addition & 0 deletions changelog/3649.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The debugger is no longer entered when a collection error is caused by a syntax error, as the traceback would only show where the error is re-raised instead of the syntax error itself.
16 changes: 15 additions & 1 deletion src/_pytest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@ def check_interactive_exception(call: CallInfo[object], report: BaseReport) -> b
return True


def is_syntax_error(exc: BaseException) -> bool:
"""Check whether the exception was caused by a syntax error.

A syntax error is re-raised as a ``CollectError`` which keeps the original
``SyntaxError`` as its cause, so both are checked here.
"""
return isinstance(exc, SyntaxError) or isinstance(exc.__cause__, SyntaxError)


TResult = TypeVar("TResult", covariant=True)


Expand Down Expand Up @@ -610,5 +619,10 @@ def collect_one_node(collector: Collector) -> CollectReport:
rep: CollectReport = ihook.pytest_make_collect_report(collector=collector)
call = rep.__dict__.pop("call", None)
if call and check_interactive_exception(call, rep):
ihook.pytest_exception_interact(node=collector, call=call, report=rep)
# The debugger is not entered for a syntax error: the traceback would
# point at the place where the error is re-raised instead of at the
# syntax error itself, which is not helpful (see #3649).
assert call.excinfo is not None
if not is_syntax_error(call.excinfo.value):
ihook.pytest_exception_interact(node=collector, call=call, report=rep)
return rep
11 changes: 11 additions & 0 deletions testing/test_debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,17 @@ def test_pdb_collection_failure_is_shown(self, pytester: Pytester) -> None:
["E NameError: *xxx*", "*! *Exit: Quitting debugger !*"] # due to EOF
)

def test_pdb_not_entered_on_syntax_error(self, pytester: Pytester) -> None:
"""The debugger is not entered for a syntax error (see #3649).

The traceback of a syntax error is still reported, entering the
debugger would only show where the error is re-raised.
"""
p1 = pytester.makepyfile("syntax error")
result = pytester.runpytest_subprocess("--pdb", p1)
result.stdout.fnmatch_lines(["E SyntaxError: invalid syntax"])
result.stdout.no_fnmatch_line("*entering PDB*")

@pytest.mark.parametrize("post_mortem", (False, True))
def test_enter_leave_pdb_hooks_are_called(
self, post_mortem, pytester: Pytester
Expand Down
Loading