Skip to content

Commit a43b016

Browse files
committed
Implement this feature only for readline readers
1 parent 68f0d6a commit a43b016

2 files changed

Lines changed: 61 additions & 45 deletions

File tree

‎Lib/_pyrepl/commands.py‎

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2121

2222
import os
23-
import re
2423
import time
2524

2625
# Categories of actions:
@@ -495,47 +494,13 @@ def do(self) -> None:
495494
self.reader.invalidate_full()
496495

497496

498-
_REPL_PASTE_HINT = "press F3 (paste mode) to strip >>> and ... prompts"
499-
500-
501497
class paste_mode(Command):
502498
def do(self) -> None:
503499
r = self.reader
504500
r.paste_mode = not r.paste_mode
505-
if r.paste_mode and getattr(r, "can_strip_repl_prompts", False):
506-
r.buffer[:] = _strip_repl_prompts(r.get_unicode())
507-
r.pos = len(r.buffer)
508-
r.invalidate_buffer(0)
509501
r.invalidate_prompt()
510502

511503

512-
def _is_repl_session(data: str) -> bool:
513-
return data[:3] in {">>>", "..."} and data[3:4] in {"", " ", "\r", "\n"}
514-
515-
516-
def _strip_repl_prompts(data: str) -> str:
517-
lines: list[str] = []
518-
statement_start = None
519-
for line in re.findall(r"[^\r\n]*(?:\r\n?|\n|$)", data):
520-
if _is_repl_session(line):
521-
if line.startswith(">>>") or statement_start is None:
522-
statement_start = len(lines)
523-
lines.append(line[3:].removeprefix(" "))
524-
elif statement_start is not None:
525-
if line.rstrip("\r\n") == "KeyboardInterrupt":
526-
# Ctrl-C leaves the abandoned input on screen with its prompts.
527-
del lines[statement_start:]
528-
statement_start = None
529-
elif (previous := lines[-1].rstrip("\r\n")).endswith("\\"):
530-
# PyREPL wraps input with a backslash and no prompt on the
531-
# next screen row. Rejoin it without the display backslash.
532-
lines[-1] = previous[:-1] + line
533-
else:
534-
# Output (including a traceback) ends the input statement.
535-
statement_start = None
536-
return "".join(lines)
537-
538-
539504
class perform_bracketed_paste(Command):
540505
def do(self) -> None:
541506
done = "\x1b[201~"
@@ -550,12 +515,7 @@ def do(self) -> None:
550515
s=time.time() - start,
551516
)
552517
data = data.replace(done, "")
553-
if (
554-
getattr(self.reader, "paste_repl_sessions", False)
555-
and _is_repl_session(data)
556-
):
557-
if self.reader.paste_mode:
558-
data = _strip_repl_prompts(data)
559-
else:
560-
self.reader.error(_REPL_PASTE_HINT)
518+
self.process_paste(data)
519+
520+
def process_paste(self, data: str) -> None:
561521
self.reader.insert(data)

‎Lib/_pyrepl/readline.py‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from dataclasses import dataclass, field
3131

3232
import os
33+
import re
3334
from site import gethistoryfile
3435
import sys
3536
from rlcompleter import Completer as RLCompleter
@@ -120,18 +121,22 @@ def __post_init__(self) -> None:
120121
self.commands["maybe-accept"] = maybe_accept
121122
self.commands["backspace_dedent"] = backspace_dedent
122123
self.commands["backspace-dedent"] = backspace_dedent
124+
self.commands["paste_mode"] = paste_mode
125+
self.commands["paste-mode"] = paste_mode
126+
self.commands["perform_bracketed_paste"] = perform_bracketed_paste
127+
self.commands["perform-bracketed-paste"] = perform_bracketed_paste
123128

124129
@property
125130
def can_strip_repl_prompts(self) -> bool:
126131
return (
127132
self.paste_repl_sessions
128-
and commands._is_repl_session(self.get_unicode())
133+
and _is_repl_session(self.get_unicode())
129134
)
130135

131136
def get_message(self) -> str:
132137
message = super().get_message()
133138
if not message and not self.paste_mode and self.can_strip_repl_prompts:
134-
return self._format_error(commands._REPL_PASTE_HINT)
139+
return self._format_error(_REPL_PASTE_HINT)
135140
return message
136141

137142
def get_stem(self) -> str:
@@ -289,6 +294,57 @@ def _should_auto_indent(buffer: list[str], pos: int) -> bool:
289294
return last_char == ":"
290295

291296

297+
_REPL_PASTE_HINT = "press F3 (paste mode) to strip >>> and ... prompts"
298+
299+
300+
def _is_repl_session(data: str) -> bool:
301+
return data[:3] in {">>>", "..."} and data[3:4] in {"", " ", "\r", "\n"}
302+
303+
304+
def _strip_repl_prompts(data: str) -> str:
305+
lines: list[str] = []
306+
statement_start = None
307+
for line in re.findall(r"[^\r\n]*(?:\r\n?|\n|$)", data):
308+
if _is_repl_session(line):
309+
if line.startswith(">>>") or statement_start is None:
310+
statement_start = len(lines)
311+
lines.append(line[3:].removeprefix(" "))
312+
elif statement_start is not None:
313+
if line.rstrip("\r\n") == "KeyboardInterrupt":
314+
# Ctrl-C leaves the abandoned input on screen with its prompts.
315+
del lines[statement_start:]
316+
statement_start = None
317+
elif (previous := lines[-1].rstrip("\r\n")).endswith("\\"):
318+
# PyREPL wraps input with a backslash and no prompt on the
319+
# next screen row. Rejoin it without the display backslash.
320+
lines[-1] = previous[:-1] + line
321+
else:
322+
# Output (including a traceback) ends the input statement.
323+
statement_start = None
324+
return "".join(lines)
325+
326+
327+
class paste_mode(commands.paste_mode):
328+
def do(self) -> None:
329+
super().do()
330+
r: ReadlineAlikeReader = self.reader # type: ignore[assignment]
331+
if r.paste_mode and r.can_strip_repl_prompts:
332+
r.buffer[:] = _strip_repl_prompts(r.get_unicode())
333+
r.pos = len(r.buffer)
334+
r.invalidate_buffer(from_pos=0)
335+
336+
337+
class perform_bracketed_paste(commands.perform_bracketed_paste):
338+
def process_paste(self, data: str) -> None:
339+
r: ReadlineAlikeReader = self.reader # type: ignore[assignment]
340+
if r.paste_repl_sessions and _is_repl_session(data):
341+
if r.paste_mode:
342+
data = _strip_repl_prompts(data)
343+
else:
344+
r.error(_REPL_PASTE_HINT)
345+
super().process_paste(data)
346+
347+
292348
class maybe_accept(commands.Command):
293349
def do(self) -> None:
294350
r: ReadlineAlikeReader

0 commit comments

Comments
 (0)