diff options
author | Sergey B Kirpichev <skirpichev@gmail.com> | 2024-09-03 12:37:29 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-03 12:37:29 (GMT) |
commit | 6822cb23c62032381971d8a47fd41d1e98710a8c (patch) | |
tree | da368dc55e3db0716a81df0c2644985a937e4146 /Python | |
parent | ef9d54703f1f21d2fde2fe3ef49d4cf7c8b38f06 (diff) | |
download | cpython-6822cb23c62032381971d8a47fd41d1e98710a8c.zip cpython-6822cb23c62032381971d8a47fd41d1e98710a8c.tar.gz cpython-6822cb23c62032381971d8a47fd41d1e98710a8c.tar.bz2 |
gh-121804: always show error location for SyntaxError's in basic repl (#123202)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pythonrun.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c index ce7f194..b675971 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -280,11 +280,42 @@ 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) { + PyThreadState *tstate = _PyThreadState_GET(); + 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); + if (xs == NULL) { + goto error; + } + PyObject *exc_lineno = PyObject_GetAttr(exc, &_Py_ID(lineno)); + if (exc_lineno == NULL) { + Py_DECREF(xs); + goto error; + } + int n = PyLong_AsInt(exc_lineno); + Py_DECREF(exc_lineno); + if (n <= 0 || n > PyList_GET_SIZE(xs)) { + Py_DECREF(xs); + goto error; + } + PyObject *line = PyList_GET_ITEM(xs, n - 1); + PyObject_SetAttr(exc, &_Py_ID(text), line); + Py_DECREF(xs); + } +error: + Py_DECREF(interactive_src); + _PyErr_SetRaisedException(tstate, exc); return -1; } + Py_DECREF(interactive_src); Py_DECREF(res); flush_io(); |