Skip to content

Commit 7429e73

Browse files
committed
Fix this better by detecting paste mode
1 parent 1525bcf commit 7429e73

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

Lib/_pyrepl/commands.py

+3
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,16 @@ def do(self) -> None:
460460
class paste_mode(Command):
461461

462462
def do(self) -> None:
463+
if not self.reader.paste_mode:
464+
self.reader.was_paste_mode_activated = True
463465
self.reader.paste_mode = not self.reader.paste_mode
464466
self.reader.dirty = True
465467

466468

467469
class enable_bracketed_paste(Command):
468470
def do(self) -> None:
469471
self.reader.paste_mode = True
472+
self.reader.was_paste_mode_activated = True
470473

471474
class disable_bracketed_paste(Command):
472475
def do(self) -> None:

Lib/_pyrepl/reader.py

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ class Reader:
221221
dirty: bool = False
222222
finished: bool = False
223223
paste_mode: bool = False
224+
was_paste_mode_activated: bool = False
224225
commands: dict[str, type[Command]] = field(default_factory=make_default_commands)
225226
last_command: type[Command] | None = None
226227
syntax_table: dict[str, int] = field(default_factory=make_default_syntax_table)

Lib/_pyrepl/readline.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,11 @@ def multiline_input(self, more_lines, ps1, ps2):
298298
reader.more_lines = more_lines
299299
reader.ps1 = reader.ps2 = ps1
300300
reader.ps3 = reader.ps4 = ps2
301-
return reader.readline()
301+
return reader.readline(), reader.was_paste_mode_activated
302302
finally:
303303
reader.more_lines = saved
304304
reader.paste_mode = False
305+
reader.was_paste_mode_activated = False
305306

306307
def parse_and_bind(self, string: str) -> None:
307308
pass # XXX we don't support parsing GNU-readline-style init files

Lib/_pyrepl/simple_interact.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ def __init__(
7777
def showtraceback(self):
7878
super().showtraceback(colorize=self.can_colorize)
7979

80-
def push(self, line, filename=None, symbol="single"):
81-
if line.count("\n") > 0:
82-
symbol = "exec"
80+
def push(self, line, filename=None, symbol='single'):
8381
return super().push(line, filename=filename, _symbol=symbol)
8482

8583

@@ -140,7 +138,7 @@ def more_lines(unicodetext: str) -> bool:
140138
ps1 = getattr(sys, "ps1", ">>> ")
141139
ps2 = getattr(sys, "ps2", "... ")
142140
try:
143-
statement = multiline_input(more_lines, ps1, ps2)
141+
statement, contains_pasted_code = multiline_input(more_lines, ps1, ps2)
144142
except EOFError:
145143
break
146144

@@ -149,7 +147,10 @@ def more_lines(unicodetext: str) -> bool:
149147

150148
input_name = f"<python-input-{input_n}>"
151149
linecache._register_code(input_name, statement, "<stdin>") # type: ignore[attr-defined]
152-
more = console.push(_strip_final_indent(statement), filename=input_name)
150+
symbol = "single" if not contains_pasted_code else "exec"
151+
more = console.push(_strip_final_indent(statement), filename=input_name, symbol=symbol)
152+
if contains_pasted_code and more:
153+
more = console.push(_strip_final_indent(statement), filename=input_name, symbol="single")
153154
assert not more
154155
input_n += 1
155156
except KeyboardInterrupt:

0 commit comments

Comments
 (0)