Skip to content

Commit b060efa

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

File tree

5 files changed

+11
-26
lines changed

5 files changed

+11
-26
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

+5-7
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ 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"
83-
return super().push(line, filename=filename, _symbol=symbol)
84-
8580

8681
def run_multiline_interactive_console(
8782
mainmodule: ModuleType | None= None, future_flags: int = 0
@@ -140,7 +135,7 @@ def more_lines(unicodetext: str) -> bool:
140135
ps1 = getattr(sys, "ps1", ">>> ")
141136
ps2 = getattr(sys, "ps2", "... ")
142137
try:
143-
statement = multiline_input(more_lines, ps1, ps2)
138+
statement, contains_pasted_code = multiline_input(more_lines, ps1, ps2)
144139
except EOFError:
145140
break
146141

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

150145
input_name = f"<python-input-{input_n}>"
151146
linecache._register_code(input_name, statement, "<stdin>") # type: ignore[attr-defined]
152-
more = console.push(_strip_final_indent(statement), filename=input_name)
147+
symbol = "single" if not contains_pasted_code else "exec"
148+
more = console.push(_strip_final_indent(statement), filename=input_name, _symbol=symbol)
149+
if contains_pasted_code and more:
150+
more = console.push(_strip_final_indent(statement), filename=input_name, _symbol="single")
153151
assert not more
154152
input_n += 1
155153
except KeyboardInterrupt:

Lib/test/test_pyrepl.py

-18
Original file line numberDiff line numberDiff line change
@@ -977,24 +977,6 @@ def test_setpos_fromxy_in_wrapped_line(self):
977977
reader.setpos_from_xy(0, 1)
978978
self.assertEqual(reader.pos, 9)
979979

980-
class TestInteractiveColoredConsole(unittest.TestCase):
981-
def test_showtraceback(self):
982-
console = InteractiveColoredConsole()
983-
with patch('code.InteractiveConsole.showtraceback') as mock_showtraceback:
984-
console.showtraceback()
985-
mock_showtraceback.assert_called_once_with(colorize=console.can_colorize)
986-
987-
def test_push_single_line(self):
988-
console = InteractiveColoredConsole()
989-
with patch('code.InteractiveConsole.runsource') as mock_runsource:
990-
console.push('print("Hello, world!")')
991-
mock_runsource.assert_called_once_with('print("Hello, world!")', '<console>', symbol='single')
992-
993-
def test_push_multiline(self):
994-
console = InteractiveColoredConsole()
995-
with patch('code.InteractiveConsole.runsource') as mock_runsource:
996-
console.push('if True:\n print("Hello, world!")')
997-
mock_runsource.assert_called_once_with('if True:\n print("Hello, world!")', '<console>', symbol='exec')
998980

999981
if __name__ == '__main__':
1000982
unittest.main()

0 commit comments

Comments
 (0)