python / cpython

The Python programming language
https://www.python.org
Other
63.4k stars 30.36k forks source link

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

Open japageth opened 1 month ago

japageth commented 1 month ago

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

terryjreedy commented 1 month ago

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 commented 1 month ago

The bug can be reproduced, here is a screenshot

2024-09-15 192801

Duplicate https://github.com/python/cpython/issues/122237 PR https://github.com/python/cpython/pull/122383

terryjreedy commented 1 month ago

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

y5c4l3 commented 1 month ago

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 commented 1 month ago

oh, you are right @terryjreedy We need reopen this