Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-121804: always show error location for SyntaxError's in basic repl #123202

Merged
merged 15 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 13 additions & 0 deletions Lib/test/test_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Correctly show error locations, when :exc:`SyntaxError` raised in basic
repl. Patch by Sergey B Kirpichev.
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 34 additions & 0 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,45 @@ 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 = tstate->current_exception;
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
if ((PyObject *)Py_TYPE(exc) == PyExc_SyntaxError) {
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
/* fix "text" attribute */
assert(interactive_src != NULL);
PyObject *xs = PyUnicode_Splitlines(interactive_src, 1);
Py_DECREF(interactive_src);
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);
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
assert(PyList_GET_SIZE(xs) >= n);
PyObject *line = PyList_GET_ITEM(xs, n - 1);
Py_INCREF(line);
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
Py_DECREF(xs);
Py_INCREF(exc);
_PyErr_Clear(tstate);
if (PyObject_SetAttr(exc, &_Py_ID(text), line) == -1) {
_PyErr_Clear(tstate);
}
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
Py_DECREF(line);
_PyErr_SetRaisedException(tstate, exc);
return -1;
}
Py_DECREF(interactive_src);
return -1;
}
Py_DECREF(interactive_src);
Py_DECREF(res);

flush_io();
Expand Down
Loading