cpython icon indicating copy to clipboard operation
cpython copied to clipboard

Bad indentation after pasting code in interactive interpreter

Open rudyment opened this issue 1 year ago • 9 comments

Bug report

Bug description:

In the interactive interpreter it is not possible to simply insert function definitions from the clipboard, because each line of the function body is indented compared to the previous one.

If I copy the function definition:

def f(): x = 1 x = 2

to the clipboard and paste it into the interactive interpreter, I get

>>> def f(): ... x = 1 ... x = 2 File "<unknown>", line 3 x = 2 IndentationError: unexpected indent >>>

CPython versions tested on:

3.13

Operating systems tested on:

Windows

rudyment avatar Jul 05 '24 11:07 rudyment

I've not been able to reproduce this, in 3.13.0a5 from Bash, CMD or Powershell, on Windows 11.

Please could you supply more details, including the exact contents to copy from, e.g. in a Markdown literal code block (enclosed in a pair of triple back-ticks)?

JamesParrott avatar Jul 05 '24 12:07 JamesParrott

I've able to reproduce it on current main (Windows 10).

Eclips4 avatar Jul 05 '24 13:07 Eclips4

Maybe your terminal doesn't have bracketed paste mode (which activates this feature by default). Can you try with normal paste mode (F3)?

pablogsal avatar Jul 13 '24 08:07 pablogsal

Certainly. I've updated Windows since I last tried, if that matters. I've not used F2 paste mode before. Somehow the first character in the clipboard is deleted (a bug, but Windows 11 does this when I paste into cmd too, so it's nothing to do with Python), so I get a syntax error, and then a different indentation error on the first line. The second line looks to me like it has the same indentation level as the first.

image

image

image

From the VS Code integrated terminal it works fine, as long as the first paste option is selected (predictably, "Paste as one line" causes a syntax error:

image

(after clicking Paste, hitting enter once or twice, then ctrl+V again, and clicking "Paste as one line")

Python 3.13.0a5 (tags/v3.13.0a5:076d169, Mar 12 2024, 21:29:03) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...    x = 1
...    x = 2
... 
>>> def f():   x = 1   x = 2
  File "<stdin>", line 1
    def f():   x = 1   x = 2
                       ^
SyntaxError: invalid syntax

Also, In Alacritty (and Powershell), I can't even copy to, or paste from the clipboard at all (even using Windows+V), so I'll probably uninstall that:

image

JamesParrott avatar Jul 13 '24 09:07 JamesParrott

Right so after your edit I should've been pressing F3 not F2? OK then, I'll give that a go.....

....F3 just pastes the last line. This is an indentation error (as it starts with white space), but not the bug.

JamesParrott avatar Jul 13 '24 09:07 JamesParrott

Right so after your edit I should've been pressing F3 not F2? OK then, I'll give that a go.....

....F3 just pastes the last line. This is an indentation error (as it starts with white space), but not the bug.

Ok, that confirms that somehow you don't have bracketed paste mode in the terminal you are using if F3-paste mode works. What terminal are you using?

pablogsal avatar Jul 13 '24 13:07 pablogsal

F3 just pastes the last line.

What do you mean the last line? Only the last line or the entire text correctly indented?

pablogsal avatar Jul 13 '24 13:07 pablogsal

It's just called "Terminal" in the start bar and start menu, or in the settings: Windows Terminal (Microsoft Corporation) 1.20.11781.0. There's another option in there that I'll try now: Windows Console Host (Microsoft Corporation). That has a lot of options of its own.

For me, F3 pastes the last line of the multiline text in the clipboard, correctly indented (the same indentation for x=2 as x=1).

Which terminal application do you recommend, that has bracketed paste mode or is it a Windows setting?

JamesParrott avatar Jul 13 '24 14:07 JamesParrott

In "Command Prompt" (Windows Console Host) there are these settings.

image

Toggling "filter clipboard contents", "Enable line wrapping", "Extended text selection" and "Legacy mode" (and restarting the terminal) did not reproduce the bug. I could paste multiline indented code into Python 3.13 just fine.

To help others investigate this further, I would suggest those that can reproduce the bug, specify which terminal application they're using, and which code page their terminal is on, and perhaps even upload a redacted settings.json from %LOCALAPPDATA%\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json

Thanks very much for your assitance Pablo. I didn't know the F keys could be used for that in a terminal, so I've picked up a few tips. But you know I'm not the one inconvenienced by the bug, right?

JamesParrott avatar Jul 13 '24 14:07 JamesParrott

I think we can close this. @pablogsal What do you think?

lysnikolaou avatar Sep 23 '24 20:09 lysnikolaou

I agree 👍

pablogsal avatar Sep 29 '24 11:09 pablogsal

The problems caused by the system's wrong response to pressing the F3 key are gone, but I'm afraid the solution is still not optimal.

For a reason I don't understand, when pressing F3 to switch back from insert mode to standard mode, the prompt changes not only on the line where the cursor is currently on, but also on the line before. I don't know if this is really necessary.

Therefore, if the user doesn't want to crash the visual indentation of the text being pasted from the clipboard, he must type Enter twice before exiting paste mode to ensure that the appearance of the pasted code on the last line is not messed up.

For further clarification, I am ruling PDF-file with a snippet of text from my last guide where I explained this to readers.

I apologize, but due to my weaker English, the text is translated by DeepL program without further editing.

Rudolf Pecinovsky

Dne 2024-09-23 22:58, Lysandros Nikolaou napsal:

I think we can close this. @pablogsal [1] What do you think?

-- Reply to this email directly, view it on GitHub [2], or unsubscribe [3]. You are receiving this because you authored the thread.Message ID: @.***>

Links:

[1] https://github.com/pablogsal [2] https://github.com/python/cpython/issues/121400#issuecomment-2369369725 [3] https://github.com/notifications/unsubscribe-auth/AGTGVMC77ET4UKCK7O2MULTZYB6HXAVCNFSM6AAAAABKNBORRGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNRZGM3DSNZSGU

rudyment avatar Sep 29 '24 20:09 rudyment