Skip to content
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

gh-111201: Allow bracketed paste to work #118700

Merged
merged 6 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/_pyrepl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,13 @@ class paste_mode(Command):
def do(self) -> None:
self.reader.paste_mode = not self.reader.paste_mode
self.reader.dirty = True


class enable_bracketed_paste(Command):
def do(self) -> None:
self.reader.paste_mode = True

class disable_bracketed_paste(Command):
def do(self) -> None:
self.reader.paste_mode = False
self.reader.insert("\n")
pablogsal marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ def make_default_commands() -> dict[CommandName, type[Command]]:
(r"\M-9", "digit-arg"),
# (r'\M-\n', 'insert-nl'),
("\\\\", "self-insert"),
(r"\x1b[200~", "enable_bracketed_paste"),
(r"\x1b[201~", "disable_bracketed_paste"),
]
+ [(c, "self-insert") for c in map(chr, range(32, 127)) if c != "\\"]
+ [(c, "self-insert") for c in map(chr, range(128, 256)) if c.isalpha()]
Expand Down
13 changes: 13 additions & 0 deletions Lib/_pyrepl/simple_interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ def showtraceback(self):
super().showtraceback(colorize=self.can_colorize)


def _enable_bracketed_paste() -> None:
sys.stdout.write("\x1b[?2004h")
sys.stdout.flush()


def _disable_bracketed_paste() -> None:
sys.stdout.write("\x1b[?2004l")
sys.stdout.flush()
pablogsal marked this conversation as resolved.
Show resolved Hide resolved


def run_multiline_interactive_console(
mainmodule: ModuleType | None= None, future_flags: int = 0
) -> None:
Expand Down Expand Up @@ -137,9 +147,12 @@ def more_lines(unicodetext: str) -> bool:
ps1 = getattr(sys, "ps1", ">>> ")
ps2 = getattr(sys, "ps2", "... ")
try:
_enable_bracketed_paste()
statement = multiline_input(more_lines, ps1, ps2)
except EOFError:
break
finally:
_disable_bracketed_paste()

if maybe_run_command(statement):
continue
Expand Down
40 changes: 40 additions & 0 deletions Lib/test/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,46 @@ def test_paste_not_in_paste_mode(self):
reader = self.prepare_reader(events)
output = multiline_input(reader)
self.assertEqual(output, output_code)

def test_bracketed_paste(self):
"""Test that bracketed paste using \x1b[200~ and \x1b[201~ works."""
# fmt: off
input_code = (
'def a():\n'
' for x in range(10):\n'
'\n'
' if x%2:\n'
' print(x)\n'
'\n'
' else:\n'
' pass\n'
)
# fmt: on

output_code = (
'def a():\n'
' for x in range(10):\n'
'\n'
' if x%2:\n'
' print(x)\n'
'\n'
' else:\n'
' pass\n'
'\n'
)

paste_start = "\x1b[200~"
paste_end = "\x1b[201~"

events = itertools.chain(
code_to_events(paste_start),
code_to_events(input_code),
code_to_events(paste_end),
code_to_events("\n"),
)
reader = self.prepare_reader(events)
output = multiline_input(reader)
self.assertEqual(output, output_code)


class TestReader(TestCase):
Expand Down
Loading