diff options
| author | adam j hartz <hz@mit.edu> | 2025-08-15 09:10:39 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-15 09:10:39 (GMT) |
| commit | 5131b8fe7ee61928a16c3aa71be91cc3b3d20f5e (patch) | |
| tree | 804fa845833faa34fea813cd7771fecd6c7db57f /Python/pythonrun.c | |
| parent | 785b39671982d270521010239f5d52d2d816a7b9 (diff) | |
| download | cpython-5131b8fe7ee61928a16c3aa71be91cc3b3d20f5e.zip cpython-5131b8fe7ee61928a16c3aa71be91cc3b3d20f5e.tar.gz cpython-5131b8fe7ee61928a16c3aa71be91cc3b3d20f5e.tar.bz2 | |
[3.13] gh-137576: Fix for Basic REPL showing incorrect code in tracebacks with `PYTHONSTARTUP` (GH-137625) (#137778)
(cherry picked from commit 04f8ef663be7589def9f456a2024e1707e3408ea)
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Diffstat (limited to 'Python/pythonrun.c')
| -rw-r--r-- | Python/pythonrun.c | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 62b7093..c4a1275 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1386,6 +1386,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) @@ -1395,8 +1418,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); } |
