Repository navigation
gh-157612: Strip prompts and output from pasted REPL sessions - #157615
johnslavik wants to merge 4 commits into
Conversation
Documentation build overview
155 files changed ·
|
There was a problem hiding this comment.
I think it should also consider cases in which the prompts have been changed. I think these would be few but still ?
E.g., (not endorsing changing the prompt, but something that could happen, especially in other tools modifying the REPL)
>>> import sys
>>> sys.ps1 = "py> "
py> # This is code I want to copy and paste later
py> class A(object):
... def __new__(self):
... raise TypeError("i do not exist")
...
py> class B(A):
... __new__ = object.__new__
... def __init__(self, x):
... self.x = x
...
py> B(1)|
We could, but I find that limiting this to Do you personally customize your PS1 and would need that in this feature? |
|
Not explicitly but it is documented behavior on the tutorial. |
Would a user paste something that contains ANSI sequences?
What tools or IDEs modify Footnotes
|
|
Please also note that |
|
Oh you are right, didn't consider the case for it not being a string :) |
|
There's a few edge cases here that I don't like around how the thing that actually executes is not the thing that the person pasting the snippet expects:
>>> x = 1
>>> x = 2
KeyboardInterrupt
>>> x
1
There's more cases that are less concerning, but the state above makes me think that maybe this convenience is not worth it by default. A plausible way out would be to discover the paste of a console session, and attach more info to the SyntaxError raised that would tell the user "To use Paste Mode (F3) to automatically strip >>> and ...". We would also need to address the backslash-wrapped lines handling, and at least document the exception handling difference. |
132cd49 to
68f0d6a
Compare
|
1 - fixed, 2 - |
ambv
left a comment
There was a problem hiding this comment.
In the current PR, generic commands know about ReadlineAlikeReader.paste_repl_sessions and can_strip_repl_prompts, and that reader imports Python-session parsing helpers from commands.py. The responsibility is split across both layers. I’d use the existing command registry to install reader-specific paste commands. ReadlineAlikeReader.post_init() already does this for maybe_accept and backspace_dedent.
Keep bracketed-paste collection generic. Then, specialise it in readline.py (specialise the F3 command there too). Finally, register both specializations in ReadlineAlikeReader.post_init().
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
✨ I have made the requested changes; please review again ✨ |
|
Thanks for making the requested changes! @ambv: please review the changes made to this pull request. |
d9f9dc6 to
1b51aea
Compare
|
Merging main to fix a flake. |
Copy a REPL example from a web page and paste it straight into the new REPL:
At an empty
>>>prompt, the REPL removes the copied prompts and output, leaving this code ready to edit:Indentation stays intact. Nothing runs on paste, so you can change
double(3)todouble(5)before running it.This also makes it easier to send students examples or copy code between two REPLs to compare different parameters. You no longer need to delete prompts and output by hand.
The terminal must support bracketed paste, which tells the REPL where a paste starts and ends. The input must be empty, and the paste must start with a standard
>>>prompt. Pasting into existing code, a string, or aninput()prompt keeps the text unchanged.Rough edges
As discussed in the issue, this only recognizes
>>>and..., since those are the prompts most web examples use. Output that looks like a prompted code line will also be kept as code.The pasted code runs as one block, so it can behave differently from entering each command separately. The issue notes differences involving
sys.displayhookandbuiltins._.Tests cover prompt and output removal, different line endings, pastes that should stay unchanged, and editing before running.
>>>and...when pasting to REPL #157612