From 7dd8f830b5e2d7c30b766e11defc9deb223844e8 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 20 Aug 2024 17:17:20 +0300 Subject: [PATCH 01/10] gh-121804: always show error location for SyntaxError's in basic repl --- Lib/test/test_repl.py | 13 +++++++++ ...-08-21-15-22-53.gh-issue-121804.r5K3PS.rst | 2 ++ Python/pythonrun.c | 28 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-08-21-15-22-53.gh-issue-121804.r5K3PS.rst diff --git a/Lib/test/test_repl.py b/Lib/test/test_repl.py index 0b938623856e4f..363808cb444322 100644 --- a/Lib/test/test_repl.py +++ b/Lib/test/test_repl.py @@ -187,6 +187,19 @@ def foo(x): ] self.assertEqual(traceback_lines, expected_lines) + def test_runsource_show_syntax_error_location(self): + user_input = dedent("""def f(x, x): ... + """) + p = spawn_repl() + p.stdin.write(user_input) + output = kill_python(p) + expected_lines = [ + ' def f(x, x): ...', + ' ^', + "SyntaxError: duplicate argument 'x' in function definition" + ] + self.assertEqual(output.splitlines()[4:-1], expected_lines) + def test_interactive_source_is_in_linecache(self): user_input = dedent(""" def foo(x): diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-08-21-15-22-53.gh-issue-121804.r5K3PS.rst b/Misc/NEWS.d/next/Core and Builtins/2024-08-21-15-22-53.gh-issue-121804.r5K3PS.rst new file mode 100644 index 00000000000000..7c9c0230cdd9e5 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-08-21-15-22-53.gh-issue-121804.r5K3PS.rst @@ -0,0 +1,2 @@ +Correctly show error locations, when :exc:`SyntaxError` raised in basic +repl. Patch by Sergey B Kirpichev. diff --git a/Python/pythonrun.c b/Python/pythonrun.c index ce7f194e929c9c..a7d577bcd62621 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -283,6 +283,34 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, _PyArena_Free(arena); Py_DECREF(main_module); if (res == NULL) { + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *exc = tstate->current_exception; + if ((PyObject *)Py_TYPE(exc) == PyExc_SyntaxError) { + /* fix "text" attribute */ + assert(interactive_src); + PyObject *xs = PyUnicode_Splitlines(interactive_src, 1); + if (xs == NULL) { + return -1; + } + PyObject *ln = PyObject_GetAttr(exc, &_Py_ID(lineno)); + if (ln == NULL) { + Py_DECREF(xs); + return -1; + } + int n = PyLong_AsInt(ln); + Py_DECREF(ln); + assert(n>0); + assert(PyList_GET_SIZE(xs)>=n); + PyObject *line = PyList_GET_ITEM(xs, n - 1); + Py_INCREF(line); + Py_DECREF(xs); + if (PyObject_SetAttr(exc, &_Py_ID(text), line) == -1) { + Py_DECREF(line); + _PyErr_Clear(tstate); + return -1; + } + Py_DECREF(line); + } return -1; } Py_DECREF(res); From 49407f794835d6e873563d6d2a3b1042f7056412 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 22 Aug 2024 10:17:28 +0300 Subject: [PATCH 02/10] fix crash --- Python/pythonrun.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Python/pythonrun.c b/Python/pythonrun.c index a7d577bcd62621..0aab577b4e9a8c 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -280,6 +280,7 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, PyObject *main_dict = PyModule_GetDict(main_module); // borrowed ref PyObject *res = run_mod(mod, filename, main_dict, main_dict, flags, arena, interactive_src, 1); + Py_INCREF(interactive_src); _PyArena_Free(arena); Py_DECREF(main_module); if (res == NULL) { @@ -289,6 +290,7 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, /* fix "text" attribute */ assert(interactive_src); PyObject *xs = PyUnicode_Splitlines(interactive_src, 1); + Py_DECREF(interactive_src); if (xs == NULL) { return -1; } @@ -304,15 +306,19 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, PyObject *line = PyList_GET_ITEM(xs, n - 1); Py_INCREF(line); Py_DECREF(xs); + Py_INCREF(exc); + _PyErr_Clear(tstate); if (PyObject_SetAttr(exc, &_Py_ID(text), line) == -1) { - Py_DECREF(line); _PyErr_Clear(tstate); - return -1; } Py_DECREF(line); + _PyErr_SetRaisedException(tstate, exc); + return -1; } + Py_DECREF(interactive_src); return -1; } + Py_DECREF(interactive_src); Py_DECREF(res); flush_io(); From 834f346df560e9b89c700d968071a781b4a0d232 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 22 Aug 2024 10:18:20 +0300 Subject: [PATCH 03/10] + cleanup --- Python/pythonrun.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 0aab577b4e9a8c..df728514954055 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -288,7 +288,7 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, PyObject *exc = tstate->current_exception; if ((PyObject *)Py_TYPE(exc) == PyExc_SyntaxError) { /* fix "text" attribute */ - assert(interactive_src); + assert(interactive_src != NULL); PyObject *xs = PyUnicode_Splitlines(interactive_src, 1); Py_DECREF(interactive_src); if (xs == NULL) { @@ -301,8 +301,8 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, } int n = PyLong_AsInt(ln); Py_DECREF(ln); - assert(n>0); - assert(PyList_GET_SIZE(xs)>=n); + assert(n > 0); + assert(PyList_GET_SIZE(xs) >= n); PyObject *line = PyList_GET_ITEM(xs, n - 1); Py_INCREF(line); Py_DECREF(xs); From 033386d3042488ddaa60a15649cd5b7a9f6583a7 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 23 Aug 2024 07:54:22 +0300 Subject: [PATCH 04/10] address review: * use _PyErr_GetRaisedException * and PyType_IsSubtype --- Python/pythonrun.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Python/pythonrun.c b/Python/pythonrun.c index df728514954055..4d9b700c7fa480 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -285,8 +285,10 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, Py_DECREF(main_module); if (res == NULL) { PyThreadState *tstate = _PyThreadState_GET(); - PyObject *exc = tstate->current_exception; - if ((PyObject *)Py_TYPE(exc) == PyExc_SyntaxError) { + PyObject *exc = _PyErr_GetRaisedException(tstate); + if (PyType_IsSubtype(Py_TYPE(exc), + (PyTypeObject *) PyExc_SyntaxError)) + { /* fix "text" attribute */ assert(interactive_src != NULL); PyObject *xs = PyUnicode_Splitlines(interactive_src, 1); @@ -306,8 +308,6 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, PyObject *line = PyList_GET_ITEM(xs, n - 1); Py_INCREF(line); Py_DECREF(xs); - Py_INCREF(exc); - _PyErr_Clear(tstate); if (PyObject_SetAttr(exc, &_Py_ID(text), line) == -1) { _PyErr_Clear(tstate); } @@ -315,6 +315,7 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, _PyErr_SetRaisedException(tstate, exc); return -1; } + _PyErr_SetRaisedException(tstate, exc); Py_DECREF(interactive_src); return -1; } From f28d57f34559b5a3629b109c77cf75507b56ba97 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sat, 24 Aug 2024 11:36:43 +0300 Subject: [PATCH 05/10] address review: validate n too --- Python/pythonrun.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 4d9b700c7fa480..0686d60fc41f47 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -292,19 +292,20 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, /* fix "text" attribute */ assert(interactive_src != NULL); PyObject *xs = PyUnicode_Splitlines(interactive_src, 1); - Py_DECREF(interactive_src); if (xs == NULL) { - return -1; + goto err; } PyObject *ln = PyObject_GetAttr(exc, &_Py_ID(lineno)); if (ln == NULL) { Py_DECREF(xs); - return -1; + goto err; } int n = PyLong_AsInt(ln); Py_DECREF(ln); - assert(n > 0); - assert(PyList_GET_SIZE(xs) >= n); + if (n <= 0 || n > PyList_GET_SIZE(xs)) { + Py_DECREF(xs); + goto err; + } PyObject *line = PyList_GET_ITEM(xs, n - 1); Py_INCREF(line); Py_DECREF(xs); @@ -312,11 +313,10 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, _PyErr_Clear(tstate); } Py_DECREF(line); - _PyErr_SetRaisedException(tstate, exc); - return -1; } - _PyErr_SetRaisedException(tstate, exc); +err: Py_DECREF(interactive_src); + _PyErr_SetRaisedException(tstate, exc); return -1; } Py_DECREF(interactive_src); From ef426d24cb4fd5241c735ceb1aeb7d378edf9e33 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sat, 24 Aug 2024 20:04:06 +0300 Subject: [PATCH 06/10] address review --- Python/pythonrun.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 0686d60fc41f47..20d0294c1f2666 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -307,12 +307,10 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, goto err; } PyObject *line = PyList_GET_ITEM(xs, n - 1); - Py_INCREF(line); - Py_DECREF(xs); if (PyObject_SetAttr(exc, &_Py_ID(text), line) == -1) { _PyErr_Clear(tstate); } - Py_DECREF(line); + Py_DECREF(xs); } err: Py_DECREF(interactive_src); From b70d00a8f54157d879b84b26747b042054761789 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Wed, 28 Aug 2024 18:28:09 +0300 Subject: [PATCH 07/10] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Python/pythonrun.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 20d0294c1f2666..996acfec216a84 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -295,13 +295,13 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, if (xs == NULL) { goto err; } - PyObject *ln = PyObject_GetAttr(exc, &_Py_ID(lineno)); - if (ln == NULL) { + PyObject *exc_lineno = PyObject_GetAttr(exc, &_Py_ID(lineno)); + if (exc_lineno == NULL) { Py_DECREF(xs); goto err; } - int n = PyLong_AsInt(ln); - Py_DECREF(ln); + int n = PyLong_AsInt(exc_lineno); + Py_DECREF(exc_lineno); if (n <= 0 || n > PyList_GET_SIZE(xs)) { Py_DECREF(xs); goto err; From b04356afb3b9c8e59e160e666ef928fe682914c5 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Wed, 28 Aug 2024 18:31:10 +0300 Subject: [PATCH 08/10] address review: label name --- Python/pythonrun.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 996acfec216a84..80dbc5aa3d3a84 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -293,18 +293,18 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, assert(interactive_src != NULL); PyObject *xs = PyUnicode_Splitlines(interactive_src, 1); if (xs == NULL) { - goto err; + goto error; } PyObject *exc_lineno = PyObject_GetAttr(exc, &_Py_ID(lineno)); if (exc_lineno == NULL) { Py_DECREF(xs); - goto err; + goto error; } int n = PyLong_AsInt(exc_lineno); Py_DECREF(exc_lineno); if (n <= 0 || n > PyList_GET_SIZE(xs)) { Py_DECREF(xs); - goto err; + goto error; } PyObject *line = PyList_GET_ITEM(xs, n - 1); if (PyObject_SetAttr(exc, &_Py_ID(text), line) == -1) { @@ -312,7 +312,7 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, } Py_DECREF(xs); } -err: +error: Py_DECREF(interactive_src); _PyErr_SetRaisedException(tstate, exc); return -1; From c11789a5884422c5f3de52bb5368e6b3fd3f0849 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 29 Aug 2024 06:11:16 +0300 Subject: [PATCH 09/10] Update Misc/NEWS.d/next/Core and Builtins/2024-08-21-15-22-53.gh-issue-121804.r5K3PS.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../2024-08-21-15-22-53.gh-issue-121804.r5K3PS.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-08-21-15-22-53.gh-issue-121804.r5K3PS.rst b/Misc/NEWS.d/next/Core and Builtins/2024-08-21-15-22-53.gh-issue-121804.r5K3PS.rst index 7c9c0230cdd9e5..ce96c316923ce5 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-08-21-15-22-53.gh-issue-121804.r5K3PS.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-08-21-15-22-53.gh-issue-121804.r5K3PS.rst @@ -1,2 +1,2 @@ -Correctly show error locations, when :exc:`SyntaxError` raised in basic -repl. Patch by Sergey B Kirpichev. +Correctly show error locations when a :exc:`SyntaxError` is raised +in the basic REPL. Patch by Sergey B Kirpichev. From 90a1b8796a5ab76e6c0115b3a077eb2b6338ec77 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sat, 31 Aug 2024 05:25:24 +0300 Subject: [PATCH 10/10] Update Python/pythonrun.c --- Python/pythonrun.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 80dbc5aa3d3a84..b67597113ead45 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -307,9 +307,7 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, goto error; } PyObject *line = PyList_GET_ITEM(xs, n - 1); - if (PyObject_SetAttr(exc, &_Py_ID(text), line) == -1) { - _PyErr_Clear(tstate); - } + PyObject_SetAttr(exc, &_Py_ID(text), line); Py_DECREF(xs); } error: