summaryrefslogtreecommitdiffstats
path: root/Python/pythonrun.c
diff options
context:
space:
mode:
authoradam j hartz <hz@mit.edu>2025-08-14 17:58:11 (GMT)
committerGitHub <noreply@github.com>2025-08-14 17:58:11 (GMT)
commit04f8ef663be7589def9f456a2024e1707e3408ea (patch)
tree1e4d6d3b1f39a3bece09017ea86b75d680e88dcd /Python/pythonrun.c
parent8b8bd3d4ad80980e86562cd11ee30d3d6cedfab4 (diff)
downloadcpython-04f8ef663be7589def9f456a2024e1707e3408ea.zip
cpython-04f8ef663be7589def9f456a2024e1707e3408ea.tar.gz
cpython-04f8ef663be7589def9f456a2024e1707e3408ea.tar.bz2
gh-137576: Fix for Basic REPL showing incorrect code in tracebacks with `PYTHONSTARTUP` (#137625)
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Diffstat (limited to 'Python/pythonrun.c')
-rw-r--r--Python/pythonrun.c27
1 files changed, 25 insertions, 2 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 8f1c78b..45211e1 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -1366,6 +1366,29 @@ run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, Py
}
static PyObject *
+get_interactive_filename(PyObject *filename, Py_ssize_t count)
+{
+ PyObject *result;
+ Py_ssize_t len = PyUnicode_GET_LENGTH(filename);
+
+ if (len >= 2
+ && PyUnicode_ReadChar(filename, 0) == '<'
+ && PyUnicode_ReadChar(filename, len - 1) == '>') {
+ PyObject *middle = PyUnicode_Substring(filename, 1, len-1);
+ if (middle == NULL) {
+ return NULL;
+ }
+ result = PyUnicode_FromFormat("<%U-%d>", middle, count);
+ Py_DECREF(middle);
+ } else {
+ result = PyUnicode_FromFormat(
+ "%U-%d", filename, count);
+ }
+ return result;
+
+}
+
+static PyObject *
run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
PyCompilerFlags *flags, PyArena *arena, PyObject* interactive_src,
int generate_new_source)
@@ -1375,8 +1398,8 @@ run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
if (interactive_src) {
PyInterpreterState *interp = tstate->interp;
if (generate_new_source) {
- interactive_filename = PyUnicode_FromFormat(
- "%U-%d", filename, interp->_interactive_src_count++);
+ interactive_filename = get_interactive_filename(
+ filename, interp->_interactive_src_count++);
} else {
Py_INCREF(interactive_filename);
}