Skip to content

Pasting a function definition does not work in 3.13 REPL with Windows Terminal #124096

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

Closed
japageth opened this issue Sep 15, 2024 · 12 comments
Closed
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes OS-windows stdlib Python modules in the Lib dir topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@japageth
Copy link

japageth commented Sep 15, 2024

Bug report

Bug description:

I use Python downloaded from python.org without using a graphical environment such as IPython. Instead, I use the Windows Terminal with the REPL built into python.exe. In 3.12.6, I can paste a function definition copied from a text editor directly into the REPL and everything works fine. In 3.13.rc2 this does not work; the indentation is all messed up and I get an IndentationError.

def letter_colors(word, guess):
    '''Compute letter colors for Wordle guesses.  B=black Y=yellow G=green'''
    if (n := len(word)) != len(guess):
        raise ValueError('Word and guess must be the same length.')
    result = ['G' if wl == gl else 'B' for (wl, gl) in zip(word, guess)]
    unused = [w for (w, r) in zip(word, result) if r == 'B']
    for i, c in enumerate(guess):
        if result[i] != 'G' and c in unused:
            result[i] = 'Y'
            unused.remove(c)
    return ''.join(result)

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

Windows

Linked PRs

@japageth japageth added the type-bug An unexpected behavior, bug, or error label Sep 15, 2024
@Eclips4 Eclips4 added OS-windows topic-repl Related to the interactive shell labels Sep 15, 2024
@terryjreedy
Copy link
Member

The code you show looks fine to me. Is this what you see in 3.13?
Please copy and paste the full traceback or message.

@Wulian233
Copy link
Contributor

The bug can be reproduced, here is a screenshot

2024-09-15 192801

Duplicate #122237
PR #122383

@terryjreedy
Copy link
Member

Thank you for the additional clear info. Closing as duplicate.

@terryjreedy terryjreedy closed this as not planned Won't fix, can't repro, duplicate, stale Sep 15, 2024
@y5c4l3
Copy link
Contributor

y5c4l3 commented Sep 16, 2024

Duplicate #122237 PR #122383

This is not a duplicate. It's a Windows-specific issue that WinAPI requires a console application to set their input handle with ENABLE_VIRTUAL_TERMINAL_INPUT flag before it can be talked in VT escaped sequences. The flag isn't set in windows_console.py, so it's impossible to have bracketed-paste wrapping sequences sent to PYREPL.

Even if it is set, the default Windows command prompt, at least on 21H2 LTSC, does not support bracketed-paste. A modern terminal emulator like your Windows Terminal is required for this feature. We need to figure out a way to make an educated guess at user's terminal and turn off the autoindentation if necessary.

PYREPL is atm only scanning virtual key codes under Windows, once the console is configured as a virtual terminal, all the keys will be sent in VT sequences.

PR: #124119

After fix

@Wulian233
Copy link
Contributor

oh, you are right
@terryjreedy We need reopen this

@MMateo1120
Copy link

MMateo1120 commented Nov 16, 2024

I am using VS Code on Windows, and extra indentations are added when I shift+enter my code to run in the terminal. Also, even if I just copy the code into the terminal extra indentations are added. Any solution to the problem so far? The problem seems similar to microsoft/vscode-python#24256 where it was solved for some by the pre-lease version of python extension for vs code, but still exist for me on Windows.

Uninstalled every extension but python's:
386728750-ad8e3b32-eb8e-42ff-86a5-b1f3de710e2a

I am using the pre-lease version:
386728870-d9e2007b-583e-4c42-b279-e0b39afd29bb

the python version of 3.13.0 64-bit:
386729104-28dad9bf-286c-4f35-8165-c86d1540439d

Started a new window, a brand new py file but after shift+enter, the same behaviour is found as before:
386729493-971b11ae-adb4-4c7d-9986-8099a425fafc

If i ctrl c ctrl v the code into the terminal, extra indents are added just by the copying of the code into the terminal:
386729985-9389dc83-1bf1-43d5-b986-c3dea30d811d

@anthonykim1
Copy link

Adding to @MMateo1120 folks are also able to repro this outside of VS Code terminal (in external terminal) where indentations breaks like the image above.

@gocap55
Copy link

gocap55 commented Dec 19, 2024

I use Linux and vim, opening a terminal to execute python with . The same indentation error happens to me. I find no way to send "bracketed code" to the terminal. Even pasting a block of code doesn't work. The only workaround, other than using python 3.12, that seems to work for me is the following.
Translate this:

def signo(x):
    if x > 0:
        return '+'
    elif x < 0:
        return '-'
    else:
        return '0'

into this:

f01="def signo(x):\n    if x > 0:\n        return '+'\n    elif x < 0:\n        return '-'\n    else:\n        return '0'"
exec(f01)

@marslinks
Copy link

marslinks commented Feb 7, 2025

I have the same issue with Python 3.13. and I tried with Notepad, Notepad++, Visualstudio.
In the second I paste the code into the Pythong Terminal the code will be ruined:
Here you can see the problem:

Image
Image

I sent a message to Python Help and I got an answer that
"Please use “paste mode” (the F3 key) for pasting text to the console.

Making it work without F3 on Windows is work in progress, tracked here: #124096"

However for me even the F3 on Windows not help, as not pasting anything to the terminal.
Can anyone please help somehow as the Indentation changes itself when I copying the code from Notepad, Notepad++, or from Visualstudio to Python 3.13?

Is there any other way to use Python on Windows like with a Virtual Machine or with any other application?

The code:

import pandas as pd
import requests
import os

# Load the input file
input_file = 'companies.csv'
df = pd.read_csv(input_file)

# Your SerpAPI key
SERPAPI_KEY = 'INSERT Your SERPAPI_KEY_HERE'
base_url = 'https://serpapi.com/search'

# Create a list to hold results
results = []

# Loop through each company name and fetch website
for company in df['Company Name']:
params = {
'api_key': SERPAPI_KEY,
'engine': 'google',
'q': company, 
'hl': 'en',
'gl': 'us'
    }
    
response = requests.get(base_url, params=params)
data = response.json()
    
    # Get the first organic result link
if 'organic_results' in data and len(data['organic_results']) > 0:
    website = data['organic_results'][0].get('link')
    results.append({'Company Name': company, 'Website': website})
else:
    results.append({'Company Name': company, 'Website': None})

# Create a DataFrame and save to CSV
results_df = pd.DataFrame(results)
results_df.to_csv('company_websites.csv', index=False)

@marslinks
Copy link

marslinks commented Feb 7, 2025

Hi, I solved the problem by writing the code into Visualstudio and run in cmd with the
cd path
py script.py

At the beginning of the code I wrote
print("Starting the script...");

and the end

print("Finished processing for company:", company)
in Visualstudio.

I run it, but got an indentation error on line 17 regarding the params inside the for loop. I used 4 white spaces to fix it and another 4 spaces to fix lines under:

Image

Then I saved it in Visualstudio and run it again in cmd with
cd path
py get_websites.py

and voila it run successfully

Image

and did not try to copy and paste into Python terminal.

So writing a code in Visualbasic and then saving it, and then running with cmd looks like a solution so far.

ambv pushed a commit that referenced this issue Feb 23, 2025
To support virtual terminal mode in Windows PYREPL, we need a scanner
to read over the supported escaped VT sequences.

Windows REPL input was using virtual key mode, which does not support
terminal escape sequences. This patch calls `SetConsoleMode` properly
when initializing and send sequences to enable bracketed-paste modes
to support verbatim copy-and-paste.

Signed-off-by: y5c4l3 <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
Co-authored-by: Pablo Galindo Salgado <[email protected]>
Co-authored-by: Dustin L. Howett <[email protected]>
Co-authored-by: wheeheee <[email protected]>
seehwan pushed a commit to seehwan/cpython that referenced this issue Apr 16, 2025
…hon#124119)

To support virtual terminal mode in Windows PYREPL, we need a scanner
to read over the supported escaped VT sequences.

Windows REPL input was using virtual key mode, which does not support
terminal escape sequences. This patch calls `SetConsoleMode` properly
when initializing and send sequences to enable bracketed-paste modes
to support verbatim copy-and-paste.

Signed-off-by: y5c4l3 <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
Co-authored-by: Pablo Galindo Salgado <[email protected]>
Co-authored-by: Dustin L. Howett <[email protected]>
Co-authored-by: wheeheee <[email protected]>
@brokedarius
Copy link

Sorry but ughh... using F3 is honestly the ugliest way of going about it, what is even a 'paste mode'? Is it like an insert mode but not quite? Sounds confusing.

It should be that if I paste text inside the console, it gets pasted. Maybe an ask if there are multiple lines of code and that's it.

That solution is a workaround at best. There is no need for a 'special mode' to do one of the most trivial operations ever done on a computer.

Another thing to take into account is that tabs vs spaces when pasting, it also seems like another issue that lasted for years at this point.

Example here:

Image

That's because of stuff like this that everybody complains about the python REPL on Windows.

Folks should look at Powershell 7 and take some notes in terms of copy/pasting as well as history navigation, because the way you are going about it, is not it at all.

We are almost may 2025 and this problem still persists...

@picnixz picnixz added stdlib Python modules in the Lib dir 3.13 bugs and security fixes 3.14 bugs and security fixes labels Apr 28, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 5, 2025
…honGH-124119)

To support virtual terminal mode in Windows PYREPL, we need a scanner
to read over the supported escaped VT sequences.

Windows REPL input was using virtual key mode, which does not support
terminal escape sequences. This patch calls `SetConsoleMode` properly
when initializing and send sequences to enable bracketed-paste modes
to support verbatim copy-and-paste.
(cherry picked from commit a65366e)

Co-authored-by: Y5 <[email protected]>
Signed-off-by: y5c4l3 <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
Co-authored-by: Pablo Galindo Salgado <[email protected]>
Co-authored-by: Dustin L. Howett <[email protected]>
Co-authored-by: wheeheee <[email protected]>
ambv pushed a commit that referenced this issue May 5, 2025
…-124119) (GH-133457)

To support virtual terminal mode in Windows PYREPL, we need a scanner
to read over the supported escaped VT sequences.

Windows REPL input was using virtual key mode, which does not support
terminal escape sequences. This patch calls `SetConsoleMode` properly
when initializing and send sequences to enable bracketed-paste modes
to support verbatim copy-and-paste.
(cherry picked from commit a65366e)

Co-authored-by: Y5 <[email protected]>
Signed-off-by: y5c4l3 <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
Co-authored-by: Pablo Galindo Salgado <[email protected]>
Co-authored-by: Dustin L. Howett <[email protected]>
Co-authored-by: wheeheee <[email protected]>
@chris-eibl
Copy link
Member

Virtual terminal support has just been backported to 3.13, so in Windows terminals this will work now.
Only in legacy terminals F3 (paste mode) is still needed, since they do not support bracketed pasting.

@ambv I think this can be closed now?

@ambv ambv closed this as completed May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes OS-windows stdlib Python modules in the Lib dir topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests