Skip to content

gh-137576: Fix for Basic REPL Showing Incorrect Code in Tracebacks with PYTHONSTARTUP #137625

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

Merged
merged 8 commits into from
Aug 14, 2025

Conversation

adqm
Copy link
Contributor

@adqm adqm commented Aug 11, 2025

This patch attempts to fix #137576 by changing the way interactive_filename is computed for the basic REPL to make sure that it always begins with < and ends with > so that doesn't get an entry in linecache.cache (which was causing the source from PYTHONSTARTUP to be used instead of the interactive input when generating tracebacks).

Updated behavior:

$ echo "print('hello from PYTHONSTARTUP')" > /tmp/foo.py
$ PYTHON_BASIC_REPL=1 PYTHONSTARTUP=/tmp/foo.py ./python
Python 3.15.0a0 (heads/basic_repl_error_reporting:3d46e8feed, Aug 10 2025, 21:12:17) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
hello from PYTHONSTARTUP
>>> 1/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    1/0
    ~^~
ZeroDivisionError: division by zero
$ echo "def foo(x):  return 1/x" > /tmp/foo.py
$ PYTHON_BASIC_REPL=1 PYTHONSTARTUP=/tmp/foo.py ./python
Python 3.15.0a0 (heads/basic_repl_error_reporting:3d46e8feed, Aug 10 2025, 21:12:17) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def bar(x):
...     return foo(x)
... 
>>> bar(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    bar(0)
    ~~~^^^
  File "<stdin>", line 2, in bar
    return foo(x)
  File "/tmp/foo.py", line 1, in foo
    def foo(x):  return 1/x
                        ~^~
ZeroDivisionError: division by zero

@adqm
Copy link
Contributor Author

adqm commented Aug 11, 2025

My original patch here also caused the tracebacks to report the filename as <stdin-N> instead of <stdin>. I think there's something nice about that (from the REPL, it's nice to see when the sources shown in the traceback came from different interactive inputs, better symmetry with pyrepl's reporting, etc.), but for now I added a commit to keep the reported filename as <stdin>.

Copy link
Member

@Eclips4 Eclips4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!
A couple of nits.

adqm and others added 2 commits August 11, 2025 14:45
@Eclips4
Copy link
Member

Eclips4 commented Aug 13, 2025

@adqm I'm going to merge this tomorrow.
Am I right that this reproducible on 3.13 and 3.14 (I don't have access to my machine now)?

@adqm
Copy link
Contributor Author

adqm commented Aug 13, 2025

Great, thanks!!

I just tested; the bug is indeed present on current 3.13 and 3.14 branches.

These patches apply cleanly to 3.14 as of right now but not to 3.13 (though I'm happy to make the necessary changes there).

@Eclips4 Eclips4 added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Aug 13, 2025
@Eclips4 Eclips4 merged commit 04f8ef6 into python:main Aug 14, 2025
46 checks passed
@miss-islington-app
Copy link

Thanks @adqm for the PR, and @Eclips4 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 14, 2025
…cks with `PYTHONSTARTUP` (pythonGH-137625)

(cherry picked from commit 04f8ef663be7589def9f456a2024e1707e3408ea)

Co-authored-by: adam j hartz <hz@mit.edu>
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
@miss-islington-app
Copy link

Sorry, @adqm and @Eclips4, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 04f8ef663be7589def9f456a2024e1707e3408ea 3.13

@bedevere-app
Copy link

bedevere-app bot commented Aug 14, 2025

GH-137771 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Aug 14, 2025
@Eclips4
Copy link
Member

Eclips4 commented Aug 14, 2025

@adqm Could you please manually backport this to 3.13? Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs backport to 3.13 bugs and security fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

traceback shows wrong source code in basic REPL when PYTHONSTARTUP is set
2 participants