From 6c8c1beb193712b71d9bfdaba0274900cceeefc4 Mon Sep 17 00:00:00 2001 From: Andrey Lisin Date: Mon, 5 Aug 2019 21:01:47 +0300 Subject: [PATCH] PY-37120 Python debugger - properly stop on syntax errors GitOrigin-RevId: e6280732fc658ed22741ac3cc5ba2535b4979e05 --- .../pydev/_pydevd_bundle/pydevd_comm.py | 12 ++++++--- python/testData/debug/test_syntax_error.py | 1 + .../env/python/PythonDebuggerTest.java | 27 +++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 python/testData/debug/test_syntax_error.py diff --git a/python/helpers/pydev/_pydevd_bundle/pydevd_comm.py b/python/helpers/pydev/_pydevd_bundle/pydevd_comm.py index 089ed4e3bb38..3d4ac2cb106d 100644 --- a/python/helpers/pydev/_pydevd_bundle/pydevd_comm.py +++ b/python/helpers/pydev/_pydevd_bundle/pydevd_comm.py @@ -705,9 +705,15 @@ class NetCommandFactory: abs_path_real_path_and_base = get_abs_path_real_path_and_base_from_frame(curr_frame) if get_file_type(abs_path_real_path_and_base[2]) == PYDEV_FILE: - # Skip pydevd files. - curr_frame = curr_frame.f_back - continue + # Syntax errors are a special case in which we don't want to skip the debugger files. + # When a syntax error happens, we stop either in the `execfile` or `_exec` function. + exception_info, is_syntax_error = curr_frame.f_locals.get('__exception__'), False + if exception_info: + is_syntax_error = exception_info[0] is SyntaxError + if not is_syntax_error: + # Skip pydevd files. + curr_frame = curr_frame.f_back + continue my_file = abs_path_real_path_and_base[0] diff --git a/python/testData/debug/test_syntax_error.py b/python/testData/debug/test_syntax_error.py new file mode 100644 index 000000000000..bfe913b3d85d --- /dev/null +++ b/python/testData/debug/test_syntax_error.py @@ -0,0 +1 @@ +x x \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/env/python/PythonDebuggerTest.java b/python/testSrc/com/jetbrains/env/python/PythonDebuggerTest.java index 1c1776ca30ed..9f27e7f5bd0d 100644 --- a/python/testSrc/com/jetbrains/env/python/PythonDebuggerTest.java +++ b/python/testSrc/com/jetbrains/env/python/PythonDebuggerTest.java @@ -488,6 +488,11 @@ public class PythonDebuggerTest extends PyEnvTestCase { addExceptionBreakpoint(fixture, properties); } + private static void createDefaultExceptionBreakpoint(IdeaProjectTestFixture fixture) { + XDebuggerTestUtil.removeAllBreakpoints(fixture.getProject()); + XDebuggerTestUtil.setDefaultBreakpointEnabled(fixture.getProject(), PyExceptionBreakpointType.class, true); + } + @Test public void testExceptionBreakpointOnFirstRaise() { runPythonTest(new PyDebuggerTask("/debug", "test_exceptbreak.py") { @@ -2192,4 +2197,26 @@ public class PythonDebuggerTest extends PyEnvTestCase { } }); } + + @Test + public void testStopsOnSyntaxError() { + runPythonTest(new PyDebuggerTask("/debug", "test_syntax_error.py") { + @Override + public void before() { + createDefaultExceptionBreakpoint(myFixture); + } + + @Override + public void testing() throws Exception { + waitForPause(); + try { + resume(); + waitForTerminate(); + } + catch (AssertionError e) { + if (!e.getMessage().contains("SyntaxError: invalid syntax")) throw e; + } + } + }); + } }